端口映射
容器运行时如果没有指定端口,与外界是无法通信的,比如当前我们的有一台MySQL的docker container,当前容器正在运行mysql并提供3306端口
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2e54153e56ca mysql:version1 "/bin/bash" 8 days ago Up About a minute mysql-new-container # docker exec -it mysql-new-container /bin/bash -c "netstat -antlp |grep 3306" tcp6 0 0 :::3306 :::* LISTEN -
此时我们并未做端口映射,所以宿主机和外界是无法访问该MySQL服务的
将容器关机,重开机时进行端口映射
先将容器内的Mysql服务停止
# docker exec -u mysqladmin -it mysql-new-container /bin/bash -c "/usr/local/mysql/bin/mysqladmin -uroot -pmypna123 shutdown"
停止容器
# docker stop mysql-new-container
mysql-new-container
根据容器创建一个image镜像
# docker commit mysql-new-container mysql-new-container-temp sha256:2f516f00086e5bd6f52bb90b75f03a763eaff08f62a93b6cd73d1eb0226571fe # docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql-new-container-temp latest 2f516f00086e 4 seconds ago 333MB mysql version1 bf6b23f800ed 2 weeks ago 333MB centos latest 0f3e07c0138f 5 weeks ago 220MB
删除容器
# docker rm mysql-new-container mysql-new-container
根据镜像新建容器并启动会mysql
# docker run --name mysql-new-container --hostname mysql-new-container --mount type=volume,source=mysql-new-vol,target=/usr/local/mysql -p 3306:3306 -it mysql-new-container-temp /bin/bash # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bdc71602ee74 mysql-new-container-temp "/bin/bash" 4 minutes ago Up 4 minutes 0.0.0.0:3306->3306/tcp mysql-new-container # docker exec -u mysqladmin -it mysql-new-container /bin/bash $ cd /usr/local/mysql/bin $ mysqld_safe &
查看该容器的端口映射信息
# docker port mysql-new-container 3306/tcp -> 0.0.0.0:3306
测试端口映射,访问宿主机的3306端口
mysql -h 172.16.101.55 -u root -pmypna123 -P 3306 mysql -e "status" mysql: [Warning] Using a password on the command line interface can be insecure. -------------- mysql Ver 14.14 Distrib 5.7.18, for linux-glibc2.5 (x86_64) using EditLine wrapper Connection id: 331 Current database: mysql Current user: root@sht-sgmhadoopnn-01.telenav.cn SSL: Cipher in use is DHE-RSA-AES256-SHA Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.18-log MySQL Community Server (GPL) Protocol version: 10 Connection: 172.16.101.55 via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 TCP port: 3306 Uptime: 7 min 25 sec Threads: 2 Questions: 28 Slow queries: 0 Opens: 117 Flush tables: 1 Open tables: 71 Queries per second avg: 0.062 --------------
容器互联
容器之间默认无法正常进行通信,我们新建一个web容器,在web容器里可以访问到另外一个容器mysql-new-container
# docker run --name web --hostname web --link mysql-new-container:db -it centos:latest /bin/bash # docker ps -a --no-trunc CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1bb29181e0cd5df1d34c94313e9920093bcf705626750da2d43f8067dd332281 centos:latest "/bin/bash" 6 minutes ago Up 6 minutes web bdc71602ee74907c8902c878b805cc28b57726b80350395fd4cd9cf69a86795d mysql-new-container-temp "/bin/bash" 31 minutes ago Up 31 minutes 0.0.0.0:3306->3306/tcp mysql-new-container,web/db
通过使用--link选项可以实现该功能,通过登录web容器查看信息
[root@web /]# cat /etc/hosts 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 mysql-new-container mysql-new-container 172.17.0.3 web [root@web /]# env DB_PORT=tcp://172.17.0.2:3306 DB_PORT_3306_TCP_ADDR=172.17.0.2 LANG=en_US.UTF-8 HOSTNAME=web DB_PORT_3306_TCP=tcp://172.17.0.2:3306 DB_PORT_3306_TCP_PORT=3306 PWD=/ HOME=/root TERM=xterm SHLVL=1 DB_NAME=/web/db PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DB_PORT_3306_TCP_PROTO=tcp LESSOPEN=||/usr/bin/lesspipe.sh %s _=/usr/bin/env
可以发现--link选项主要是向节点的hosts文件和环境变量添加被连接节点的信息
===================来自一泽涟漪的博客,转载请标明出处 www.cnblogs.com/ilifeilong===================