Redes de Docker
Tiempo estimado: 20 min
Lo que obtienes de esta lección You will create a network, start two containers on it, and show that one reaches the other by name.
In this lesson you connect containers to each other. A user-defined network lets containers find one another by name, and you prove it by running a command in one container that reaches a database in another. You also see why a container on the default network cannot do the same thing.
Docker’s networking guide explains the difference. Containers on the default bridge network can reach each other by container IP address, but they cannot refer to each other by name. Containers on a network you create can use either the IP address or the container name. Two ways of reaching a container are easy to mix up:
- A published port connects your computer to a container. Your browser uses a published port such as
8080to reach Nginx. - A network path connects one container to another. A container reaches a second container by the second container’s name and its own port, such as
db:5432. No port of your computer is involved.
Step 1: Create a network
Type this command and press Enter:
docker network create course-net
Docker prints a long identifier. The network is called course-net, and every container you attach to it can find the others by name.
Step 2: Start a database on the network
Type this command and press Enter:
docker run -d --name db --network course-net -e POSTGRES_PASSWORD=learner-only-change-me postgres:17
The new part is --network course-net, which attaches the container to your network. The container is named db. This lesson uses the tag 17 for PostgreSQL, as in Run PostgreSQL. Wait about 10 seconds for the database to start before the next step.
Step 3: Reach the database by name from a second container
Type this command and press Enter. It starts a second, short-lived container on the same network. That container runs the psql command line and asks the database db for one line of text:
docker run --rm --network course-net -e PGPASSWORD=learner-only-change-me postgres:17 psql -h db -U postgres -c "SELECT 'reached db by name' AS result;"
The parts of the command are:
--rmremoves the container as soon as its command finishes.-e PGPASSWORD=...givespsqlthe password, so it does not stop to ask for one.psql -h dbmeans “connect to the host nameddb”. Docker resolves the name to the database container’s address, because both containers are oncourse-net.
The result is a table with one row, whose value is reached db by name. The second container reached the first one by its name, with no published port in between.
Step 4: Try the same thing on the default network
Docker also has a default network that a container joins when you do not choose one. Run the same kind of command without --network course-net:
docker run --rm -e PGPASSWORD=learner-only-change-me postgres:17 psql -h db -U postgres -c "SELECT 1;"
This time the command fails. psql cannot find a host called db, because the default network does not let containers find one another by name. The database is still running, but this container is on a different network from it. Only a network you create yourself, such as course-net, gives containers name lookups. The exact wording of the message can differ between systems, but it will name db.
Step 5: Know which path is which
Your browser reaches a container through a published port. The database in this lesson publishes no port, so nothing on your computer can connect to it. Only containers on course-net can reach it.
Publishing a database port, such as -p 5432:5432, would let programs on your computer connect as well. Do that only when you need it, and set a strong password when you do.
Containers on the same network use the name and the port inside the container. Inside course-net, the name db and the port 5432 are the address of the database.
Step 6: Clean up
Stop and remove the database, then remove the network. Docker removes a network only when no containers are attached to it, so remove the container first:
docker stop db
docker rm db
docker network rm course-net
What you learned
- A user-defined network, such as
course-net, lets containers find each other by name. - The default network does not let containers find one another by name.
- A published port connects your computer to a container. A network connects one container to another.
What comes next
Go on to Docker Compose, which creates a network for you and starts several services together.