Lesson 9 of 30
Compose's service-name DNS feels like magic until you know what it's built on — a specific network driver, chosen from a small set of options, each with a genuinely different isolation model.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
A container is run with `--network host`. What happens to `-p 3000:3000` on that same command?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Every container gets attached to a network, and which driver that network uses decides what the container can actually reach — this is the layer underneath Compose's automatic service-name DNS from the earlier lesson, not a replacement for it. Docker ships four drivers that matter for a working developer:
localhost inside the container is the host's localhost.Isolation is the concrete, specific idea underneath all four: a bridge-network container has its own network namespace, so localhost inside it refers only to itself, never to the host machine or to a dev server running on the host's port 3000. This is exactly why the Compose lesson called out localhost as a trap — that trap is bridge-mode isolation working correctly, not a bug.
# bridge (default): isolated network, needs explicit port mapping to reach it
docker run -d -p 3000:3000 myapp:dev
# host: no isolation, no mapping needed — and no isolation from anything else either
docker run -d --network host myapp:dev
The same container, run two different ways — watch what it can reach change.
In bridge mode, -p 3000:3000 exists because the container is isolated — Docker has to explicitly forward the host's port 3000 into the container's own private network for anything outside to reach it. In host mode, that flag is meaningless: the container is already listening directly on the host's own network stack, so a host-machine process on port 3000 and a --network host container trying to bind port 3000 will collide, because they're not actually separate anymore.
localhost. host.docker.internal (on Docker Desktop) or the bridge gateway IP is the real answer — localhost:3000 inside a bridge container never reaches a server running on the host's own port 3000, no matter how obvious it feels that it should.none isn't a niche option — it's the correct choice whenever a container has no legitimate reason to touch a network at all, minimizing attack surface for something like an offline batch job.bridge network can only reach each other by IP."Why can't my container reach localhost:5432 on my machine?" is one of the most commonly actually hit Docker bugs, and answering it well means naming the mechanism — bridge-mode network namespace isolation — rather than guessing at a firewall or a typo. It's also the natural follow-up to any Compose question, since Compose's service-name DNS is this driver model with the isolation problem already solved for you.