Docker-端口映射实现访问容器
2016-10-20 18:36
711 查看
外部网络访问容器
在启动容器时,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。
当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射。当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放的网络端口。
首先使用centos镜像新建一个容器,然后在该容器中安装httpd服务并启动
再把该容器导成一个新的镜像(centos_with_httpd),然后再使用新镜像创建容器并指定端口映射
-p 可以指定端口映射,本例中将容器的80端口映射为本地的5123端口
写入ginesis2011.github.io,保存退出(内容可以随意填写)
注:192.168.1.179为宿主机IP
在启动容器时,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。
当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射。当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放的网络端口。
首先使用centos镜像新建一个容器,然后在该容器中安装httpd服务并启动
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7169e8be6d3e centos "/bin/bash" About an hour ago Up About an hour serene_goldstine 4cd696928bbe centos "bash" About an hour ago Up About an hour cent_testv2 4f5bf6f33f2c centos "bash" About an hour ago Up About an hour gloomy_colden 0a80861145c9 centos "bash" About an hour ago Up About an hour mad_carson fb45150dbc21 centos "bash" 2 hours ago Up 2 hours cent_testv 3222c7c5c456 centos "bash" 2 hours ago Up 2 hours sick_albattani e136b27a8e17 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/httpd AH00558: 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 httpd root 151 0.2 0.1 221856 3504 ? Ss 09:18 0:00 /usr/sbin/httpd apache 152 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpd apache 153 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpd apache 154 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpd apache 155 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpd apache 156 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpd root 158 0.0 0.0 8988 816 ? S+ 09:19 0:00 grep --color=auto httpd [root@7169e8be6d3e /]# netstat -lnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::80 :::* LISTEN 151/httpd Active 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:genesis 50611dba3a229ab939d21b0606d6abb4b0a443e077691d96273a0e09fc58d6ad [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos_with_httpd genesis 50611dba3a22 32 seconds ago 335.3 MB registry latest 5c929a8b587a 33 hours ago 33.27 MB genesis_centos latest 85bc3a58f134 5 days ago 277.6 MB 192.168.1.179:5000/busybox latest 9967c5ad88de 12 days ago 1.093 MB busybox latest 9967c5ad88de 12 days ago 1.093 MB centos-6-x86 latest 8fca9486a39b 13 days ago 341.3 MB centos_with_net latest 3e8ea8607f08 4 weeks ago 294.9 MB centos latest 9baab0af79c4 6 weeks ago 196.7 MB [root@localhost ~]# docker run -itd -p 5123:80 centos_with_httpd:genesis bash c7ffadc832916e12fedb16afc4edd4dfc6e9e38d8cda7333ffac72f7b0da19ee
-p 可以指定端口映射,本例中将容器的80端口映射为本地的5123端口
[root@localhost ~]# docker exec -it c7ffa bash [root@c7ffadc83291 /]# netstat -lnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name Active UNIX domain sockets (only servers) Proto RefCnt Flags Type State I-Node PID/Program name Path [root@c7ffadc83291 /]# /usr/sbin/httpd AH00558: 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 httpd root 30 0.0 0.1 221856 3508 ? Ss 09:27 0:00 /usr/sbin/httpd apache 31 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpd apache 32 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpd apache 33 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpd apache 34 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpd apache 35 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpd root 37 0.0 0.0 8988 816 ? S+ 09:28 0:00 grep --color=auto httpd [root@c7ffadc83291 /]# curl localhost <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Apache HTTP Server Test Page powered by CentOS</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- Bootstrap --> <link href="/noindex/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="noindex/css/open-sans.css" type="text/css" /> <style type="text/css"><!-- body { font-family: "Open Sans", Helvetica, sans-serif; font-weight: 100; color: #ccc; background: rgba(10, 24, 55, 1); font-size: 16px; } h2, h3, h4 { font-weight: 200; } h2 { font-size: 28px; } .jumbotron { margin-bottom: 0; color: #333; background: rgb(212,212,221); /* Old browsers */ background: radial-gradient(ellipse at center top, rgba(255,255,255,1) 0%,rgba(174,174,183,1) 100%); /* W3C */ } .jumbotron h1 { font-size: 128px; font-weight: 700; color: white; text-shadow: 0px 2px 0px #abc, 0px 4px 10px rgba(0,0,0,0.15), 0px 5px 2px rgba(0,0,0,0.1), 0px 6px 30px rgba(0,0,0,0.1); } .jumbotron p { font-size: 28px; font-weight: 100; } .main { background: white; color: #234; border-top: 1px solid rgba(0,0,0,0.12); padding-top: 30px; padding-bottom: 40px; } .footer { border-top: 1px solid rgba(255,255,255,0.2); padding-top: 30px; } --></style> </head> <body> <div class="jumbotron text-center"> <div class="container"> <h1>Testing 123..</h1> <p class="lead">This page is used to test the proper operation of the <a href="http://apache.org">Apache HTTP server</a> after it has been installed. If you can read this page it means that this site is working properly. This server is powered by <a href="http://centos.org">CentOS</a>.</p> </div> </div> <div class="main"> <div class="container"> <div class="row"> <div class="col-sm-6"> <h2>Just visiting?</h2> <p class="lead">The website you just visited is either experiencing problems or is undergoing routine maintenance.</p> <p>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.</p> <p>For example, if you experienced problems while visiting www.example.com, you should send e-mail to "webmaster@example.com".</p> </div> <div class="col-sm-6"> <h2>Are you the Administrator?</h2> <p>You should add your website content to the directory <tt>/var/www/html/</tt>.</p> <p>To prevent this page from ever being used, follow the instructions in the file <tt>/etc/httpd/conf.d/welcome.conf</tt>.</p> <h2>Promoting Apache and CentOS</h2> <p>You are free to use the images below on Apache and CentOS Linux powered HTTP servers. Thanks for using Apache and CentOS!</p> <p><a href="http://httpd.apache.org/"><img src="images/apache_pb.gif" alt="[ Powered by Apache ]"></a> <a href="http://www.centos.org/"><img src="images/poweredby.png" alt="[ Powered by CentOS Linux ]" height="31" width="88"></a></p> </div> </div> </div> </div> </div> <div class="footer"> <div class="container"> <div class="row"> <div class="col-sm-6"> <h2>Important note:</h2> <p class="lead">The CentOS Project has nothing to do with this website or its content, it just provides the software that makes the website run.</p> <p>If you have issues with the content of this site, contact the owner of the domain, not the CentOS project. Unless you intended to visit CentOS.org, the CentOS Project does not have anything to do with this website, the content or the lack of it.</p> <p>For example, if this website is www.example.com, you would find the owner of the example.com domain at the following WHOIS server:</p> <p><a href="http://www.internic.net/whois.html">http://www.internic.net/whois.html</a></p> </div> <div class="col-sm-6"> <h2>The CentOS Project</h2> <p>The CentOS Linux distribution is a stable, predictable, manageable and reproduceable platform derived from the sources of Red Hat Enterprise Linux (RHEL).<p> <p>Additionally to being a popular choice for web hosting, CentOS also provides a rich platform for open source communities to build upon. For more information please visit the <a href="http://www.centos.org/">CentOS website</a>.</p> </div> </div> </div> </div> </div> </body></html> [root@c7ffadc83291 /]# vi /var/www/html/1.html
写入ginesis2011.github.io,保存退出(内容可以随意填写)
[root@c7ffadc83291 /]# curl localhost/1.html ginesis2011.github.io [root@c7ffadc83291 /]# exit [root@localhost ~]# curl 192.168.1.179:5123/1.html ginesis2011.github.io
注:192.168.1.179为宿主机IP
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7ffadc83291 centos_with_httpd:genesis "bash" 8 minutes ago Up 8 minutes 0.0.0.0:5123->80/tcp furious_banach 7169e8be6d3e centos "/bin/bash" About an hour ago Up About an hour serene_goldstine 4cd696928bbe centos "bash" About an hour ago Up About an hour cent_testv2 4f5bf6f33f2c centos "bash" 2 hours ago Up 2 hours gloomy_colden 0a80861145c9 centos "bash" 2 hours ago Up 2 hours mad_carson fb45150dbc21 centos "bash" 2 hours ago Up 2 hours cent_testv 3222c7c5c456 centos "bash" 3 hours ago Up 3 hours sick_albattani e136b27a8e17 centos "bash" 3 hours ago Up 3 hours tender_euclid
相关文章推荐
- Docker端口映射实现网络访问
- (菜汪学docker) 用vmware 完成 docker+ovs+vxlan 实现容器跨主机访问
- docker跨主机容器访问(网桥实现)
- Docker端口映射实现网络访问的方法
- Docker容器技术介绍(六)--- 端口映射和容器间互访问
- docker-proxy 实现容器代理访问
- Windows上访问虚拟机上的docker容器
- Centos上Docker 使用dockerfile构建容器实现ssh
- centos+bond+bridge+docker(ssh容器)固定ip实现测试环境(一)
- centos+bond+bridge+docker(ssh容器)固定ip实现测试环境(二)
- boot2docker(Mac) NAT 容器PORT 给 主机,外部访问
- Docker实现Tengine+php-fpm的多容器协作的web应用环境
- 用dockerfile配置生成docker image并实现容器部署 推荐
- 2.3-docker网络-如何让外部网络访问容器资源
- 【C++】容器适配器实现队列Queue的各种功能(入队、出队、判空、大小、访问所有元素等)
- 使用docker搭建nfs实现容器间共享文件
- docker原始镜像启动容器并创建Apache服务器实现反向代理
- docker从容器中怎么访问宿主机
- Docker:利用Linux容器实现可移植的应用部署
- centos+bond+bridge+docker(ssh容器)固定ip实现测试环境(四)