Introduction
This tutorial will cover the basics of using Docker networks. We will start with simple examples and move on to more complicated scenarios.
This tutorial will cover the basics of using Docker networks. We will start with simple examples and move on to more complicated scenarios.
To create a network, we use the docker network create
command. Let us create a bridge network named my-network
:
docker network create my-network
We can verify that the network was created by running:
docker network ls
This will list all the available networks on your machine. You should see my-network
listed.
NETWORK ID NAME DRIVER SCOPE
1191cb61c989 bridge bridge local
91199fc6ad2e host host local
32e2857073a9 minikube bridge local
47ac4e684a72 my-network bridge local
1078d2c781b6 none null local
Now that we have a network, we can launch containers in it. Let us launch two containers and connect them to our my-network
network:
docker run -d --name container1 --network my-network nginx
docker run -d --name container2 --network my-network nginx
The --network
flag specifies the network that the container should be connected to.
View the output:
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
2f44b7a888fa: Pull complete
8b7dd3ed1dc3: Pull complete
35497dd96569: Pull complete
36664b6ce66b: Pull complete
2d455521f76c: Pull complete
dc9c4fdb83d6: Pull complete
8056d2bcf3b6: Pull complete
Digest: sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
Status: Downloaded newer image for nginx:latest
65a2412dcc717a0d1d46385dd57ca8a80faf517a0fb5b14ecf0defc5f4268f31
d05411b6115018d7d77b3f4e1b8ff381458af0481ce206acf4e503578d424619
We can now test connectivity between the containers. Let us execute a command in container1
that will access container2
:
docker exec container1 curl container2
This should return the HTML content of the nginx web server running in container2
.
labex:~/ $ docker exec container1 curl container2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 615 100 615 0 0 376k 0 --:--:-- --:--:-- --:--:-- 600k
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
A container can be connected to multiple networks. Let us create another network and connect container2
to both networks:
docker network create my-network2
docker network connect my-network2 container2
We can verify that container2
is now connected to both networks by running:
docker network inspect my-network2
This will show the list of containers connected to the my-network2
network. You should see container2
listed.
Use the docker network inspect my-network2 | grep container2
call to get the direct result.
labex:~/ $ docker network inspect my-network2 | grep container2
"Name": "container2",
We can disconnect a container from a network using the docker network disconnect
command. Let us disconnect container2
from my-network
:
docker network disconnect my-network container2
We can verify that container2
is no longer connected to my-network
by running:
docker network inspect my-network
This will show the list of containers connected to my-network
. You should not see container2
listed.
Use the docker network inspect my-network | grep container
call to get the direct result.
labex:~/ $ docker network inspect my-network | grep container
"Name": "container1",
We can remove a network using the docker network rm
command. Let us remove "my-network2":
docker network disconnect my-network2 container2
docker network rm my-network2
We can verify that "my-network2" was removed by running:
docker network ls
This will list all the available networks on your machine. You should not see "my-network2" listed.
NETWORK ID NAME DRIVER SCOPE
1191cb61c989 bridge bridge local
91199fc6ad2e host host local
32e2857073a9 minikube bridge local
47ac4e684a72 my-network bridge local
1078d2c781b6 none null local
In this tutorial, we covered the basics of using Docker networks. We created a network, launched containers in the network, tested connectivity, connected a container to multiple networks, disconnected a container from a network, and removed a network.