docker network

Put containers on the same network so they reach each other by name — create, --network, connect and inspect.

>_ Try it: a simulated Linux, right here
Loading the terminal…

Examples

  • docker network create app-net
  • docker run -d --name api --network app-net nginx
  • docker run -d --name web nginx
  • docker network connect app-net web
  • docker network inspect app-net
  • docker network ls

How it works

Containers on the same user-made network find each other by name: from web, the address http://api reaches the api container. That is how an app talks to its database.

Create one with docker network create NAME, start containers on it with --network NAME, or add a running one with docker network connect.

Syntax

docker network create|ls|inspect|connect|rm [NAME] [CONTAINER]

Options

OptionWhat it does
create NAMEcreate a network
--network NAMEstart a container on it (docker run)
connect NET CONTAINERadd a running container
inspect NAMEwhich containers are on it, and their IPs
lslist networks

Good to know

  • Publishing ports (-p) is for reaching a container from your machine; containers on the same network don't need it to talk to each other.
  • Docker Compose creates a network for each project, so its services reach each other by service name.