本文共 8319 字,大约阅读时间需要 27 分钟。
$ docker network lsNETWORK ID NAME DRIVER1a54de857c50 host host1421a175401a bridge bridge62bf0f8a1267 none null
$ docker run \--name=mysql-host \--net=host \-e MYSQL_ROOT_PASSWORD=mypassword \-v /storage/mysql-host/datadir:/var/lib/mysql \-d mysql
[machine-host]$ docker exec -it mysql-host /bin/bash[container-host]$ ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN group defaultlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:fa:f6:30 brd ff:ff:ff:ff:ff:ffinet 192.168.55.166/24 brd 192.168.55.255 scope global eth0 valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fefa:f630/64 scope link valid_lft forever preferred_lft forever3: docker0: mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:93:50:ee:c8 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft foreverinet6 fe80::42:93ff:fe50:eec8/64 scope link
[machine-host]$ netstat -tulpn | grep 3306tcp6 0 0 :::3306 :::* LISTEN 25336/mysqld
/etc/default/docker
(Debian) 或者 /etc/sysconfig/docker
(RedHat)里改变该值。如何更改参考 。 $ docker run \--name=mysql-bridge \-p 3307:3306 \-e MYSQL_ROOT_PASSWORD=mypassword \-v /storage/mysql-bridge/datadir:/var/lib/mysql \-d mysql
[machine-host]$ docker exec -it mysql-container-bridge /bin/bash[container-host]$ ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN group defaultlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever4: eth0: mtu 1500 qdisc noqueue state UP group defaultlink/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ffinet 172.17.0.2/16 scope global eth0 valid_lft forever preferred_lft foreverinet6 fe80::42:acff:fe11:2/64 scope link valid_lft forever preferred_lft forever
[machine-host]$ iptables -L -n -t natChain POSTROUTING (policy ACCEPT)target prot opt source destinationMASQUERADE all -- 172.17.0.0/16 0.0.0.0/0MASQUERADE tcp -- 172.17.0.2 172.17.0.2 tcp dpt:3306Chain DOCKER (2 references)target prot opt source destinationDNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3307 to:172.17.0.2:3306
[machine-host]$ netstat -tulpn | grep 3307tcp6 0 0 :::3307 :::* LISTEN 4150/docker-proxy
[machine-host]$ docker network create mysql-network
[machine-host]$ for i in {1..5}; do docker run --name=mysql$i --net=mysql-network -e MYSQL_ROOT_PASSWORD=mypassword -d mysql; done
[machine-host]$ docker exec -it mysql3 /bin/bash
[mysql3-container]$ for i in {1..5}; do ping -c 1 mysql$i ; donePING mysql1 (172.18.0.2): 56 data bytes64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.151 ms--- mysql1 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max/stddev = 0.151/0.151/0.151/0.000 msPING mysql2 (172.18.0.3): 56 data bytes64 bytes from 172.18.0.3: icmp_seq=0 ttl=64 time=0.138 ms--- mysql2 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max/stddev = 0.138/0.138/0.138/0.000 msPING mysql3 (172.18.0.4): 56 data bytes64 bytes from 172.18.0.4: icmp_seq=0 ttl=64 time=0.087 ms--- mysql3 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max/stddev = 0.087/0.087/0.087/0.000 msPING mysql4 (172.18.0.5): 56 data bytes64 bytes from 172.18.0.5: icmp_seq=0 ttl=64 time=0.353 ms--- mysql4 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max/stddev = 0.353/0.353/0.353/0.000 msPING mysql5 (172.18.0.6): 56 data bytes64 bytes from 172.18.0.6: icmp_seq=0 ttl=64 time=0.135 ms--- mysql5 ping statistics ---1 packets transmitted, 1 packets received, 0% packet lossround-trip min/avg/max/stddev = 0.135/0.135/0.135/0.000 ms
[mysql3-container]$ cat /etc/resolv.confsearch localdomainnameserver 127.0.0.11options ndots:0
[machine-host]$ docker run --name=mysql0 --net=none -e MYSQL_ROOT_PASSWORD=mypassword -d mysql
[machine-host]$ docker exec -it mysql0 /bin/bash[mysql0-container]$ ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN group defaultlink/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
[mysql0-container]$ mysql -uroot -pmypassword -h127.0.0.1 -P3306mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 6Server version: 5.7.13 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
崔婧雯,现就职于IBM,高级软件工程师,负责IBM WebSphere业务流程管理软件的系统测试工作。曾就职于VMware从事桌面虚拟化产品的质量保证工作。对虚拟化,中间件技术,业务流程管理有浓厚的兴趣。
原文发布时间为:2016-08-05
本文作者:崔婧雯
本文来自云栖社区合作伙伴Dockerone.io,了解相关信息可以关注Dockerone.io。
原文标题:Docker上的MySQL:MySQL容器的单主机网络
转载地址:http://asdeo.baihongyu.com/