Contents
Docker Network Command Tutorial: Create Networks, Connect Containers, and Use Aliases
docker network manages Docker networks. For the network concept and the differences between default and custom networks, see Networking between containers.
Subcommands
| Command | Description |
|---|---|
docker network ls | List networks |
docker network create NAME | Create a network |
docker network inspect NAME | Show details, including connected containers and their IP addresses |
docker network connect NETWORK CONTAINER | Attach a running container to a network |
docker network disconnect NETWORK CONTAINER | Detach a container from a network |
docker network rm NAME... | Remove networks (you can't remove one while containers are connected) |
docker network prune | Remove all custom networks that aren't used by containers |
Common docker network create options:
| Option | Description |
|---|---|
-d, --driver | Driver; defaults to bridge |
--subnet | Subnet, such as 172.30.0.0/24 |
--gateway | Gateway IP address |
--ip-range | Allocate IP addresses from a specific range within the subnet |
--internal | Prevent connections to external networks |
--ipv6 | Enable IPv6 |
--label | Add a label |
Common docker network connect options: --alias ALIAS and --ip STATIC_IP.
Examples
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
58a492b7c501 bridge bridge local
720ed2121dc4 host host local
07184d7358b0 none null local
$ docker network create appnet
c94640ca44aae05529e21bfe93c1fd957f70ca0d4bcc40209e91aa434dbc65e4
$ docker network create --subnet 172.30.0.0/24 fixednet
320f2c205537c3957304a23459143038a39e3b3a9323edc3bb5cec13688a1cf5
$ docker run -d --name api --network appnet --network-alias backend nginx:1.30-alpine
a40d4c075b88d806529bc6b58f9e1a90f6bc918eed0af6a852e3e8983f3e8e7a
$ docker run --rm --network appnet alpine:3.23 sh -c "ping -c1 -W1 api >/dev/null && echo api ok; ping -c1 -W1 backend >/dev/null && echo backend ok"
api ok
backend ok
$ docker network inspect -f "{{range .Containers}}{{.Name}} {{.IPv4Address}}{{end}}" appnet
api 172.19.0.2/16
- The three default networks are
bridge(the default),host(uses the host's network directly), andnone(no network). - Create
appnetandfixednetwith a specified subnet. - The
apicontainer joinsappnetand gets an additional name,backend, through--network-alias. Other containers on the same network can reach it using eitherapiorbackend.
$ docker network connect fixednet api
$ docker inspect -f "{{range \$k, \$v := .NetworkSettings.Networks}}{{\$k}}={{\$v.IPAddress}} {{end}}" api
appnet=172.19.0.2 fixednet=172.30.0.2
$ docker network disconnect fixednet api
$ docker network rm appnet
Error response from daemon: error while removing network: network appnet has active endpoints (name:"api" id:"df7a1c38176a")
exit status 1
$ docker rm -f api
api
$ docker network rm appnet fixednet
appnet
fixednet
network connectalso attaches the runningapicontainer tofixednet, giving it two network interfaces and two IP addresses:172.19.0.2onappnetand172.30.0.2from the specified subnet onfixednet. This lets a container communicate with two groups of services at once, such as a reverse proxy connected to both a frontend network and a backend network.- Removing a network while a container is connected causes an error:
network appnet has active endpoints. Remove the container (or rundisconnect) before removing the network.
Related chapters
- Networking between containers
- Nginx and reverse proxies
- compose.yaml syntax:
networksin Compose
Exercises
Exercise: Create an --internal network named private, start a container attached to it, and confirm that the container can't connect externally (for example, run ping -c1 1.1.1.1).
Answer
docker network create --internal private
docker run --rm --network private alpine:3.23 ping -c1 -W2 1.1.1.1
The tested output is ping: sendto: Network unreachable: the container has no route to the outside. When you're done, run docker network rm private.
FAQ
Why can't I connect by container name on the default network?
The default bridge network doesn't provide name resolution. Custom networks (docker network create) and networks created by Compose automatically resolve IP addresses by container name and service name.
What is --network-alias for?
It gives a container an additional name on a network. For example, you can name a container api-v2 while other services still connect to backend; or multiple containers can use the same alias to distribute requests simply.
What is an --internal network?
On a network created with --internal, containers can communicate with each other but can't connect to external networks. It's suitable for services such as databases that don't need internet access.
Updated October 14, 2026