Once you have more than one container, they need a way to talk. Docker networking controls communication between containers and between a container and your computer.

Default bridge network

By default, containers run on a bridge network.

Containers can make outbound requests, but inbound access from the host requires published ports.

Published ports

docker run -p 8080:3000 my-api

This means:

Requests to localhost:8080 on the host are forwarded to port 3000 inside the container.

Container-to-container communication

Containers on the same user-defined network can reach each other by container name.

docker network create app-net
docker run --name api --network app-net my-api
docker run --name db --network app-net postgres

The API can connect to host db.

Compose service names

In Docker Compose, services can reach each other by service name.

Example:

Do not use localhost from one container to reach another container. Inside a container, localhost means that same container.

Production view

Docker networking concepts carry into container platforms, but production networking is usually handled by orchestrators, load balancers, and service discovery.

Quick revision