Docker ignores your -p mapping because the container uses host networking
WARNING: Published ports are discarded when using host network modeShort answer
In host network mode the container shares the host's network stack and has no IP address of its own, so there is nothing to map. Docker ignores -p and prints the warning. Drop the port mapping and use the port the app listens on, or switch to a bridge network if you need a different host port.
Fix it fast
1Find out which cause you have
Check how the container is attached. If the network mode is host, the warning is expected and the cause is your run command or compose file, not a bug.
docker inspect --format '{{.HostConfig.NetworkMode}}' my-container2Match what you see, then jump to its fix
- 1
The warning appears on docker run, or Compose fails at runtime when ports: and network_mode: host are both set-p or ports: is combined with --network hostFix - 2
Port mapping is set in the service and the service also uses network_mode: hostCompose has ports: next to network_mode: hostFix - 3
On macOS or Windows, --network host starts but the app is not reachable from the hostHost networking is not enabled in Docker DesktopFix
- 1
What does the error look like?
WARNING: Published ports are discarded when using host network mode
Cause 1 of 3
-p or ports: is combined with --network host
1Check
Look for the two options together in your run command or compose file.
docker inspect --format '{{.HostConfig.NetworkMode}} {{json .HostConfig.PortBindings}}' my-container
grep -n -E "network_mode|ports:" docker-compose.yml2Fix
Remove the port mapping. The app is reachable on the port it listens on, as seen from the host (for a Spring Boot app, its server.port). If you want a different host port, stop using host mode and publish with -p on a bridge network.
# host mode: no -p
docker run --network host my-app
# or bridge mode with a mapping
docker run -p 8081:8080 my-app3Verify
Request the app on the port you expect.
curl -I http://localhost:8080Confirmed by docs.docker.com
Cause 2 of 3
Compose has ports: next to network_mode: host
1Check
The Compose file reference says port mapping must not be used with network_mode: host, because the container ports are already on the host's network.
docker compose config | grep -n -E 'network_mode|published'2Fix
Delete the ports: block from that service, or delete network_mode: host and keep the mapping.
services:
app:
image: my-app
network_mode: host
# no ports: here3Verify
Recreate the service and check it is running.
docker compose up -d
docker compose psConfirmed by docs.docker.com
Cause 3 of 3
Host networking is not enabled in Docker Desktop
1Check
Docker Desktop supports host networking from version 4.34 and only after the feature is switched on. Check the installed version.
docker version --format '{{.Client.Version}}'2Fix
Sign in to Docker Desktop, open Settings, Resources, Network, turn on Enable host networking, then Apply and restart. Note that the container's processes cannot bind to the host's IP addresses, and only TCP and UDP work.
# after enabling the setting
docker run --rm --network host my-app3Verify
Request the app from the host.
curl -I http://localhost:8080Confirmed by docs.docker.com
How do I stop it coming back?
- Pick one model per service: bridge with a port mapping, or host without one.
- Prefer bridge networks for anything exposed to others; host mode removes the network isolation between container and host.
- Keep the listen port in the app configuration, and use the same number in curl checks and health checks.
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
Servers
apk cannot find py-pip in an Alpine Docker image
py-pip (no such package):
On current Alpine releases there is no package called py-pip.
2 causes, each with a fixServers
ng serve runs in the container but localhost:4200 does not open
The browser cannot open localhost:4200 although ng serve is running in the container
ng serve listens on localhost by default, which inside a container is the container itself.
3 causes, each with a fixServers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
AH00558: Apache in Docker cannot reliably determine its fully qualified domain name
Set the 'ServerName' directive globally to suppress this message
AH00558 is a warning, not a crash.
2 causes, each with a fixServers
Apache in a Docker container logs AH01114 when it proxies to a backend
AH01114: HTTP: failed to make connection to backend: api
Apache's proxy worker could not open a connection to the address in ProxyPass.
5 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fix