Maaz Zindani

Loading the index…

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

Docker ignores your -p mapping because the container uses host networking

ErrorWARNING: Published ports are discarded when using host network mode

Short 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.

  • Applies to Docker Engine 27, Docker Desktop 4.34+, Docker Compose v2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-container
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2Port mapping is set in the service and the service also uses network_mode: hostCompose has ports: next to network_mode: hostFix
    3. 3On macOS or Windows, --network host starts but the app is not reachable from the hostHost networking is not enabled in Docker DesktopFix

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.yml

2Fix

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-app

3Verify

Request the app on the port you expect.

curl -I http://localhost:8080

Confirmed by docs.docker.com

Did this fix it?

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: here

3Verify

Recreate the service and check it is running.

docker compose up -d
docker compose ps

Confirmed by docs.docker.com

Did this fix it?

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-app

3Verify

Request the app from the host.

curl -I http://localhost:8080

Confirmed by docs.docker.com

Did this fix it?

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

Next step

Want this handled for good?

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