Docker容器互聯
阿新 • • 發佈:2019-01-02
在同一個宿主機、同一網段下,容器間ping IP可通。
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
c7ffadc83291 centos_with_httpd:genesis "bash" 4 hours ago Up 4 hours
7169e8be6d3e centos "/bin/bash" 6 hours ago Up 6 hours
4cd696928bbe centos "bash" 6 hours ago Up 6 hours
4f5bf6f33f2c centos "bash" 6 hours ago Up 6 hours
0a80861145c9 centos "bash" 6 hours ago Up 6 hours
fb45150dbc21 centos "bash" 6 hours ago Up 6 hours
3222c7c5c456 centos "bash" 7 hours ago Up 7 hours
e136b27a8e17 centos "bash" 7 hours ago Up 7 hours
[root@localhost ~]# docker exec -it c7ffa bash
[root@c7ffadc83291 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.9 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:9 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:09 txqueuelen 0 (Ethernet)
RX packets 26 bytes 2103 (2.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14 bytes 1255 (1.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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 20 bytes 7036 (6.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20 bytes 7036 (6.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@c7ffadc83291 /]# exit
exit
[root@localhost ~]# docker exec -it 7169 bash
[root@7169e8be6d3e /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.8 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:8 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:08 txqueuelen 0 (Ethernet)
RX packets 10103 bytes 41156618 (39.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8762 bytes 562975 (549.7 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
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (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@7169e8be6d3e /]# ping 172.17.0.9
PING 172.17.0.9 (172.17.0.9) 56(84) bytes of data.
64 bytes from 172.17.0.9: icmp_seq=1 ttl=64 time=1.58 ms
64 bytes from 172.17.0.9: icmp_seq=2 ttl=64 time=0.058 ms
^C
--- 172.17.0.9 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.058/0.820/1.583/0.763 ms
[root@7169e8be6d3e /]# curl 172.17.0.9/1.html
ginesis2011.github.io
使用mysql映象
下載一個mysql映象
[root@localhost ~]# docker pull mysql
新建一個容器
[root@localhost ~]# docker run -itd -p 13306:3306 mysql bash
4611d806d8d01cb5a8c2976d8bfbf511a9cc23a7307c723ecfbdd2ebcb25b8ef
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4611d806d8d0 mysql "docker-entrypoint.s 54 seconds ago Up 52 seconds 0.0.0.0:13306->3306/tcp condescending_nobel
再新建一個web容器並和前一個容器互聯
[[email protected] ~]# docker run -itd -p 12308:80 --name web --link condescending_nobel:db centos_with_httpd:genesis bash
3d3248766159a31546093d42912a748fc616808910223a36a20c55b3a15ea1cc
- 使用
--name
標記可以為容器自定義命名 - 在執行
docker run
時,如果新增--rm
標記,則容器在終止後會立刻刪除。注意,--rm
和-d
引數不能同時使用。 - 使用
--link
引數可以讓容器之間安全的進行互動 --link
引數的格式為--link name:alias
,其中name是要連線容器的名稱,alias是這個連線eider別名。
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d3248766159 centos_with_httpd:genesis "bash" 9 minutes ago Up 9 minutes 0.0.0.0:12308->80/tcp web
4611d806d8d0 mysql "docker-entrypoint.s 12 minutes ago Up 12 minutes 0.0.0.0:13306->3306/tcp condescending_nobel
Docker在兩個互聯的容器之間建立了一個安全隧道,而且不用對映它們的埠到宿主主機上,避免暴露資料庫埠到外部網路上。
在web容器中用ping命令測試跟db容器的連通
[[email protected]ocalhost ~]# docker exec -it web bash
[[email protected]3d3248766159 /]# ping db
PING db (172.17.0.2) 56(84) bytes of data.
64 bytes from db (172.17.0.2): icmp_seq=1 ttl=64 time=1.11 ms
64 bytes from db (172.17.0.2): icmp_seq=2 ttl=64 time=0.090 ms
64 bytes from db (172.17.0.2): icmp_seq=3 ttl=64 time=0.048 ms
^C
--- db ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.048/0.416/1.110/0.491 ms
Docker通過兩種方式為容器公開連線資訊:
- 環境變數
- 更新/etc/hosts檔案
[root@3d3248766159 /]# cat /etc/hosts
172.17.0.3 3d3248766159
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 db 4611d806d8d0 condescending_nobel
這裡有兩個hosts資訊,第一個是web容器,web容器用自己的id作為預設主機名;第二個是db容器的IP和主機名。
[[email protected] /]# telnet db 3306
bash: telnet: command not found
[[email protected] /]# yum install -y telnet
[[email protected] /]# telnet db 3306
Trying 172.17.0.2...
telnet: connect to address 172.17.0.2: Connection refused
[[email protected] /]# exit
exit
[[email protected] ~]# docker exec -it condescending_nobel bash
[email protected]:/# /etc/init.d/mysql start
[email protected]:/# exit
exit
[[email protected] ~]# docker exec -it web bash
[[email protected] /]# telnet db 3306
Trying 172.17.0.2...
Connected to db.
Escape character is '^]'.
CHost '172.17.0.3' is not allowed to connect to this MySQL serverConnection closed by foreign host.
使用env命令檢視web容器的環境變數
[[email protected] /]# env
HOSTNAME=3d3248766159
DB_NAME=/web/db
TERM=xterm
DB_PORT=tcp://172.17.0.2:3306
DB_PORT_3306_TCP_PORT=3306
DB_ENV_GOSU_VERSION=1.7
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
DB_PORT_3306_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
DB_PORT_3306_TCP_ADDR=172.17.0.2
SHLVL=1
HOME=/root
DB_PORT_3306_TCP=tcp://172.17.0.2:3306
DB_ENV_MYSQL_VERSION=5.7.16-1debian8
DB_ENV_MYSQL_MAJOR=5.7
_=/usr/bin/env
使用centos 6映象
使用OpenVZ提供的模板(centos-6-x86-minimal.tar.gz)建立的映象,報錯仍未解決
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos_with_httpd genesis 50611dba3a22 About an hour ago 335.3 MB
registry latest 5c929a8b587a 34 hours ago 33.27 MB
genesis_centos latest 85bc3a58f134 5 days ago 277.6 MB
192.168.1.179:5000/busybox latest 9967c5ad88de 12 days ago 1.093 MB
busybox latest 9967c5ad88de 12 days ago 1.093 MB
centos-6-x86 latest 8fca9486a39b 2 weeks ago 341.3 MB
centos_with_net latest 3e8ea8607f08 4 weeks ago 294.9 MB
centos latest 9baab0af79c4 6 weeks ago 196.7 MB
[root@localhost ~]# docker run -itd centos-6-x86 bash
06e48529b37a743ee6a03102b37f37f5724749324115bd034085d3a70f9f7132
[root@localhost ~]# docker exec -it 06e4 bash
[root@06e48529b37a /]# yum install -y mysql-server
操作到這裡出錯,centos-6-x86是使用OpenVZ提供的模板(centos-6-x86-minimal.tar.gz)建立的映象。
使用OpenVZ提供的模板(centos-6-x86_64.tar.gz)建立的映象
重新在openvz官網下載另一個centos6模板(centos-6-x86_64.tar.gz)做成映象
[[email protected] ~]# wget http://download.openvz.org/template/precreated/centos-6-x86_64.tar.gz
2016-10-21 20:56:01 (18.9 KB/s) - 已儲存 “centos-6-x86_64.tar.gz” [230555815/230555815])
[[email protected] ~]# ls
anaconda-ks.cfg centos-6-x86-minimal.tar.gz genesis_centos.tar install.log.syslog 模板 圖片 下載 桌面
centos-6-x86_64.tar.gz centos_with_net.tar install.log 公共的 視訊 文件 音樂
[[email protected] ~]# du -sh centos-6-x86_64.tar.gz
220M centos-6-x86_64.tar.gz
[[email protected] ~]# cat centos-6-x86_64.tar.gz |docker import - centos-6-x86_64
78555e13daef8eb0d8f34cb39979ced687cd80ccb6491c5262795231f9410d3c
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos-6-x86_64 latest 78555e13daef 2 minutes ago 615.8 MB
centos_with_httpd genesis 50611dba3a22 28 hours ago 335.3 MB
registry latest 5c929a8b587a 2 days ago 33.27 MB
mysql latest 5536fdb1bd72 6 days ago 383.4 MB
genesis_centos latest 85bc3a58f134 7 days ago 277.6 MB
192.168.1.179:5000/busybox latest 9967c5ad88de 13 days ago 1.093 MB
busybox latest 9967c5ad88de 13 days ago 1.093 MB
centos-6-x86 latest 8fca9486a39b 2 weeks ago 341.3 MB
centos_with_net latest 3e8ea8607f08 5 weeks ago 294.9 MB
centos latest 9baab0af79c4 6 weeks ago 196.7 MB
[[email protected] ~]# docker run -itd centos-6-x86_64 bash
4dfbe855582616a2dbfb51275be7ab66dcd6afb04774cf503c2783952a7176ed
[[email protected] ~]# docker exec -it 4df bash
[root@4dfbe8555826 /]# yum list |grep mysql
apr-util-mysql.x86_64 1.3.9-3.el6_0.1 base
bacula-director-mysql.x86_64 5.0.0-13.el6 base
bacula-storage-mysql.x86_64 5.0.0-13.el6 base
dovecot-mysql.x86_64 1:2.0.9-22.el6 base
freeradius-mysql.x86_64 2.2.6-6.el6_7 base
libdbi-dbd-mysql.x86_64 0.8.3-5.1.el6 base
mod_auth_mysql.x86_64 1:3.0.0-11.el6_0.1 base
mysql.x86_64 5.1.73-7.el6 base
mysql-bench.x86_64 5.1.73-7.el6 base
mysql-connector-java.noarch 1:5.1.17-6.el6 base
mysql-connector-odbc.x86_64 5.1.5r1144-7.el6 base
mysql-devel.i686 5.1.73-7.el6 base
mysql-devel.x86_64 5.1.73-7.el6 base
mysql-embedded.i686 5.1.73-7.el6 base
mysql-embedded.x86_64 5.1.73-7.el6 base
mysql-embedded-devel.i686 5.1.73-7.el6 base
mysql-embedded-devel.x86_64 5.1.73-7.el6 base
mysql-libs.i686 5.1.73-7.el6 base
mysql-libs.x86_64 5.1.73-7.el6 base
mysql-server.x86_64 5.1.73-7.el6 base
mysql-test.x86_64 5.1.73-7.el6 base
pcp-pmda-mysql.x86_64 3.10.9-6.el6 base
php-mysql.x86_64 5.3.3-48.el6_8 updates
qt-mysql.i686 1:4.6.2-28.el6_5 base
qt-mysql.x86_64 1:4.6.2-28.el6_5 base
rsyslog-mysql.x86_64 5.8.10-10.el6_6 base
rsyslog7-mysql.x86_64 7.4.10-5.el6 base
[root@4dfbe8555826 /]# yum install -y mysql-server
[root@4dfbe8555826 /]# /etc/init.d/mysqld start
[root@4dfbe8555826 /]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN -
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 59005 - /var/lib/mysql/mysql.sock
[root@4dfbe8555826 /]# exit
exit
[[email protected] ~]# docker commit -m "centos_6_with_mysql" 4df centos6_with_mysql
821a69bf88525484ad718468adc65b84719d9b8d952c4da7c41d913a767056e1
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos6_with_mysql latest 821a69bf8852 26 seconds ago 724.8 MB
centos-6-x86_64 latest 78555e13daef 57 minutes ago 615.8 MB
centos_with_httpd genesis 50611dba3a22 29 hours ago 335.3 MB
registry latest 5c929a8b587a 2 days ago 33.27 MB
mysql latest 5536fdb1bd72 6 days ago 383.4 MB
genesis_centos latest 85bc3a58f134 7 days ago 277.6 MB
192.168.1.179:5000/busybox latest 9967c5ad88de 13 days ago 1.093 MB
busybox latest 9967c5ad88de 13 days ago 1.093 MB
centos-6-x86 latest 8fca9486a39b 2 weeks ago 341.3 MB
centos_with_net latest 3e8ea8607f08 5 weeks ago 294.9 MB
centos latest 9baab0af79c4 6 weeks ago 196.7 MB
為了接著用13306、12308埠,先停掉之前用於容器互聯的兩個容器
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4dfbe8555826 centos-6-x86_64 "bash" 16 minutes ago Up 16 minutes sleepy_pare
3d3248766159 centos_with_httpd:genesis "bash" 4 hours ago Up 4 hours 0.0.0.0:12308->80/tcp web
4611d806d8d0 mysql "docker-entrypoint.s 4 hours ago Up 4 hours 0.0.0.0:13306->3306/tcp condescending_nobel
[[email protected] ~]# docker stop 3d3
3d3
[[email protected] ~]# docker stop 461
461
[[email protected] ~]# docker run -itd -p 13306:3306 --name db centos6_with_mysql bash
18010636c53584fd1f300a459c1bd03bbb2cbc4111acd72a48c8b65655d334c9
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18010636c535 centos6_with_mysql "bash" 2 seconds ago Up 2 seconds 0.0.0.0:13306->3306/tcp db
4dfbe8555826 centos-6-x86_64 "bash" 21 minutes ago Up 21 minutes sleepy_ pare
[[email protected] ~]# docker run -itd -p 18080:80 --name web --link db:db centos_with_httpd:genesis bash
1dcac96e9649c0e96797a01d17cce7cd7952987570bd3a822812ee1c3f8e78e5
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1dcac96e9649 centos_with_httpd:genesis "bash" 5 seconds ago Up 4 seconds 0.0.0.0:18080->80/tcp web
18010636c535 centos6_with_mysql "bash" 3 minutes ago Up 3 minutes 0.0.0.0:13306->3306/tcp db
4dfbe8555826 centos-6-x86_64 "bash" 24 minutes ago Up 24 minutes sleepy_pare
[[email protected] ~]# docker exec -it web bash
[[email protected]1dcac96e9649 /]# ping db
PING db (172.17.0.7) 56(84) bytes of data.
64 bytes from db (172.17.0.7): icmp_seq=1 ttl=64 time=0.628 ms
64 bytes from db (172.17.0.7): icmp_seq=2 ttl=64 time=0.112 ms
^C
--- db ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.112/0.370/0.628/0.258 ms
[[email protected]1dcac96e9649 /]# cat /etc/hosts
172.17.0.8 1dcac96e9649
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.7 db 18010636c535
[[email protected]1dcac96e9649 /]# telnet db 3306
bash: telnet: command not found
[[email protected]1dcac96e9649 /]# yum install -y telnet
[[email protected]1dcac96e9649 /]# telnet db 3306
Trying 172.17.0.7...
telnet: connect to address 172.17.0.7: Connection refused
[[email protected]1dcac96e9649 /]# exit
exit
[[email protected] ~]# docker exec -it db bash
[[email protected]18010636c535 /]# /etc/init.d/mysqld start
Starting mysqld: [ OK ]
[[email protected]18010636c535 /]# exit
[[email protected] ~]# docker exec -it web bash
[[email protected]1dcac96e9649 /]# telnet db 3306
Trying 172.17.0.7...
Connected to db.
Escape character is '^]'.
CHost '172.17.0.8' is not allowed to connect to this MySQL serverConnection closed by foreign host.
[[email protected]1dcac96e9649 /]# env
HOSTNAME=1dcac96e9649
DB_NAME=/web/db
TERM=xterm
DB_PORT=tcp://172.17.0.7:3306
DB_PORT_3306_TCP_PORT=3306
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;