一、端口映射实现访问容器
1.1、-P参数(大写),会随机映射一个端口至容器内部开放的端口
[root@CentOS702 ~]# docker run -d -P training/webapp python app.py 7a8bd82f251d7e59e0c413c577c4986e02f2d62284e8b01fe215f8c49fd6bd61 [root@CentOS702 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a8bd82f251d training/webapp "python app.py" 6 seconds ago Up 5 seconds 0.0.0.0:32781->5000/tcp quizzical_brattain [root@CentOS702 ~]# docker logs -f 7a8b * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 192.168.17.218 - - [31/May/2017 09:00:23] "GET / HTTP/1.1" 200 - 192.168.17.218 - - [31/May/2017 09:00:23] "GET /favicon.ico HTTP/1.1" 404 - 192.168.17.218 - - [31/May/2017 09:00:23] "GET /favicon.ico HTTP/1.1" 404 - 192.168.17.218 - - [31/May/2017 09:00:29] "GET / HTTP/1.1" 200 - 192.168.17.218 - - [31/May/2017 09:00:33] "GET / HTTP/1.1" 200 -
1.2、-p参数(小写),可以指定要映射的端口
1、映射所有接口地址 (hostPort:containerPort)
docker run -d -p 5000:5000 training/webapp python app.py docker run -d -p 5000:5000 -p 3000:80 training/webapp python app.py //多次使用-p标记可以绑定多个端口
2、映射到指定地址的指定端口 (ip:hostPort:containerPort)
docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py
3、映射到指定地址的任意端口 (ip::containerPort)
docker run -d -p 127.0.0.1::5000 training/webapp python app.py docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py //指定udp协议
4、查看映射端口配置
[root@CentOS702 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 097d8db0ae77 training/webapp "python app.py" 9 minutes ago Up 9 minutes 5000/tcp, 127.0.0.1:5000->5000/udp hungry_jepsen af92eebaed1c training/webapp "python app.py" 9 minutes ago Up 9 minutes 127.0.0.1:32768->5000/tcp distracted_heyrovsky [root@CentOS702 ~]# docker port af92 5000/tcp -> 127.0.0.1:32768 [root@CentOS702 ~]# docker port af92 5000 127.0.0.1:32768 [root@CentOS702 ~]# docker port 097d 5000/udp 127.0.0.1:5000
[root@CentOS702 ~]# docker inspect 097d //查看容器信息,其中网络信息截图如下
二、容器互联实现容器间通信
1、自定义容器命名
容器互联通过容器的名称来操作,因此首先需要为容器指定一个好记的名称,使用--name标记为容器命名。
[root@CentOS702 ~]# docker run -d -P --name web training/webapp python app.py 8f661aada5658a0e3121713190ed7724f02dbc65bf4a185975d6cf302ed7cb31 [root@CentOS702 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8f661aada565 training/webapp "python app.py" 6 seconds ago Up 5 seconds 0.0.0.0:32784->5000/tcp web [root@CentOS702 ~]# docker inspect -f "{{ .Name }}" 8f66 /web
2、容器互联
使用--link参数可以让容器之间安全
创建一个新的数据库容器 docker run -d --name db training/postgres 删除之前创建的web容器 docker rm -f web 创建一个新的web容器,并将它连接到db容器 docker run -d -P --name web --link db:db training/webapp python app.py --link参数的格式为:--link name:alias,其中name是要链接的容器名称,alias是这个连接的别名。
docker通过两种方式为容器公开连接信息:环境变量、更新/etc/hosts文件
[root@CentOS702 ~]# docker exec d91b env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=d91b7c33a198 DB_PORT=tcp://172.17.0.2:5432 DB_PORT_5432_TCP=tcp://172.17.0.2:5432 DB_PORT_5432_TCP_ADDR=172.17.0.2 DB_PORT_5432_TCP_PORT=5432 DB_PORT_5432_TCP_PROTO=tcp DB_NAME=/web/db DB_ENV_PG_VERSION=9.3 HOME=/root [root@CentOS702 ~]# docker exec d91b 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 7e17a1d0cb21 172.17.0.3 d91b7c33a198
[root@CentOS702 ~]# docker exec -ti d91b /bin/bash
root@d91b7c33a198:/opt/webapp# 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=0.120 ms
64 bytes from db (172.17.0.2): icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from db (172.17.0.2): icmp_seq=3 ttl=64 time=0.124 ms
64 bytes from db (172.17.0.2): icmp_seq=4 ttl=64 time=0.052 ms