Manage Docker Networks
In Docker, a network is a communication mechanism that allows isolated containers to communicate with each other as well as external services located on the same host or distant machines.
The short answer
The base command for managing Docker networks is the [.inline-code] docker network[.inline-code] command, which enables developers to create, inspect, connect, disconnect, and remove networks:
Where [.inline-code] subcommand[.inline-code] includes [.inline-code] create[.inline-code] , [.inline-code] inspect[.inline-code] , [.inline-code] connect[.inline-code] , and so on.
[#easily-recall-syntax-with-ai] retrieve these commands using Wrap’s AI Command Suggestions [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve these commands using the Warp AI Command Suggestions feature:
Entering [.inline-code] docker network[.inline-code] in the AI Command Suggestion will prompt a list of [.inline-code] docker[.inline-code] commands that can then quickly be inserted into your shell by pressing [.inline-code] CMD+ENTER[.inline-code] .
[#create-a-new-network] Creating a new network [#create-a-new-network]
To create a new Docker network, you can use the [.inline-code] docker network create[.inline-code] command as follows:
For example, this command will create a new network named [.inline-code] my_network[.inline-code] :
[#specify-a-network-driver] Specifying a network driver [#specify-a-network-driver]
In Docker, a network driver is a component responsible for determining how containers on a Docker host connect and communicate with each other and with external networks.
Docker supports multiple network drivers, each designed to meet specific use cases:
- The default [.inline-code] bridge[.inline-code] driver enables communication between containers on the same host. Only the containers connected to the same bridge network can communicate, but they are isolated from containers on different bridge networks.
- The [.inline-code] host[.inline-code] driver removes network isolation between the container and the Docker host. Containers share the host's network namespace. This driver can provide better network performance as it skips the additional encapsulation used in other drivers.
- The [.inline-code] overlay[.inline-code] driver is used in Docker swarm mode for multi-host networking. It facilitates communication between containers running on different nodes in a swarm.
- The [.inline-code] macvlan[.inline-code] driver allows containers to have their own MAC addresses, giving them a physical presence on the network.
- The [.inline-code] ipvlan[.inline-code] driver enables containers to have their own MAC address but share the same IP address as the underlying host or other containers.
- The [.inline-code] none[.inline-code] driver disables all networking for a container, which is useful when a container doesn’t need network access.
To create a new Docker network with a specific driver instead of the default [.inline-code] bridge[.inline-code] driver, you can use the [.inline-code] docker network create[.inline-code] command with the [.inline-code] -d[.inline-code] flag (short for [.inline-code] --driver[.inline-code] ):
Note that, if the [.inline-code] -d[.inline-code] flag is not specified, the network driver will default to [.inline-code] bridge[.inline-code].
For example, this command will create a new network named [.inline-code] my_new_bridge[.inline-code] using the [.inline-code] host[.inline-code] driver:
[#list-existing-networks] Listing existing networks [#list-existing-networks]
To get the list of existing Docker networks, you can use the [.inline-code] docker network ls[.inline-code] command:
For example:
[#get-networks-information] Getting detailed information about networks [#get-networks-information]
To get detailed information about a Docker network, such as its name, ID, driver type, IP address, associated containers, and more, you can use the [.inline-code] docker network inspect[.inline-code] command:
For example, this command will return the detailed information of the network named [.inline-code] my_network[.inline-code] :
[#add-containers-to-networks] Adding containers to a network [#add-containers-to-networks]
To connect a running container to an existing network and enable it to communicate and exchange data with the other containers connected to the same network, you can use the [.inline-code] docker network connect[.inline-code] command as follows:
Note that, by default, Docker automatically assigns an IP address to the container from the subnet of the specified network.
For example, this command connects the container identified by the ID [.inline-code] 7783cbb0deaa[.inline-code] to the network named [.inline-code] my_network[.inline-code] :
Alternatively, you can launch a new container and connect it to a network at the same time using the [.inline-code] docker run[.inline-code] command combined with the [.inline-code] --network[.inline-code] flag as follows:
[#disconnect-containers-from-networks] Disconnecting a container from a network [#disconnect-containers-from-networks]
To disconnect a container from a network, you can use the [.inline-code] docker network disconnect[.inline-code] command as follows:
For example:
[#remove-networks] Removing networks [#remove-networks]
To remove an existing Docker network, you can use the [.inline-code] docker network rm[.inline-code] command as follows:
Note that, if containers are connected to the network you are trying to remove, Docker will prompt you for confirmation before disconnecting these containers and removing the network.
For example:
[#prune-networks] Pruning networks [#prune-networks]
To free up disk space and resources, you can remove all unused networks that are not connected to any containers (i.e. pruning) using the [.inline-code] docker network prune[.inline-code] command:
[#run-containers-in-isolation] Running containers in isolation [#run-containers-in-isolation]
The [.inline-code] none[.inline-code] network driver is used to create containers without any networking capabilities, which means that they won’t be able to communicate with the external world or with other containers.
This network mode is mostly useful for containers that are used solely for data storage, batch processing jobs, or debugging.
To create a container without any networking capabilities, you can use the [.inline-code] --network[.inline-code] flag as follows: