外部网络访问容器
在启动容器时,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。
当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-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="data:images/apache_pb.gif" alt="[ Powered by Apache ]"></a> <a href="http://www.centos.org/"><img src="data: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 fromthe 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 informationplease 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

转载于:https://www.cnblogs.com/Genesis2018/p/9079796.html

Docker-端口映射实现访问容器相关推荐

  1. Docker端口映射无法访问的问题排查

    概述 前些天,老大让升级Docker版本,后面折腾了一番,成功升级到最新版本Docker,Docker容器啥的都跑起来了,以为一切顺利了,结果第二天就发现容器映射到宿主机的端口无法访问.在宿主机用 c ...

  2. docker端口映射或启动容器时报错 driver failed programming external connectivity on endpoint quirky_allen

    docker端口映射或启动容器时报错  Error response from daemon: driver failed programming external connectivity on e ...

  3. docker端口映射或启动容器时报错 driver failed programming external connectivity on endpoint

    docker端口映射或启动容器时报错 Error response from daemon: driver failed programming external connectivity on en ...

  4. docker端口映射或启动容器时报错Error response from daemon: Container is not running

    docker端口映射或启动容器时: docker exec -it 7c5a2350c4cb /bin/bash 出现了错误: Error response from daemon: Containe ...

  5. linux docker端口映射无法访问,docker设置了端口映射,不能访问的解决方案

    #docker ps 查看, 所有端口都 做了映射 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 764b158ba491 open-fa ...

  6. docker端口映射或启动容器时报错Error response from daemon: Container 7c5a2350c4cb2370d8aba94eef9cec4a6c2ac3207030

    现象: docker exec -it 7c5a2350c4cb /bin/bash Error response from daemon: Container 7c5a2350c4cb2370d8a ...

  7. docker端口映射,批量删除容器

    docker端口映射 http://blog.csdn.net/yjk13703623757/article/details/69212521 批量删除容器 http://blog.csdn.net/ ...

  8. [视频教程] docker端口映射与目录共享运行PHP

    当我们在容器中安装完环境以后,需要在宿主机的端口上访问到容器中的端口,这时候就需要做端口映射.在开发代码的时候,需要频繁的修改代码,因此要把宿主机上的代码目录共享到容器中,这样容器里面就能访问的到代码 ...

  9. docker端口映射失效解决方法

    docker端口映射失效解决方法 参考文章: (1)docker端口映射失效解决方法 (2)https://www.cnblogs.com/erfsfj-dbc/p/11815972.html 备忘一 ...

  10. docker端口映射--外网无法访问tomcat容器

    1.dockers运行tomcat容器,并进行端口映射到宿主机 docker run -dit --name tomcat01 -p 28080:8080 tomcat 2.外网访问tomcat 映射 ...

最新文章

  1. android imageview 的常用属性,android imageview scaletype属性
  2. 春节回来了,你收获了什么?
  3. 深度学习pytorch--softmax回归(一)
  4. 锋利的jQuery--jQuery与DOM对象的互相转换,DOM的三种操作(读书笔记一)
  5. 中国长租公寓市场白皮书
  6. Python学习入门5:Python到底应该怎么学?
  7. oracle 表空间 碎片,Oracle表空间碎片整理
  8. 【Deep Learning 二】课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)答案
  9. ITIL小故事-谁动了他的红包
  10. 【分享】小工具大智慧之Sql执行工具
  11. ajaxfileupload struts2 null_去掉烦人的 “ ! = null (判空语句)
  12. python调用通达信函数用户指标_最新最全通达信公式教程大全(函数-指标-实例)...
  13. 《博弈论》— 人生何处不博弈
  14. 故障集——user is currently used(无法删除用户)
  15. 使用facebook账号登录
  16. JavaScript几种继承方式
  17. Sql Server Update 更新数据
  18. 管中窥豹之淘宝大数据平台
  19. colab使用入门(1)-安装库,保存/加载笔记本
  20. c语言智能手环程序,首款医学智能手环c+手环使用图文教程

热门文章

  1. python---用python实现插入排序
  2. unity 获取鼠标点击位置_Unity中实现瓶中液体晃动的效果(从建模开始)一
  3. golang语言中的关于err的函数封装事项的函数使用
  4. JAVA冰箱评测开题报告,家用冰箱毕业论文 开题报告
  5. ip代理服务器8需要云速捷_nginx获取真实ip,proxy_set_head怎么设置头部信息
  6. 乱码 设置界面_三星手机系统爆出BUG!有用户反映手机黑屏、乱码、无限重启...
  7. sqlmap tamper mysql_Sqlmap过waf命令tamper各脚本的适用环境
  8. 项目搭建 windows_08. SpringCloud实战项目-配置Git
  9. Samba配置文件常用参数详解
  10. Quick-cocos2d-x3.3 ios实现头像选取