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 herenothing runs on your computer
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
| Option | What it does |
|---|---|
create NAME | create a network |
--network NAME | start a container on it (docker run) |
connect NET CONTAINER | add a running container |
inspect NAME | which containers are on it, and their IPs |
ls | list 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.