You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
By default, containers are isolated from each other and from the host network. Docker's networking model lets you control precisely how containers communicate — with each other, with the host, and with the outside world.
Docker supports several network drivers:
When you run a container without specifying a network, it attaches to the default bridge network. Containers on the default bridge can reach each other by IP address but not by name.
# Inspect the default bridge network
docker network inspect bridge
# See a container's IP address
docker inspect --format '{{.NetworkSettings.IPAddress}}' my-container
User-defined bridge networks are superior to the default bridge because they provide automatic DNS resolution — containers can reach each other by container name, not just by IP.
# Create a user-defined bridge network
docker network create my-network
# Run two containers on the same network
docker run -d --name backend --network my-network my-backend-image
docker run -d --name frontend --network my-network my-frontend-image
# The frontend container can reach the backend by name:
# http://backend:8080
# No need to look up IP addresses
# List networks
docker network ls
# Remove a network
docker network rm my-network
Containers have their own internal network; to reach a service from outside you must publish a port. This maps a port on the host to a port inside the container.
# Publish port 80 inside the container to port 8080 on the host
docker run -d -p 8080:80 nginx
# Bind to a specific host interface (localhost only)
docker run -d -p 127.0.0.1:8080:80 nginx
# Publish to a random available host port
docker run -d -p 80 nginx
# See which host port was assigned
docker port <container-name> 80
A typical web application has a frontend service, a backend API, and a database. Each runs in its own container. With a user-defined network, they can communicate by name:
docker network create app-network
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.