In this article, we will talk about how to create and consume a Docker network, and to demonstrate it, we have provided examples created on Docker Desktop.
Make sure that Docker Desktop is on your computer. Otherwise, follow the link to find out how to install Docker Desktop.
Briefly about Docker Network
One of the primary reasons Docker containers and services are so useful is their ability to communicate with one another and with non-Docker workloads. Whether your Docker hosts run Linux, Windows, or a combination of the two, Docker can manage them in a platform-independent manner.
Docker automatically creates three networks during installation: bridge, none, and host. The default network driver is bridge. Bridge networks are typically ideal for application development in isolated containers, which requires communication.
Follow the link for more Docker Network theory.
How to Create Docker Network
The easiest way to create your own Docker network is “bridge”, and hence we will demonstrate how to create it in this section.
Get a list of the networks from the Docker network:
PS C:\docker-host\TestProject> docker network ls
NETWORK ID NAME DRIVER SCOPE
285c81e804e9 bridge bridge local
5fb8ad8bb95f host host local
83f3da821570 none null local
000d8c5d22dd od-docker-demo_app-network bridge local
5094064cdf2c postgres-network bridge local
f4654962472a testproject_default bridge local
Let us create a new bridge network:
PS C:\docker-host\TestProject> docker network create php-bridge-net
dddbaa8a29a1afb9f0eacb4132122b343afc1f40eb1f829a75dd68afd5098ac3
Check the installed Docker networks again:
PS C:\docker-host\TestProject> docker network ls
NETWORK ID NAME DRIVER SCOPE
285c81e804e9 bridge bridge local
5fb8ad8bb95f host host local
83f3da821570 none null local
000d8c5d22dd od-docker-demo_app-network bridge local
dddbaa8a29a1 php-bridge-net bridge local
5094064cdf2c postgres-network bridge local
f4654962472a testproject_default bridge local
Inspect the installed bridge network:
PS C:\docker-host\TestProject> docker network inspect php-bridge-net
[
{
"Name": "php-bridge-net",
"Id": "dddbaa8a29a1afb9f0eacb4132122b343afc1f40eb1f829a75dd68afd5098ac3",
"Created": "2022-01-20T09:56:03.4992475Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.26.0.0/16",
"Gateway": "172.26.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
How to Consume a Desktop Network
You can add containers to the installed network:
PS C:\docker-host\TestProject> docker run --net=php-bridge-net --name=testproject_php_2 testproject_php
[20-Jan-2022 10:10:54] NOTICE: fpm is running, pid 1
[20-Jan-2022 10:10:54] NOTICE: ready to handle connections
The container added to the Docker network. You can use it and then remove both container and network:
docker stop 1652f95c16b5
docker rm 1652f95c16b5
docker network rm php-bridge-net
Run the network list command as it is done above to ensure that Docker network php-bridge-net is removed.
Other Docker Network Types
In the preceding sections, we demonstrated how to create and consume a bridge-type network. Docker also supports a variety of other network types, and we will review them in this section briefly.
None: this network is internal to the Docker, and it has no external network interface.
Host: this network type enables a container to attach to your host’s network. It is useful when the network stack should remain connected to the Docker host but other aspects of the container should be isolated.
Overlay: you should create an overlay network if you need containers running on different Docker hosts to communicate, or when multiple applications communicate with one another via swarm services.
Macvlan: this network type allows you to assign a MAC address to a container and consume it as a physical device on your network.
Was this helpful?
0 / 0