Docker networks explained: connect containers by name
A Docker network is a virtual network that lets containers on the same host reach each other, and the outside world, by rules Docker sets up.
- Updated Nov 27, 2026
- Markdown
Connect two containers by name on a user-defined network.
- Docker installed
How containers talk on a Docker network
- Only the container with a published port is reachable from outside.
- The containers all join the same user-defined network.
- On that network a container name works like a hostname.
- The web container reaches the API by name, with no address to look up.
- The API reaches the database by name over the same private network.
Static diagram
- Outside worldrequestweb
- webjoinUser-defined network
- User-defined networkname: apiapi
- webhttp://apiapi
- apidb:5432db
Containers on the same user-defined Docker network can reach each other by container name, on the container’s own port, with no -p needed. Containers on the default bridge network cannot look each other up by name, which is why a fresh docker run setup often cannot find its database.
How it works#
Docker creates virtual networks on the host. Each container gets its own network stack and an address on one or more of those networks. Containers on the same network can talk directly, on the container’s own ports. Containers on different networks cannot, unless one joins both.
There are two kinds of bridge network you will meet. The default bridge (called bridge, usually 172.17.0.0/16) is where a container lands if you name no network. Containers there reach each other only by IP address, and IP addresses change between restarts. A user-defined bridge adds an embedded DNS service: each container name resolves to its current address. That is why you should create a network for each app.
Two names to avoid mixing up: localhost inside a container is the container itself, not the host and not another container. And publishing a port with -p does something different from networking: it forwards traffic from the host to one container. Containers talking to each other never need it. A database with no -p is reachable by its neighbours but not from the host or the internet.
A worked example, start to finish#
With appnet and the db container from step 2 running, ask the built-in DNS what db is:
docker run --rm --network appnet alpine nslookup db
docker run --rm --network appnet alpine ping -c 1 dbThe first command shows the resolver address 127.0.0.11, which is Docker’s embedded DNS inside every container on a user-defined network, and then an address for db. The second pings it and gets a reply from that address. Nothing was published with -p, and it worked, because both containers sit on the same network. Stop db, start it again, and the address may change while the name keeps resolving. That is the reason to use names, never addresses.
Step 1: Create a network#
docker network create appnetWhat you should see: the new network’s ID.
Step 2: Start containers on it#
docker run -d --name db --network appnet -e POSTGRES_PASSWORD=change-me postgres:16
docker run --rm --network appnet alpine ping -c 2 dbThe second container is a throwaway (--rm) that pings db twice by name. db has no -p: containers on appnet can reach it, the host machine cannot. That is a good default for a database.
What you should see: two replies, with a line like 64 bytes from 172.x.x.x (the address of db), and no packet loss.
Step 3: Look around#
docker network ls
docker network inspect appnetnetwork ls lists networks and their drivers. inspect prints JSON, including each attached container with its address.
What you should see: appnet with driver bridge, and db under Containers.
Step 4: Compose does this for you#
Docker Compose creates a default network for each project, named after the project (for example demo_default). Every service joins it, and each service name works as a hostname. In a Compose file, an app connects to a database service named db at host db, port 5432, with no ports entry needed.
To split services, declare networks yourself:
services:
web:
image: nginx
networks: [front, back]
db:
image: postgres:16
networks: [back]
networks:
front:
back:Here web reaches db, but nothing on front alone can.
Check it worked#
The ping prints replies from db. docker network inspect appnet lists db under Containers with an IPv4 address. Run docker run --rm alpine ping -c 1 db (default bridge) and it fails with a “bad address” message, which shows the difference.
Edge cases and gotchas#
localhostis the container. An app that connects tolocalhost:5432looks inside itself. Use the service or container name.- Use the container’s own port, not the published one. If a db publishes
5433:5432, other containers still connect todb:5432. The left number only matters from the host. - Names only work on user-defined networks. The default bridge has no name lookup. Create a network or use Compose.
- Reaching the host from a container. On Linux,
host.docker.internaldoes not exist unless you add--add-host=host.docker.internal:host-gateway. And a host service bound to 127.0.0.1 is still not reachable from the container network, so it must listen on an address the container can reach. - Published ports bypass ufw. Docker writes its own firewall rules, so
-p 8080:80is open to the world even if ufw denies it. Bind to127.0.0.1or do not publish. - Address ranges can clash. Docker picks private subnets. If one overlaps with your VPN or office network, routes break. You can set a different subnet with
docker network create --subnet. - Host networking removes isolation.
--network host(Linux) shares the host’s network. The container’s ports are the host’s ports, and-pis ignored.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
ping: bad address 'db' | The containers are not on the same user-defined network | Put both on one network with --network |
network appnet has active endpoints | Containers still attached | Remove or stop them, then docker network rm appnet |
connection refused to db:5432 | The db is not ready yet, or wrong port | Wait or retry, and check docker logs db |
App reaches db but with the wrong data | Another container named db on another network | Use unique names per project |
| Works from the host, fails between containers | You used a published port or localhost | Use the service name and container port |
Error response from daemon: network with name appnet already exists | You ran create twice | Reuse it, or pick another name |
| Host cannot connect to the database container | No port is published, which is the default and is intentional | Use docker exec, or publish on 127.0.0.1 only |
| Name stops resolving after a restart | The app cached the old address | Restart the app, or make it re-resolve the name |
| Lookups of outside names fail inside a container | The host resolver or a firewall blocks the container | Test with docker run --rm alpine nslookup example.com and check host DNS |
Real-world scenario#
You run a blog container and a MySQL container. The blog’s config says the database host is localhost, so it cannot connect. You then try the container’s IP address, and it works until the next restart changes the address.
The fix takes a minute. You create a network, restart both containers with --network blognet, and change the host setting to the container name mysql. The database has no -p, so it is invisible from outside. Later you move both into a compose file, where the same names work with no extra setup, and put a reverse proxy in front of the blog as the only published service.
Quick reference#
| Task | Command |
|---|---|
| Create a network | docker network create appnet |
| Start a container on it | docker run -d --name db --network appnet IMAGE |
| Attach a running container | docker network connect appnet NAME |
| Detach it | docker network disconnect appnet NAME |
| List and inspect | docker network ls, docker network inspect appnet |
| Remove an unused network | docker network rm appnet |
Security and safety notes#
- Do not publish database ports to all interfaces. If you must, bind to
127.0.0.1and reach it over an SSH tunnel.
Related lessons#
- Docker Compose basics: the project network created for you
- Publish container ports safely: what
-pexposes and to whom - What is a reverse proxy: one published door in front of private services
- Run commands inside a container: test connections from inside
Next#
Learn how images get from your machine to a server in Image, container and registry explained.
Check what you learned
Frequently asked questions
Why can my app not reach the database by name?
On the default bridge network, containers cannot resolve each other by name. Put both on a user-defined network, or use Docker Compose, which creates one for you.
Do I need -p for containers to talk to each other?
No. Containers on the same network reach each other on the container's own port. Publishing with -p is only for access from outside Docker.
Can containers on different networks talk to each other?
Not by default. Each network is isolated. Attach a container to both networks with docker network connect, or put them on one shared network.
How does a container reach a service running on the host itself?
On Linux there is no host.docker.internal by default. Add extra_hosts with host.docker.internal:host-gateway (or --add-host with the same value), then connect to that name.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.