Objective
It is well known that bridge mode is docker default network setting, which assigns network Namespace to each container, sets IP, and connects the Docker container on one host to a virtual network bridge. Let's look at the way Docker specifies the container IP for Network bridge mode.
Implementation methods
If you simply create a bridge-mode network, you cannot assign IP to the container
[Root@vultrvpn conf.d]# Docker Network create--driver Bridge wordpress_net
Ad1ff3d972e8bfe6d992a8403a98f46784aa75d5514adbf35ee6ae4528513be7
[root@vultrvpn conf.d]# docker Network ls
Network ID NAME DRIVER SCOPE
38fab5c74b87 bridge
f0cf94dd0c5e Host host Local
465a31c55aa1 the none null local
ad1ff3d972e8 wordpress_net Bridge
Created a wordpress_net network, created a container on this network module, and specified IP
[Root@vultrvpn conf.d]# Docker run-d--name db_mysql-m 220m--memory-swap 220m-v/data/docker_project/db_mysql/data:/va R/lib/mysql-v/data/docker_project/db_mysql/conf:/etc/mysql/conf.d-e mysql_root_password=qwe123--network= Wordpress_net--ip=172.18.0.2 mysql:5.7
da497e56bd4b0f3434dd4f81534096574d167354dc508b657ceb390ba8e63771
docker:error response from Daemon:user specified IP address are supported only if connecting to networks with User Configured subnets.
Prompted an error, to configure IP, you need to assign a subnet to the network. So get a new network.
Docker Network create--driver bridge--subnet 172.25.0.0/16 wordpress_net [Root@vultrvpn conf.d]# Docker-network
INS Pect wordpress_net
[
{
' Name ': ' wordpress_net ', '
Id ': ' 925e64493df9b674ef9bdadaae380c8a335aa6353e48b447cb5d910df19c3a3e ","
Scope ":" Local ",
" Driver ":" Bridge ",
' EnableIPv6 ': false,
' IPAM ': {
' Driver ': ' Default ',
' options ': {},
' Config ': [
{
"Subnet": "172.25.0.0/16"
}
]
},
"Internal": false,
"containers": {},
options: {},
"Labels": {}
}
]
Specify an IP for the container
[Root@vultrvpn conf.d]# Docker run-d--name my_vpn-m 100m--memory-swap 100m-e wordpress_db_host=172.25.0.2-e-WORDPRES S_db_user=root-e wordpress_db_password=qwe123-e wordpress_db_name=myvpn-p 8001:80--network=wordpress_net WORDPRESS : 4.6
096518d84419476f71647a8c70c91c3f336ef8772af50672b88a9c1fd7b3a719
[Root@vultrvpn conf.d]# Docker Ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
096518d84419 wordpress:4.6 "/ entrypoint.sh Apach "seconds ago up seconds 0.0.0.0:8001->80/tcp my_vpn 6d23ed99db20 wordpress:4.6 "/entrypoint.sh Apach" about a minute ago up about a minute 0.0.0.0:8000->80/tcp my_wordpress
ade42edf5a93 mysql:5.7 "docker-entrypoint.sh" 3 minutes ago up 3 minutes 3306/tcp DB _mysql
Normal creation
View Network card information
[Root@vultrvpn conf.d]# Docker Network Inspect wordpress_net [{' Name ': ' wordpress_net ', ' Id ': ' 925e64493df9b674ef 9bdadaae380c8a335aa6353e48b447cb5d910df19c3a3e ", Scope": "Local", "Driver": "Bridge", "EnableIPv6": false, "IPAM" : {"Driver": "Default", "Options": {}, "Config": [{"Subnet": "172.25.0.0/16"}]}, "Inter NAL ": false," containers ": {" 096518d84419476f71647a8c70c91c3f336ef8772af50672b88a9c1fd7b3a719 ": {" Name ":" MY_VP N "," EndpointId ":" e44636c2b3a49d4aedaad68cd6ca17aea3a9fe4f71afecb27219a6412701536a "," MacAddress ":" 02:42:ac:19:0 0:04 "," ipv4address ":" 172.25.0.4/16 "," ipv6address ":" "}," 6d23ed99db20f17d99dd5dcf6cc65f5ced8cb936917dee0 44914cf278179150c ": {" Name ":" My_wordpress "," EndpointId ":" 3c765d5953428c1d66be584eb0d34b58f6eee3f598e3a37263d54 d77e2b87263 "," MacAddress ":" 02:42:ac:19:00:03 "," ipv4address ":" 172.25.0.3/16 "," ipv6address ":" "}," a De42edf5a934b02f7bb741bd5a45a282a50d3a49e9f1866579e8f93fef22a0e ": {" Name ":" Db_mysql "," EndpointId ":" 2322bfb9da72b3b6db68d59cea97172e66b 960ca5b782bd2fa7d09f9c93f0288 "," MacAddress ":" 02:42:ac:19:00:02 "," ipv4address ":" 172.25.0.2/16 "," ipv6addres S ': ' "}}," Options ": {}," Labels ": {}}]
Summarize
The above is the entire content of this article, I hope the content of this article can help, if you have questions you can message exchange.