一、从外部访问容器应用
1、端口映射实现访问容器root@docker-test:~# docker run -d -P training/webapp python app.py61975c6a1981e829a7edb8e07d24c849aad7132c80e14338e6aaab4d12d25021root@docker-test:~# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES61975c6a1981 training/webapp:latest "python app.py" 8 seconds ago Up 7 seconds 0.0.0.0:32783->5000/tcp furious_pasteur 大写的-P。会随机映射一个49000-49900的端口至容器内部开放的网络端口小写的-p,则指定固定端口上面的ps -l看出,本地的32783端口被映射到了容器的5000端口,访问宿主机的32783端口即可访问容器内web应用提供界面通过我宿主机的IP地址进行访问 http://192.168.89.130:32783/2、通过docker logs查看应用的信息root@docker-test:~# docker logs -f furious_pasteur * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)192.168.89.1 - - [12/Aug/2017 12:58:34] "GET / HTTP/1.1" 200 -192.168.89.1 - - [12/Aug/2017 12:58:35] "GET /favicon.ico HTTP/1.1" 404 -192.168.89.1 - - [12/Aug/2017 12:59:56] "GET / HTTP/1.1" 200 -3、映射所有接口地址将本地的5000端口映射到容器的5000端口docker run -d -p 5000:5000 training/webapp python app.py #此时会绑定本地所有接口上的所有地址4、映射到指定地址的指定端口docker run -d -p 192.168.89.130:5000:5000 training/webapp python app.py5、映射到指定地址的任意端口docker run -d -p 192.168.89.130::5000 training/webapp python app.py6、查看映射端口配置root@docker-test:~# root@docker-test:~# docker port testweb5000/tcp -> 0.0.0.0:32782root@docker-test:~# docker port web5000/tcp -> 0.0.0.0:32781root@docker-test:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES61975c6a1981 training/webapp:latest "python app.py" 14 minutes ago Up 14 minutes 0.0.0.0:32783->5000/tcp furious_pasteur 235a7f166a79 training/webapp:latest "/bin/bash" 33 minutes ago Up 33 minutes 0.0.0.0:32782->5000/tcp testweb 6c498fba9a26 training/webapp:latest "/bin/bash" 40 minutes ago Up 40 minutes 0.0.0.0:32781->5000/tcp web 39d71abc194d ubuntu:latest "/bin/bash" About an hour ago Up About an hour db2 593de70961a0 ubuntu:latest "/bin/bash" About an hour ago Up About an hour db1 a6a32f7838ee ubuntu:latest "/bin/bash" About an hour ago Up About an hour dbdata root@docker-test:~# docker port 61975c6a19815000/tcp -> 0.0.0.0:32783root@docker-test:~# 二、自定义容器命名1、自定义容器命名root@docker-test:~# docker run -d -P --name web training/webapp python app.py #使用--name选项2、查看设定后的命名root@docker-test:~# docker ps -l3、使用inspect查看指定容器ID的容器的名字root@docker-test:~# docker inspect -f "` `.`Name `" 382b382521b5二、容器互联(实现容器间通信)使用--link参数可以让容器之间安全的进行交互创建一个新的数据库容器进行演示1、创建并运行数据库容器,名称为dbroot@docker-test:~# docker run -d --name db training/postgres #如本地没有training/postgres镜像,则会自动从docker hub搜索到镜像并下载2、创建完成之后查看一下root@docker-test:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES19f67a71ac16 training/postgres:latest "su postgres -c '/us 2 minutes ago Up 2 minutes 5432/tcp db 3、然后创建一个新的web容器,并将它连接到db容器root@docker-test:~# docker run -d -P --name web --link db:db training/webapp python app.py3ac18b1df0d487c99b0f07e2fd273c53ad7b6f5beb29b830985c89fd6df402414、查看创建的两个docker容器(web容器和db容器)root@docker-test:~# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3ac18b1df0d4 training/webapp:latest "python app.py" 6 seconds ago Up 4 seconds 0.0.0.0:32769->5000/tcp web 651b10778678 training/postgres:latest "su postgres -c '/us 2 minutes ago Up 2 minutes 5432/tcp db