- Ansible beginner tutorial - Wed, Nov 15 2023
- Nested Docker containers: Run Docker in a Docker container - Fri, Nov 10 2023
- Docker networking: Connect a Docker container - Fri, Oct 27 2023
Create Docker networks
To get help and information about the docker network create command, use the –help option, as shown below.
docker network create –-help
You can use the docker network create command to create a Docker network. The basic syntax for creating a Docker network is shown below.
docker network create [OPTIONS] NETWORK
There are different types of Docker network drivers that you can use to create a specific type of Docker network. In this section, we will show you how to use some of them to create a Docker network.
Create a custom bridge network
Bridge is a private default network driver. The bridge network driver allows containers connected to the same bridge network to communicate while providing isolation from containers not connected to that bridge network. To create a custom bridge network named test-network, use the following command:
docker network create test-network
Create an overlay network
An overlay network in Docker facilitates distributed network communication between containers running on different hosts, often used in multihost and orchestrated environments like Docker Swarm. To create an overlay network named test-overlay-network, use the following command:
docker network create --driver overlay test-overlay-network
Create a macvlan network
A macvlan network in Docker allows a container to be directly connected to the physical host network, assigning a unique MAC address to each container, thereby making it appear as a physical network device on the network. To create a macvlan network named test-macvlan-network with a private subnet and gateway, run the following command:
docker network create --driver macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 test-macvlan-network
List Docker networks
You can use the docker network ls or docker network list command to list Docker networks on your system. You can use this list to identify your created network and manage Docker network configurations.
docker network ls
You will see the list of all created networks on the following screen.
Inspect Docker networks
You can inspect a Docker network using the docker network inspect command. This command retrieves detailed, low-level information about Docker networks in a JSON format.
The basic syntax for inspecting a Docker network is shown below.
docker network inspect [network-name]
For example, to inspect the Docker network named test-network, run the following command:
docker network inspect test-network
You will see detailed information about your network, such as the network's name, ID, driver, scope, and list of connected containers on the following screen.
Connect containers to a Docker network
You connect containers to a Docker network to enable communication between them.
The basic syntax for connecting containers to a network is shown below:
docker network connect [network-name] [container-name]
For example, to connect your existing container named nginx-container to a Docker network named test-network, run the following command:
docker network connect test-network nginx-container
You can now verify the connected container using the following command:
docker network inspect test-network
If you want to create a new container with your custom network, use the --network option, as shown below:
docker run --network test-macvlan-network nginx:latest
Here is an explanation of the above command:
- test-macvlan—Specify the name of your network.
- nginx:latest—Specify the image used to create a container.
You can also use the --network host flag to run a container in host networking mode:
docker run -dit --network host nginx:latest
In Docker, host networking mode allows a container to share the same network namespace as the host system, meaning the container uses the host's networking directly without any isolation, effectively giving it full access to the host's network interfaces and IP address.
Disconnect containers from a Docker network
You can use the docker network disconnect command to disconnect containers from a Docker network.
First, find the name of the network that you want to disconnect from the container using the docker network ls command.
docker network ls
After identifying the network name, run the following command:
docker network disconnect [container-name-or-id]
Retrieve the current networking configuration:
docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}"
To disconnect a container named nginx-container from a network named test-network, run the following:
docker network disconnect test-network nginx-container
The above command will disconnect nginx-container from test-network.
You can now verify that the network has been disconnected.
docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}"
The docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}" command retrieves networking details of a Docker container named nginx-container. The output is formatted as a JSON string, focusing specifically on its network configurations. It uses Go templating to extract and present only the desired information from the container's comprehensive details.
Use Docker networks in Docker Compose
If you are using Docker Compose for multicontainer applications, you can define networks in the docker-compose.yml file using the networks section.
For example, create a docker-compose.yml file for the NGINX web server and define a custom network:
nano docker-compose.yml
Add the following configurations to the file:
version: '3' services: web: image: nginx:latest networks: - my-network networks: my-network: driver: bridge
Save and close the file, and then run the docker-compose up -d command to create an NGINX container in the my-network Docker network.
docker-compose up -d
This command will create an NGINX container and connect it to the my-network Docker network.
To verify that your new container is connected to the correct network, inspect the container:
docker inspect nginx-container -f "{{json .NetworkSettings.Networks }}"
Remove Docker networks
You can use the docker network rm command to remove your existing network from your system. The basic syntax for removing a Docker network is shown below.
docker network rm [network-name]
For example, to remove a network named test-network, run the following command:
docker network rm test-network
You can also use the docker network prune command to remove all unused networks from your system. This will help you clean up your system and free up resources.
To remove all unused networks, run the following command:
Subscribe to 4sysops newsletter!
docker network prune
Conclusion
Navigating the intricacies of connecting Docker containers is integral to maximizing Docker's capabilities. By leveraging different network types, such as bridge, overlay, and macvlan, you can architect diverse and adaptable communication pathways for your containers.
Read the latest IT news and community updates!
Join our IT community and read articles without ads!
Do you want to write for 4sysops? We are looking for new authors.
You have done a fantastic job of explaining the topic. Keep up the good work!