博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Docker-端口映射实现访问容器
阅读量:6672 次
发布时间:2019-06-25

本文共 10036 字,大约阅读时间需要 33 分钟。

外部网络访问容器

在启动容器时,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。
当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-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 localhost
Apache 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.

Just 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!

[ Powered by Apache ] [ Powered by CentOS Linux ]

[root@c7ffadc83291 /]# vi /var/www/html/1.html

写入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

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

你可能感兴趣的文章
数据蒋堂 | SQL的困难源于关系代数
查看>>
【★】RSA-什么是不对称加密算法?
查看>>
安全漏洞的分类、描述与解决方案
查看>>
新民航安保标准十一起实施 机场安防智慧升级
查看>>
新浪微博应对弹性扩容的架构演进
查看>>
Docker run 命令的使用方法
查看>>
Medium 内部使用 css/less 的代码风格指南
查看>>
解析网络附加存储NAS在安防等行业中应用
查看>>
聊一聊负载均衡SLB的DDoS防护
查看>>
互联网金融正在告别“颠覆者”角色
查看>>
如何缓解Microsoft XML漏洞带来的风险?
查看>>
阿里云启动地震AI大赛:挑战余震震源捕捉
查看>>
中国人工智能学会通讯——构建强健的人工智能:原因及方式 3. 优化对风险敏感的目标...
查看>>
走近比特币:一个故事看懂“区块链”
查看>>
在这里 创新是最默契的表达
查看>>
IDC:第三季度全球HPC服务器收入增长3.9%
查看>>
eclipse中spring访问mysql的简易实现
查看>>
深入DellWorld2015:SC9000存储软硬件更新解密
查看>>
分析:“AI on Hadoop”有意义吗?
查看>>
Logstash详解之——output模块
查看>>