Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

ng serve runs in the container but localhost:4200 does not open

Errorng serve not working in Docker container

Short answer

ng serve listens on localhost by default, which inside a container is the container itself. Start it with --host 0.0.0.0, publish the port with -p or ports in Compose, and keep the container port equal to the port the server really uses, since the PORT environment variable overrides --port.

  • Applies to Angular CLI 20, Docker Compose 2.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Call the dev server from inside the container, then from the host. Works inside only means the host binding or port publishing is wrong.

    docker compose exec web curl -I http://localhost:4200
    curl -I http://localhost:4200
  2. 2Match what you see, then jump to its fix

    1. 1curl works inside the container, fails from the hostThe dev server listens on localhost inside the containerFix
    2. 2Nothing listens on host port 4200 (docker compose ps shows no mapping)The port is not published to the hostFix
    3. 3The ng serve log shows a port other than 4200The PORT variable overrides --portFix

What does the error look like?

  • ng serve not working in Docker container
  • The browser cannot open localhost:4200 although ng serve is running in the container

Cause 1 of 3

The dev server listens on localhost inside the container

1Check

The ng serve --host option defaults to localhost, so only processes in the same container can connect.

docker compose logs web

2Fix

Pass --host 0.0.0.0 so it listens on every interface.

# compose.yaml
services:
  web:
    command: npx ng serve --host 0.0.0.0

3Verify

Restart and request from the host.

docker compose up -d web
curl -I http://localhost:4200

Confirmed by angular.dev

Did this fix it?

Cause 2 of 3

The port is not published to the host

1Check

Service-to-service traffic uses the container port; the host port applies only to access from outside the network, and needs a HOST:CONTAINER mapping.

docker compose ps

2Fix

Add a quoted port mapping.

services:
  web:
    ports:
      - "4200:4200"

3Verify

docker compose ps lists 0.0.0.0:4200->4200/tcp.

docker compose up -d web
docker compose ps

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

The PORT variable overrides --port

1Check

Angular documents that the PORT environment variable, when set, takes precedence over --port. A PORT in your .env or image changes the listening port, and your mapping no longer matches.

docker compose exec web printenv PORT
docker compose logs web | grep -i "localhost"

2Fix

Unset PORT or map the port ng serve actually uses.

ports:
  - "4200:4200"
environment:
  PORT: "4200"

3Verify

The log shows the port you mapped, and the host request succeeds.

curl -I http://localhost:4200

Confirmed by angular.dev

Did this fix it?

How do I stop it coming back?

  • Put --host 0.0.0.0 in the npm script used for containers.
  • Keep the port mapping and the PORT variable in one place.
  • Use a production build served by nginx for anything beyond local development.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.