When using Docker and Docker Compose for containerized deployment, Docker will allocate a private network segment (usually172.17.0.0/16
). However, in some cases, this default network segment may conflict with the existing network environment, causing network connection problems. To avoid this situation, we can manually modify the default network segments of Docker and Docker Compose.
This article will introduce how to modify the default network segment of Docker and Docker Compose, and provide detailed steps and examples.
1. Modify Docker’s default network segment
Docker uses by default172.17.0.0/16
The network segment assigns an IP address to the container. To modify this default network segment, we need to edit the Docker configuration file.
1.1 Modify Docker configuration file
In most Linux distributions, Docker's configuration files are located in/etc/docker/
. If the file does not exist, you can create it manually.
-
Open or create a Docker configuration file:
sudo nano /etc/docker/
-
Add or modify in configuration file
bip
Configuration item, specify the new network segment. For example, change the default network segment to192.168.100.1/24
:{ "bip": "192.168.100.1/24" }
-
bip
Is the abbreviation of "bridge IP" and is used to specify the IP address and subnet mask of the Docker bridge network.
-
-
Save and close the file.
1.2 Restart the Docker service
After modifying the configuration file, you need to restart the Docker service for the changes to take effect:
sudo systemctl restart docker
1.3 Verify changes
After restarting Docker, you can use the following command to view the configuration of the Docker network and confirm whether the default network segment has been changed:
docker network inspect bridge
In the output you should seeSubnet
The field has been updated with the new network segment you specified.
2. Modify Docker Compose default network segment
Docker Compose uses Docker's bridge network by default (bridge
), so it will inherit Docker's default network segment. If you wish to specify a different network segment separately for the Docker Compose project, you canCustomize the network configuration in the file.
2.1 Modification
document
Let's say you have afile where you can define a custom network and specify network segments.
-
Open
document:
nano
-
Add or modify in file
networks
section, define a custom network and specify the network segment. For example:version: '3.8' services: web: image: nginx networks: - my_network networks: my_network: driver: bridge ipam: config: - subnet: 10.0.0.0/24
-
my_network
is the name of the custom network. -
subnet
Specifies the network segment of the custom network.
-
-
Save and close the file.
2.2 Start the Docker Compose project
Start the Docker Compose project with the following command:
docker-compose up -d
2.3 Verify changes
After starting the project, you can use the following command to view the configuration of the Docker Compose network and confirm whether the custom network segment has taken effect:
docker network inspect <project_name>_my_network
In the output you should seeSubnet
The field has been updated with the new network segment you specified.
3. Summary
By modifying the default network segment of Docker and Docker Compose, you can avoid conflicts with the existing network environment and ensure the normal operation of the container network. This article explains how to achieve this by modifying the Docker configuration file and customizing the Docker Compose network. Hopefully these steps will help you better manage your Docker containers' network configuration.
If you have any questions or suggestions, please leave a message in the comment area for discussion!