初始化情况:(后头会返回到这个情况状态)
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES我们实验通过docker网桥方法(桥接)给一个容器配置一个ip地点(自界说的),下面看一下呼吁执行的功效:
[root@localhost ~]# docker run -itd --name test --network bridge --ip 172.17.0.10 CentOS:7 /bin/bash Unable to find image 'centos:7' locally 7: Pulling from library/centos ab5ef0e58194: Pull complete Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c Status: Downloaded newer image for centos:7 29f9e9cbb398085d7c89bed2982d626bfb7e564371a76a6cb693cffa68b917af docker: Error response from daemon: user specified IP address is supported on user defined networks only.首先,由于没有改镜像,所以会先拉取镜像之后建设以及实验运行容器,可是会碰着报错,报错原因是因为用户本身配置的ip地点只适合于他们本身界说的网络。可是这并不影响镜像的获取与建设,我们实验运行看当作果:
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 5e35e350aded 4 months ago 203MB [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 29f9e9cbb398 centos:7 "/bin/bash" About a minute ago Created test [root@localhost ~]# docker start 29f9e9cbb398 Error response from daemon: user specified IP address is supported on user defined networks only Error: failed to start containers: 29f9e9cbb398 很显然,功效汇报我们照旧上述的原因导致了这个问题,好吧只能删除这个容器了。索性还原为初始化情况吧(其实可以去掉--network bridge就不会报错,可是配置的ip地点会不生效,可以自行实验一下),我们不指定ip地点来试一下,顺便验证一下不加这个参数试试,最后还原为初始化状态后我们再通过自界说的方法来配置ip地点吧。
[root@localhost ~]# docker run -itd --name demo centos:7 /bin/bash 984545df8ebf2c30f4ccf3f5f4699ed67bd2a93cbc068f2ace4865a828b4a4d1 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 984545df8ebf centos:7 "/bin/bash" 8 seconds ago Up 6 seconds demo进入容器查察ip地点
[root@localhost ~]# docker exec -it 984545df8ebf /bin/bash [root@984545df8ebf /]# yum install -y net-tools Loaded plugins: fastestmirror, ovl Determining fastest mirrors [root@984545df8ebf /]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 2201 bytes 15042254 (14.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1999 bytes 111171 (108.5 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那么首先我们需要建设一个自界说的子网段,而且给他一个名称,通过这个网段给对应的容器配置牢靠的ip地点。
[root@localhost ~]# docker network create --subnet=172.18.0.0/24 mynetwork Error response from daemon: Failed to Setup IP tables: Unable to enable SKIP DNAT rule: (iptables failed: iptables --wait -t nat -I DOCKER -i br-4cd28c051bec -j RETURN: iptables: No chain/target/match by that name. (exit status 1))看来很不幸啊,又error了,不要慌,我们看看是什么原因导致的,这个报错的来源看似是防火墙,说是无法跳过DNAT(方针网络地点转换)法则,其实导致这个问题的原因是我们方才开启了防火墙,却没有重启docker处事,就有点雷同变动了设置文件没有重启处事。
[root@localhost ~]# systemctl restart docker [root@localhost ~]# docker network create --subnet=172.18.0.0/24 mynetwork 723d0fd514eb219d57667f72c3eb75fc4864af0cd94c21b6c70e868fb8d520a1 [root@localhost ~]# docker network ls NETWORK ID NAME DRIVER SCOPE 00008a1e778b bridge bridge local 3bb2f50eb211 host host local 723d0fd514eb mynetwork bridge local e12cd7b98c54 none null local [root@localhost ~]#此时举办验证自界说配置一个容器的牢靠IP地点的测试:
[root@localhost ~]# docker run -itd --name test2 --network mynetwork --ip 172.18.0.10 centos:7 /bin/bash Unable to find image 'centos:7' locally 7: Pulling from library/centos ab5ef0e58194: Pull complete Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c Status: Downloaded newer image for centos:7 683ad76f7789b5c16f251577047a288a3a4a00777128a216adfee15985b94c04