900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Docker-端口映射实现访问容器

Docker-端口映射实现访问容器

时间:2020-10-14 07:23:00

相关推荐

Docker-端口映射实现访问容器

外部网络访问容器

在启动容器时,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。

当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射。当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放的网络端口。

首先使用centos镜像新建一个容器,然后在该容器中安装httpd服务并启动

[root@localhost ~]# docker psCONTAINER ID IMAGECOMMAND CREATED STATUS PORTSNAMES7169e8be6d3e 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 hourscent_testv3222c7c5c456 centos "bash" 2 hours ago Up 2 hourssick_albattanie136b27a8e17 centos "bash" 2 hours ago Up 2 hourstender_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/httpdapache152 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache153 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache154 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache155 0.0 0.1 221856 2632 ? S 09:18 0:00 /usr/sbin/httpdapache156 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 00 :::80 :::*LISTEN151/httpdActive UNIX domain sockets (only servers)Proto RefCnt Flags Type State I-Node PID/Program namePath

再把该容器导成一个新的镜像(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 ago335.3 MBregistry latest 5c929a8b587a 33 hours ago 33.27 MBgenesis_centoslatest 85bc3a58f134 5 days ago277.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 namePath[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/httpdapache31 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache32 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache33 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache34 0.0 0.1 221856 2632 ? S 09:27 0:00 /usr/sbin/httpdapache35 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<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "/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="">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="">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 , you should send e-mail to "webmaster@".</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="/"><img src="images/apache_pb.gif" alt="[ Powered by Apache ]"></a> <a href="/"><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 , 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 , you would find the owner of the domain at the following WHOIS server:</p><p><a href="/whois.html">/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="/">CentOS website</a>.</p></div></div></div></div></div></body></html>[root@c7ffadc83291 /]# vi /var/www/html/1.html

写入ginesis.github.io,保存退出(内容可以随意填写)

[root@c7ffadc83291 /]# curl localhost/1.htmlginesis.github.io[root@c7ffadc83291 /]# exit[root@localhost ~]# curl 192.168.1.179:5123/1.htmlginesis.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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。