This article has been published by the author yuan Huan to authorize the Netease cloud community.
Welcome to the Netease cloud community to learn more about the operation experience of Netease technology products.
Create a docker container
Docker run-it -- name = YH-h yh -- Net = none Debian: sshd bash ### ensure that the -- Net = none parameter is used, and no Nic is created in the newly created container.
Docker PS
Log on to the container and check the IP address. The eth0 Nic is not found:
[Email protected]:/# ifconfig- Lo link encap: local loopback Inet ADDR: 127.0.0.1 mask: 255.0.0.0 Inet6 ADDR: 1/128 scope: Host Up loopback running MTU: 65536 Metric: 1 RX packets: 0 errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 0 errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 txqueuelen: 0 RX Bytes: 0 (0.0 B) TX Bytes: 0 (0.0 B) |
Centos6.6 upgrade iproute
The host machine is centos6.6. To support the IP netns command, you need to upgrade it:
Rpm-IVH http://www.elrepo.org/elrepo-release-6-6.el6.elrepo.noarch.rpm
Yum -- enablerepo = elrepo-kernel install kernel-lt-y ### upgrade the kernel
VI/etc/grub. conf ### modify default = 0. The new kernel is started by default.
Reboot ### restart to make the new kernel take effect
Uname-r ### check whether the kernel version is new
Yum install-y http://rdo.fedorapeople.org/rdo-release.rpm ### update rdo Repository
Vim/etc/yum. Repos. d/rdo-release.repo ### modify the file content as follows
[Openstack-Juno] Name = openstack Juno Repository Base url = http://repos.fedorapeople.org/repos/openstack/openstack-icehouse/epel-6/ Enabled = 1 Skip_if_unavailable = 0 Gpgcheck = 0 Gpgkey = file: // etc/pki/rpm-GPG-key-rdo-Juno |
Yum -- enablerepo = openstack-Juno install iproute ### update iproute
Rpm-Q iproute
Set static IP
Create a script modify_docker_ip.sh to modify the static IP address. The content is as follows:
#/Bin/baslif [-Z $1] | [-Z $2] | [-Z $3] | [-Z $4] | [-z $5]; thenecho "Usage: $0 containerid/container_name IP mask gateway ethname" Echo "Call the script like: Sh unzip b0e18b6a4432 192.168.5.123 24 192.168.5.1 deth0" Echo "Call the script like: sh manual_con_static_ip.sh my_container 192.168.5.123 24 192.168.5.1 deth0 "exitfi containerid_name = $1 setip = $2 setmask = $3 Gateway = $4 ethname = $5 # determine whether the host machine Nic has ifconfig $ ethname>/dev/null 2> & 1if [$? -EQ 0]; then read-P "$ ethname exist, do you want delelte it? Y/N "del if [[$ del = 'y']; then IP link del $ ethname else exit Fifi # pid = 'docker inspect-F '{{. state. PID} '$ containerid_name' echo pid = $ pidmkdir-P/var/run/netnsfind-L/var/run/netns-type L-Delete if [-F/var/ run/netns/$ pid]; then Rm-F/var/run/netns/$ pidfiln-S/proc/$ PID/ns/NET/var/run/netns/$ pid # IP link add $ ethname type veth peer name bbrctl addif docker0 $ ethnameip link set $ ethname upip link set B netns $ pid # Delete the existing eth0ip netns exec $ PID IP link del eth0>/dev/ null 2> & 1 # set the new container Nic eth0ip netns exec $ pid ip link set Dev B Name eth0ip netns exec $ PID IP link set eth0 upip netns exec $ PID IP ADDR add $ setip/$ setmask Dev eth0ip netns exec $ pid ip Route add default via $ Gateway
Run the following command on the host machine to create a NIC for the container and assign a static IP Address:
./Modify_docker_ip.sh 8feff00a0a26 172.17.0.2 16 172.17.42.1 deth0
Where: 8feff00a0a26 is the container ID, 172.17.0.2 is the static IP address of the container, 16 is the mask, 172.17.42.1 is the container gateway address (that is, the IP address of docker0 in the operating container system ), deth0 is the name of the newly created host network card (corresponding to the eth0 in the container)
View the Host IP Address:
[[Email protected] ~] # Ifconfig Deth0 link encap: Ethernet hwaddr da: 19: 96: 9B: 1b: E5 Inet6 ADDR: fe80: d819: 96ff: fe9b: 1be5/64 scope: Link Up broadcast running Multicast MTU: 1500 Metric: 1 RX packets: 6 errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 6 errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 FIG: 1000 RX Bytes: 468 (468.0 B) TX Bytes: 468 (468.0 B) Docker0 link encap: Ethernet hwaddr 56: 84: 7A: Fe: 97: 99 Inet ADDR: 172.17.42.1 bcast: 0.0.0.0 mask: 255.255.255.0.0 Inet6 ADDR: fe80: 5484: 7aff: Fefe: 9799/64 scope: Link Up broadcast running Multicast MTU: 1500 Metric: 1 RX packets: 12 errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 6 errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 txqueuelen: 0 RX Bytes: 768 (768.0 B) TX Bytes: 468 (468.0 B) |
View the IP address in the container:
[Email protected]:/# ifconfig- Eth0 link encap: Ethernet hwaddr 22: E1: 72: 17: B6: dd Inet ADDR: 172.17.0.2 bcast: 0.0.0.0 mask: 255.255.255.0.0 Inet6 ADDR: fe80: 20e1: 72ff: fe17: b6dd/64 scope: Link Up broadcast running Multicast MTU: 1500 Metric: 1 RX packets: 3 errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 3 errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 FIG: 1000 RX Bytes: 238 (238.0 B) TX Bytes: 238 (238.0 B) Lo link encap: local loopback Inet ADDR: 127.0.0.1 mask: 255.0.0.0 Inet6 ADDR: 1/128 scope: Host Up loopback running MTU: 65536 Metric: 1 RX packets: 0 errors: 0 dropped: 0 overruns: 0 frame: 0 TX packets: 0 errors: 0 dropped: 0 overruns: 0 carrier: 0 Collisions: 0 txqueuelen: 0 RX Bytes: 0 (0.0 B) TX Bytes: 0 (0.0 B) |
Appendix:
Docker for the network of this document see: https://docs.docker.com/articles/networking/
Another tool, pipework, can also set static IP: https://github.com/jpetazzo/pipework
Legacy problems:
Problem: After the docker container is restarted, eth0 disappears and the IP address becomes invalid.
Description: docker automatically clears the NIC configuration when the container is stopped. After the restart, the eth0 in the container disappears and the static IP address becomes invalid.
Solution: 1. Run a docker container and re-execute the script or pipework in the text to reset the IP address. 2. There may be a better way to study.
Netease cloud Container Service provides users with serverless containers, allowing enterprises to quickly deploy services and easily maintain services. Container Service supports auto scaling, vertical resizing, phased upgrade, service discovery, service orchestration, error recovery, and performance monitoring.
Free trial of cloud security (yundun) content security, verification code and other services
For more information about Netease technologies, products, and operations, click.
Related Articles:
[Recommendation] windows extended screen Development Summary
[Recommendation] online code compiler (I)-editing and compilation
[Recommended] springboot entry (2) -- start dependency
Set static IP addresses for docker containers