Docker network basic operation

Docker network basic operation

Set the IP address of the docker container

  • Earlier we learned that when the docker installation is completed, three networks will be automatically created, bridge (the network to which the container is connected by default), none, host
    • host: The container will not virtualize its own network card, configure its own IP address, etc., but use the IP and port of the host machine
    • container: The created container will not create its own network card and configure its own IP, but will share the IP and port range with a specified container
    • none: This mode turns off the network function of the container
    • bridge: This mode will assign and set IP for each container, and connect the container to a docker0 virtual bridge, and communicate with the host through the docker0 bridge and iptables nat table configuration
  • After we install docker, we check the IP address of docker0
[root@localhost mysql]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:7ff:fe9e:a0c9  prefixlen 64  scopeid 0x20<link>
        ether 02:42:07:9e:a0:c9  txqueuelen 0  (Ethernet)
        RX packets 45211  bytes 1843153 (1.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64943  bytes 229872092 (219.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.73.11  netmask 255.255.255.0  broadcast 192.168.73.255
        inet6 fe80::62c1:41da:f11f:5003  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:22:4d:74  txqueuelen 1000  (Ethernet)
        RX packets 213025  bytes 288850783 (275.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 72995  bytes 6980153 (6.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 88  bytes 9732 (9.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 88  bytes 9732 (9.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth660d973: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::a41a:89ff:fe4a:908e  prefixlen 64  scopeid 0x20<link>
        ether a6:1a:89:4a:90:8e  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11  bytes 891 (891.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:74:9d:6e  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • Create a new container and configure him with the IP address of the same network segment as docker0
[root@localhost ~]# docker run -itd --name test1 --network bridge --ip 172.17.0.100 centos:7 /bin/bash
98b29e7191b76909d6ccd95b29d4b17d91decee7e8da74320e7ebfb8906485fc
docker: Error response from daemon: user specified IP address is supported on user defined networks only.
#检查镜像
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               new                 b43d6632e783        9 hours ago         10.1GB
centos              7                   5e35e350aded        5 months ago        203MB
#检查容器
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
98b29e7191b7        centos:7            "/bin/bash"         2 minutes ago       Created                                       test1
86f87f283a50        mysql:new           "init"              6 hours ago         Up 6 hours          0.0.0.0:32776->3306/tcp   mysql_server
#由上面可知,容器的创建并没有问题,接下来尝试启动容器
[root@localhost ~]# docker start 98b29e7191b7
Error response from daemon: user specified IP address is supported on user defined networks only
Error: failed to start containers: 98b29e7191b7
#容器并没有启动成功,所以不能这样指定bridge模式的固定的IP地址
  • Next, customize the network fixed IP
[root@localhost ~]# docker rm 98b29e7191b7
98b29e7191b7
#删除刚刚的容器
[root@localhost ~]# docker run -itd --name test1 --network bridge centos:7 /bin/bash
6bf95817ff0d0a43fa4d1bf97df32101347d364af25161025dd78dff9b536fda
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
6bf95817ff0d        centos:7            "/bin/bash"         6 seconds ago       Up 4 seconds                                  test1
86f87f283a50        mysql:new           "init"              6 hours ago         Up 6 hours          0.0.0.0:32776->3306/tcp   mysql_server
[root@localhost ~]# docker exec -it 6bf95817ff0d /bin/bash
[root@6bf95817ff0d /]# yum -y install net-tools
[root@6bf95817ff0d /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 5349  bytes 15213111 (14.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3978  bytes 219357 (214.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@6bf95817ff0d /]# ping 172.17.0.1
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.338 ms
64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.112 ms
64 bytes from 172.17.0.1: icmp_seq=3 ttl=64 time=0.119 ms
^C
--- 172.17.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.112/0.189/0.338/0.106 ms
#尝试不指定容器的固定IP,并发现容器的地址为172.17.0.3
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
9ac53bd24af2        bridge              bridge              local
25ef346a7df8        host                host                local
9de3e1ea939c        none                null                local
[root@localhost ~]# docker network create --subnet=172.18.0.0/16 mynetwork
aac45434a763e5c4588ce05aee36f628ecfe2b0cb9185270023c93e5c0962b4c
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
9ac53bd24af2        bridge              bridge              local
25ef346a7df8        host                host                local
aac45434a763        mynetwork           bridge              local
9de3e1ea939c        none                null                local
[root@localhost ~]# docker run -itd --name test2 --net mynetwork --ip=172.18.0.10 centos:7 /bin/bash
5f867fcc8d55ab4b9e7776dab43fac499ede9ac97145ef347eaef19d12f43557
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                     NAMES
5f867fcc8d55        centos:7            "/bin/bash"         7 seconds ago       Up 6 seconds                                  test2
86f87f283a50        mysql:new           "init"              7 hours ago         Up 7 hours          0.0.0.0:32776->3306/tcp   mysql_server
[root@localhost ~]# docker exec -it 5f867fcc8d55 /bin/bash
[root@5f867fcc8d55 /]# yum -y install net-tools
[root@5f867fcc8d55 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.10  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:0a  txqueuelen 0  (Ethernet)
        RX packets 5444  bytes 15219394 (14.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4014  bytes 221367 (216.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 63  bytes 5759 (5.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 63  bytes 5759 (5.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
#可以看到,容器的IP地址就是自定义的地址。
Published 140 original articles · 49 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/double_happy111/article/details/105667978