外部网络访问容器
在启动容器时,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。 当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射。当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放的网络端口。首先使用centos镜像新建一个容器,然后在该容器中安装httpd服务并启动
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7169e8be6d3e centos "/bin/bash" About an hour ago Up About an hour serene_goldstine4cd696928bbe centos "bash" About an hour ago Up About an hour cent_testv24f5bf6f33f2c centos "bash" About an hour ago Up About an hour gloomy_colden0a80861145c9 centos "bash" About an hour ago Up About an hour mad_carsonfb45150dbc21 centos "bash" 2 hours ago Up 2 hours cent_testv3222c7c5c456 centos "bash" 2 hours ago Up 2 hours sick_albattanie136b27a8e17 centos "bash" 2 hours ago Up 2 hours tender_euclid[root@localhost ~]# docker exec -it 7169 bash[root@7169e8be6d3e /]# yum install -y httpd[root@7169e8be6d3e /]# /usr/sbin/httpdAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.8. Set the 'ServerName' directive globally to suppress this message[root@7169e8be6d3e /]# ps aux |grep httpdroot 151 0.2 0.1 221856 3504 ? Ss 09:18 0:00 /usr/sbin/httpdapache 152 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache 153 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache 154 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache 155 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache 156 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdroot 158 0.0 0.0 8988 816 ? S+ 09:19 0:00 grep --color=auto httpd[root@7169e8be6d3e /]# netstat -lnpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp6 0 0 :::80 :::* LISTEN 151/httpdActive UNIX domain sockets (only servers)Proto RefCnt Flags Type State I-Node PID/Program name Path
再把该容器导成一个新的镜像(centos_with_httpd),然后再使用新镜像创建容器并指定端口映射
[root@7169e8be6d3e /]# exit[root@localhost ~]# docker commit -m "centos_with_httpd" -a "genesis" 7169e centos_with_httpd:genesis50611dba3a229ab939d21b0606d6abb4b0a443e077691d96273a0e09fc58d6ad[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZEcentos_with_httpd genesis 50611dba3a22 32 seconds ago 335.3 MBregistry latest 5c929a8b587a 33 hours ago 33.27 MBgenesis_centos latest 85bc3a58f134 5 days ago 277.6 MB192.168.1.179:5000/busybox latest 9967c5ad88de 12 days ago 1.093 MBbusybox latest 9967c5ad88de 12 days ago 1.093 MBcentos-6-x86 latest 8fca9486a39b 13 days ago 341.3 MBcentos_with_net latest 3e8ea8607f08 4 weeks ago 294.9 MBcentos latest 9baab0af79c4 6 weeks ago 196.7 MB[root@localhost ~]# docker run -itd -p 5123:80 centos_with_httpd:genesis bashc7ffadc832916e12fedb16afc4edd4dfc6e9e38d8cda7333ffac72f7b0da19ee
- -p 可以指定端口映射,本例中将容器的80端口映射为本地的5123端口
[root@localhost ~]# docker exec -it c7ffa bash[root@c7ffadc83291 /]# netstat -lnpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nameActive UNIX domain sockets (only servers)Proto RefCnt Flags Type State I-Node PID/Program name Path[root@c7ffadc83291 /]# /usr/sbin/httpdAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.9. Set the 'ServerName' directive globally to suppress this message[root@c7ffadc83291 /]# ps aux |grep httpdroot 30 0.0 0.1 221856 3508 ? Ss 09:27 0:00 /usr/sbin/httpdapache 31 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache 32 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache 33 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache 34 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache 35 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdroot 37 0.0 0.0 8988 816 ? S+ 09:28 0:00 grep --color=auto httpd[root@c7ffadc83291 /]# curl localhostApache HTTP Server Test Page powered by CentOS Testing 123..
This page is used to test the proper operation of the Apache HTTP server after it has been installed. If you can read this page it means that this site is working properly. This server is powered by CentOS.
[root@c7ffadc83291 /]# vi /var/www/html/1.htmlJust visiting?
The website you just visited is either experiencing problems or is undergoing routine maintenance.
If you would like to let the administrators of this website know that you've seen this page instead of the page you expected, you should send them e-mail. In general, mail sent to the name "webmaster" and directed to the website's domain should reach the appropriate person.
For example, if you experienced problems while visiting www.example.com, you should send e-mail to "webmaster@example.com".
Are you the Administrator?
You should add your website content to the directory /var/www/html/.
To prevent this page from ever being used, follow the instructions in the file /etc/httpd/conf.d/welcome.conf.
Promoting Apache and CentOS
You are free to use the images below on Apache and CentOS Linux powered HTTP servers. Thanks for using Apache and CentOS!
写入ginesis2011.github.io,保存退出(内容可以随意填写)
[root@c7ffadc83291 /]# curl localhost/1.htmlginesis2011.github.io[root@c7ffadc83291 /]# exit[root@localhost ~]# curl 192.168.1.179:5123/1.htmlginesis2011.github.io
- 注:192.168.1.179为宿主机IP
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc7ffadc83291 centos_with_httpd:genesis "bash" 8 minutes ago Up 8 minutes 0.0.0.0:5123->80/tcp furious_banach7169e8be6d3e centos "/bin/bash" About an hour ago Up About an hour serene_goldstine4cd696928bbe centos "bash" About an hour ago Up About an hour cent_testv24f5bf6f33f2c centos "bash" 2 hours ago Up 2 hours gloomy_colden0a80861145c9 centos "bash" 2 hours ago Up 2 hours mad_carsonfb45150dbc21 centos "bash" 2 hours ago Up 2 hours cent_testv3222c7c5c456 centos "bash" 3 hours ago Up 3 hours sick_albattanie136b27a8e17 centos "bash" 3 hours ago Up 3 hours tender_euclid