Dynamic App Link Printing for Dockerised Environments

Improve developer experience by printing the correct local app URL dynamically at startup.


๐Ÿ”— Purpose

In containerised development environments, it's easy to lose track of what port an app is running on โ€” especially when host-to-container port mappings differ.

To solve this, we introduce a simple pattern that dynamically prints the correct app link (e.g. http://localhost:4001) when the server starts, based on the current environment variable (e.g. HOST_PORT).

This reduces guesswork and ensures developers always know exactly where to access the application โ€” whether it's running locally or inside Docker.


โš™๏ธ High-Level Approach

  1. Keep the app startup script clean and reusable

    Define your actual server start command in a dedicated script (e.g. start-server.sh):

    next start -p 3001

    This script contains no logging or environment logic and can be reused in CI, production, or other automation.

  2. Add a dynamic wrapper

    Create a wrapper script (e.g. start.ts or start.js) that:

    • Spawns the actual start script
    • Waits for the server "ready" signal
    • Detects the port from the environment (e.g. HOST_PORT)
    • Prints the resolved URL like:
    App ready โ€” visit: http://localhost:4001
    

โœ… Why This Matters

  • ๐Ÿงญ Instant feedback: Clear startup logs help devs find the running app without searching Docker configs or port mappings.
  • ๐Ÿ’ป Portable setup: Works the same in Docker, local dev, or cloud shell environments.
  • ๐Ÿ”„ DRY principle: Keeps logic clean and reusable across environments.
  • ๐Ÿงช CI/CD friendly: Start logic remains testable and scriptable.

๐Ÿงช Example Output

In Docker:

> App starting...
> App ready โ€” visit: http://localhost:4001

Locally:

> App ready โ€” visit: http://localhost:3001

๐Ÿ—‚๏ธ Suggested File Structure

scripts/
โ”œโ”€โ”€ start-server.sh      # Minimal entry point (no logs, no logic)
โ””โ”€โ”€ start.js or start.ts # Dynamic wrapper (prints resolved URL)
.env                     # Defines HOST_PORT or fallback
package.json

โœจ Tip

Inject HOST_PORT into your environment via Docker or shell:

HOST_PORT=4001 node scripts/start.js

This makes it easy to reuse across local and containerised setups.


๐Ÿงพ Summary

This simple pattern helps teams:

  • Remove confusion around Docker port mapping
  • Improve developer onboarding and DX
  • Maintain clean separation of concerns between start logic and messaging

Feel free to adapt it to your own stack โ€” whether you're using Node.js, Python, or any web server.