ng serve runs in the container but localhost:4200 does not open
ng serve not working in Docker containerShort 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.
Fix it fast
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:42002Match what you see, then jump to its fix
- 1
curl works inside the container, fails from the hostThe dev server listens on localhost inside the containerFix - 2
Nothing listens on host port 4200 (docker compose ps shows no mapping)The port is not published to the hostFix - 3
The ng serve log shows a port other than 4200The PORT variable overrides --portFix
- 1
What does the error look like?
ng serve not working in Docker containerThe 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 web2Fix
Pass --host 0.0.0.0 so it listens on every interface.
# compose.yaml
services:
web:
command: npx ng serve --host 0.0.0.03Verify
Restart and request from the host.
docker compose up -d web
curl -I http://localhost:4200Confirmed by angular.dev
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 ps2Fix
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 psConfirmed by docs.docker.com
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:4200Confirmed by angular.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Servers
bcrypt_lib.node: invalid ELF header inside a Docker container
Error: /app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node: invalid ELF header
bcrypt is a compiled addon, so the file in node_modules only runs on the operating system it was installed on.
3 causes, each with a fixServers
cdk deploy fails in a GitLab job with spawnSync docker ENOENT
code: 'ENOENT'
ENOENT means the operating system found no docker program to run.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fixServers
Node.js app says Cannot find module only when it runs under Docker Compose
The same image starts fine with docker run but fails with docker compose up
The image installed node_modules, but a Compose volume mounted over the app folder hides them.
3 causes, each with a fixServers
EACCES permission denied when a container creates node_modules/.cache
The dev server exits right after start when node_modules is a Compose volume
The container user cannot write inside node_modules because the folder is owned by root.
2 causes, each with a fixServers
The Next.js container starts with docker run but Compose fails with next: not found
/bin/sh: 1: next: not found
The shell cannot find the next binary.
3 causes, each with a fix