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

CommandDescription
docker network lsList networks
docker network create NAMECreate a network
docker network inspect NAMEShow details, including connected containers and their IP addresses
docker network connect NETWORK CONTAINERAttach a running container to a network
docker network disconnect NETWORK CONTAINERDetach a container from a network
docker network rm NAME...Remove networks (you can't remove one while containers are connected)
docker network pruneRemove all custom networks that aren't used by containers

Common docker network create options:

OptionDescription
-d, --driverDriver; defaults to bridge
--subnetSubnet, such as 172.30.0.0/24
--gatewayGateway IP address
--ip-rangeAllocate IP addresses from a specific range within the subnet
--internalPrevent connections to external networks
--ipv6Enable IPv6
--labelAdd 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), and none (no network).
  • Create appnet and fixednet with a specified subnet.
  • The api container joins appnet and gets an additional name, backend, through --network-alias. Other containers on the same network can reach it using either api or backend.
$ 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 connect also attaches the running api container to fixednet, giving it two network interfaces and two IP addresses: 172.19.0.2 on appnet and 172.30.0.2 from the specified subnet on fixednet. 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 run disconnect) before removing the network.

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