我们一般将应用部署在容器里面,而一个服务器上会有许许多多的容器,那么外界该如何访问我们的应用呢?答案是:端口映射。

Docker可以将容器对外提供服务的端口映射到host的某个端口上,外网通过此端口访问容器,要开启此功能,容器在启动时需要通过-p参数指定映射的端口号。

$ sudo docker run -d -p 80 nginx
0627d4b5aefe03d4d99a92b559d3e58d789c006961722178c9f1e94f0f270492
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0627d4b5aefe nginx "/docker-entrypoint.…" 9 seconds ago Up 8 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp stupefied_brahmagupta
$ curl 127.0.0.1:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>

我们可以看到,使用-p 80参数后,docker将容器中80端口映射到host的49153端口,所以我们在host直接通过49153端口就能访问到nginx服务。

那我们能否自行指定所要映射的host的端口号呢?这也是可以的,我们只需要在-p参数中将host的端口也指定上就可以了:

$ sudo docker run -d -p 8080:80 nginx
13cd7862e4d74238599e9321e897ea03cf2c01f837202bb4363ea48d052c850e
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13cd7862e4d7 nginx "/docker-entrypoint.…" 9 seconds ago Up 7 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp kind_kirch
$ curl 127.0.0.1:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>

最新文章

  1. Oracle 文件的导入与导出
  2. angular+ionic返回上一页并刷新
  3. 小心一些,断言可能让你的造成循环引用NSAssert
  4. css省略号
  5. Linux命令入门
  6. 查询计划Hash和查询Hash
  7. SharedPreference 存储小量数据,一般首次启动显示引导界面就用这个。
  8. 使用命令行设置svn忽略列表
  9. 2013华为校园招聘java实现(大家水个回复啊)
  10. 实现ImageView中两张图片重叠显示
  11. MFC界面开发(QQ透明皮肤:多层算法,一键适配各种背景 )
  12. win7 VMware Workstation Centos6.5虚机桥接上网设置 详解(靠谱)
  13. 操作3 mongodb和mysql 开启慢查询日志 ,以及mongodb从配置文件启动
  14. mmc生产任务分配问题
  15. MongoDB的upsert状态判断和pymongo使用方法
  16. php7.0 和 php7.1新特性
  17. 48、tensorflow入门二,线性模型的拟合
  18. openpose模型在AI challenge人体骨骼关键点检测的表现
  19. [ZJOI2015]地震后的幻想乡
  20. 在IT行业获得成功 你只需一项技能[转]

热门文章

  1. Linux 软件安装位置选择指南
  2. 学习java 7.9
  3. day08 外键字段的增删查改
  4. Linux:spool命令
  5. Redis集群环境各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的解决方式
  6. 一文详解 纹理采样与Mipmap纹理——构建山地渲染效果
  7. JavaScript中的NaN
  8. IOS学习路径
  9. [BUUCTF]PWN——ciscn_2019_n_3
  10. 在【自定义列】中使用M函数(Power Query 之 M 语言)