Containers and Kubernetes · Intermediate · 16 min
Connect containers with user-defined Docker networks
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Docker Engine 29.10
You will run a PostgreSQL database on an internal Docker network with no route out, reach it by name from an Adminer container that sits on two networks and is published to 127.0.0.1 only, and then write the same layout as a Compose file.
On this page
Every container you start without a --network flag lands on Docker's default bridge network. It works, but it gives you nothing to organise with: containers there can only find each other by IP address, and every unrelated container on the server shares the same network.
User-defined networks fix both. Containers on one find each other by name through a DNS server that Docker runs for that network, and only the containers you attach can talk on it. You can also mark a network as internal so nothing on it can reach the internet, which suits a database.
In this tutorial you build a small, realistic layout: a PostgreSQL 18 database on an internal network, and Adminer (a one-file web admin tool for databases) on a front network and the database network at once, published only to the server's loopback address. You then check it with docker network inspect and a throwaway debug container, look at when host networking makes sense, and finish with the same layout in a Compose file.
The images used are postgres:18.7, adminer:6.3.1 and busybox:1.38.0. BusyBox is only a few megabytes; the PostgreSQL image is much larger, so if you are on prepaid mobile data, pull it once on a fixed connection with docker pull postgres:18.7 before you start.
Step 1 · See the limit of the default bridge
Work in a throwaway folder so nothing here mixes with your own projects:
mkdir -p ~/networks-demo
cd ~/networks-demoStart a long-running BusyBox container with no network flag, then try to reach it by name from a second, one-off container that --rm deletes as soon as it exits:
docker run -d --name lone1 busybox:1.38.0 sleep 600
docker run --rm busybox:1.38.0 ping -c 1 lone1The ping fails with ping: bad address 'lone1'. Now look up its IP address and ping that instead:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' lone1
docker run --rm busybox:1.38.0 ping -c 1 "$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' lone1)"The address works. That is the default bridge in short: reachable, but only by an IP that changes whenever the container is recreated. The old --link flag patched over this and is now legacy; do not use it in new setups.
Remove the BusyBox container:
docker rm -f lone1Check it worked
You saw bad address 'lone1' for the name and 1 packets received for the IP address, and docker ps -a --filter name=lone1 now lists nothing.
Step 2 · Create a user-defined network and resolve names
Create a bridge network of your own and start two containers on it:
docker network create notes-net
docker run -d --name peer1 --network notes-net busybox:1.38.0 sleep 600
docker run -d --name peer2 --network notes-net busybox:1.38.0 sleep 600
docker exec peer2 ping -c 1 peer1This time the name resolves. Look at how the container finds it:
docker exec peer2 cat /etc/resolv.confThe nameserver line reads 127.0.0.11. That is Docker's embedded DNS server. It answers with the names of containers on the same user-defined networks and forwards other queries to the DNS servers the host uses.
Clean up the two peers and the network:
docker rm -f peer1 peer2
docker network rm notes-netCheck it worked
The ping from peer2 printed a line like 64 bytes from 172.18.0.2, resolv.conf showed nameserver 127.0.0.11, and docker network ls no longer lists notes-net.
Step 3 · Put the database on an internal network
A database should not be able to reach the internet: it never needs to, and a compromised database that can phone home is worse than one that cannot. The --internal flag creates a network whose containers can talk to each other but not to any other network, including the route out.
Create the internal back network and a normal front network:
docker network create --internal notes-back
docker network create notes-frontPostgreSQL needs a password. Keep it out of your shell history and the host's process list by putting it in a file only you can read. Docker still stores it in the container's settings, so anyone who can run docker inspect db can see it; for production, use Docker secrets or the image's POSTGRES_PASSWORD_FILE variable. Create the empty file, then type the password at a hidden prompt:
install -m 600 /dev/null db.env
read -rs -p 'Choose a PostgreSQL password: ' PGPW; echo
printf 'POSTGRES_PASSWORD=%s\n' "$PGPW" >> db.env
unset PGPWStart the database on the internal network only:
docker run -d --name db --network notes-back --env-file db.env postgres:18.7Now prove it has no way out. Run a debug container on the same network and try to fetch a public website:
docker run --rm --network notes-back busybox:1.38.0 wget -T 5 -q -O /dev/null http://example.comIt fails, with an error such as bad address 'example.com' or download timed out. Run the same command on notes-front and it succeeds silently.
One limit to know: an internal network still lets containers reach the Docker host itself through the network's gateway address. It cuts the route out of the server, not access to services listening on the host.
Check it worked
docker network inspect notes-back --format '{{.Internal}}' prints true, docker ps shows db as Up, the wget on notes-back printed an error, and ls -l db.env shows -rw-------.
Step 4 · Join Adminer to two networks and publish it to 127.0.0.1
Adminer needs to reach the database on notes-back, and you need to reach Adminer. A container that publishes a port needs a network with a route out, so start it on notes-front with the port published, then attach it to notes-back as well:
docker run -d --name adminer --network notes-front -p 127.0.0.1:8080:8080 adminer:6.3.1
docker network connect notes-back adminerThe 127.0.0.1: part of the -p value matters. Without an address, Docker publishes the port on every address the server has, IPv4 and IPv6, and its firewall rules come before the ones UFW manages, so a UFW deny rule does not stop it. Binding to 127.0.0.1 keeps the port on the server itself. The install tutorial's firewall step explains the UFW interaction in more detail.
Confirm Adminer answers locally on the server:
curl -s http://127.0.0.1:8080 | grep -o '<title>[^<]*</title>'To use it from your own computer, open an SSH tunnel. Run this on your laptop, replacing you@your-server with your login and server address:
ssh -N -L 8080:127.0.0.1:8080 you@your-serverLeave it running and open http://localhost:8080 in your laptop's browser. Choose PostgreSQL as the system, type db as the server, postgres as the username and the password you chose. Press Ctrl+C in the tunnel terminal when you are done.
Check it worked
The curl printed a title containing Adminer, and logging in through the tunnel showed the postgres database in Adminer's list. From any other machine, http://<server-ip>:8080 does not load. If you can see postgres there, Adminer resolved db over notes-back, because that is the only network the two share.
Step 5 · Give the database a second name with an alias
Apps often hard-code a host name such as postgres. Rather than renaming the container, give it a network alias. An alias is scoped to one network, so the same container can answer to different names on different networks.
Recreate the database with an alias on notes-back. Removing the container also removes the data inside it, which is fine for this demo; the volumes tutorial covers keeping it.
docker rm -f db
docker run -d --name db --network notes-back --network-alias postgres --env-file db.env postgres:18.7For a container that is already running, docker network connect --alias <name> <network> <container> adds an alias when you attach it.
Resolve both names from a debug container on the same network:
docker run --rm --network notes-back busybox:1.38.0 nslookup db
docker run --rm --network notes-back busybox:1.38.0 nslookup postgresCheck it worked
Both lookups show Server: 127.0.0.11 and return the same address. In Adminer (through the tunnel again), logging in with postgres as the server works the same way db did.
Step 6 · Inspect networks and debug from a throwaway container
docker network inspect prints a network's subnet, gateway, flags and attached containers as JSON. A format string picks out the parts you want. List what is on each network:
docker network inspect notes-back --format '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'
docker network inspect notes-front --format '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'From the container side, list the networks one container is on:
docker inspect adminer --format '{{range $name, $_ := .NetworkSettings.Networks}}{{$name}}{{"\n"}}{{end}}'When an app reports that it cannot reach another service, start a debug container on the same network as the app and check from there. That way you check the network the app uses, not the server's:
docker run --rm --network notes-back postgres:18.7 pg_isready -h db
docker run --rm --network notes-front busybox:1.38.0 nslookup dbThe first command uses the pg_isready tool from the PostgreSQL image you already pulled and prints something like db:5432 - accepting connections. The second fails to resolve db, because the database is not on notes-front. That one difference, which network a container is on, is behind most "could not translate host name" errors you will meet.
Check it worked
notes-back lists db and adminer, notes-front lists only adminer, Adminer reports both networks, pg_isready reported accepting connections, and the nslookup on notes-front returned an error rather than an address.
Step 7 · Decide when host networking fits
With --network host, a container uses the server's own network stack. It gets no IP address of its own, binds directly to the server's interfaces, and Docker ignores any -p flags (it prints a warning). See it for yourself:
docker run --rm --network host busybox:1.38.0 ip addrThe output lists the server's real interfaces, such as eth0 or ens3, and docker0, not a single eth0 with a 172.x address.
When it fits:
- The container needs many ports or a wide port range, where publishing each one is impractical.
- You need the last bit of network performance and want to skip Docker's address translation.
- A monitoring agent needs to see the host's own interfaces.
What you give up:
- Isolation. Anything the container listens on is on the server's interfaces, and
127.0.0.1inside the container is the server's loopback. - Name lookup by container name, and the internal network control you built above.
- Two containers cannot use the same port, and they compete with services on the host.
On Docker Engine for Linux it works as described. Docker Desktop on macOS and Windows supports it from version 4.34 only after you turn it on in Settings, with limits, so do not rely on it for a setup that has to run the same everywhere.
Check it worked
The ip addr output showed more than one interface, with the same names and addresses as ip addr run directly on the server.
Step 8 · Write the same layout in Compose
Compose creates networks for you. With no networks key, every service joins one network named after the project (the folder name), and services find each other by service name. Declaring networks yourself gives you the same front and back split.
Remove the containers and networks from the earlier steps first, because the Compose version uses the same port:
docker rm -f adminer db
docker network rm notes-back notes-frontCreate compose.yaml in ~/networks-demo:
services:
db:
image: postgres:18.7
env_file: db.env
networks:
back:
aliases: [postgres]
adminer:
image: adminer:6.3.1
ports:
- "127.0.0.1:8080:8080"
networks: [front, back]
depends_on: [db]
networks:
front: {}
back:
internal: trueIt reuses the db.env file you created earlier. Check the file, then start it:
docker compose config --quiet && echo "compose.yaml is valid"
docker compose up -d
docker network ls --filter name=networks-demo
docker network inspect networks-demo_back --format '{{.Internal}}'Compose prefixes each network with the project name, so you get networks-demo_front and networks-demo_back. Repeat the internal check from earlier against the Compose network:
docker run --rm --network networks-demo_back busybox:1.38.0 wget -T 5 -q -O /dev/null http://example.comWhen you have finished, remove everything this tutorial created. This deletes the demo database and its password file:
docker compose down
cd ~
rm -rf ~/networks-demoCheck it worked
docker compose config printed compose.yaml is valid, the network list showed networks-demo_front and networks-demo_back, the inspect printed true, and the wget failed again. After cleanup, docker ps -a and docker network ls show nothing from networks-demo and no notes- networks.
Next steps
- Keep the database's data across container rebuilds in Docker volumes and bind mounts.
- Build a full app with a database and health checks in a Docker Compose app with PostgreSQL.
- Revisit how published ports interact with UFW in the firewall step of installing Docker Engine on Ubuntu 26.04.
Related tutorials
- How to Run an App and PostgreSQL Together with Docker ComposeYou will describe a Node.js app and a PostgreSQL 18 database in one compose.yaml, start both with one command, keep the data in a named volume, and keep the database password out of the file.
- How to Install Docker Engine on Ubuntu 26.04You will install Docker Engine 29 from Docker's official apt repository on Ubuntu 26.04, run the hello-world image to prove it works, and set it up to start at boot, run without sudo and keep its logs from filling the disk.
- Keep container data safe with Docker volumes and bind mountsYou will keep data in a named volume, edit files through a read-only bind mount, fix a user ID mismatch, use tmpfs for scratch files, and back up and restore a volume with a throwaway container and tar.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials