docker--container的port映射
使用nginx为例
先运行nginx
[root@localhost ~]# docker run --name web -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
0a4690c5d889: Pull complete
9719afee3eb7: Pull complete
44446b456159: Pull complete
Digest: sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Status: Downloaded newer image for nginx:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
06537c95ca2e0c1885a93755e9fa92aa1a8c7b98ce169764c3197e0208febf79
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06537c95ca2e nginx "nginx -g 'daemon of…" 8 minutes ago Up 8 minutes 80/tcp web
19216c85489e busybox "/bin/sh -c 'while t…" About an hour ago Up About an hour test4
cec10f0cd32d busybox "/bin/sh -c 'while t…" 6 hours ago Up 6 hours test3
68789fa4dc47 busybox "/bin/sh -c 'while t…" 19 hours ago Up 19 hours test2
cba625871070 busybox "/bin/sh -c 'while t…" 24 hours ago Up 24 hours test1
[root@localhost ~]#
查看 IP
[root@localhost ~]# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "4e8172ef8e0169e74285225030d0b5f271494df46c4f7bc3ba38e9ca87a1c6f9",
"Created": "2019-07-17T06:50:29.144315528-07:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"06537c95ca2e0c1885a93755e9fa92aa1a8c7b98ce169764c3197e0208febf79": {
"Name": "web",
"EndpointID": "8ee4eb75d3551bd76a19790f0bdc9f8bbd555e5674daa69e9b51e96d38deb9f1",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
在宿主机能访问nginx
[root@localhost ~]# curl http://172.17.0.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 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>
[root@localhost ~]#
我的宿主机是在VMware虚拟的centos,因为IP是绑定在nginx container网络空间, 想要让nginx能从外面访问,则需要做端口映射,把container上的80端口,映射到宿主机上

[root@localhost ~]# docker run -d --name web -p : nginx #-p 8080:80 把本地8080端口映射到container 80端口
c24bf84a9d3bdd46fa33f13032c27bbba22ff4cb24ba9cb2309d2570a41e2853
[root@localhost ~]# docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c24bf84a9d3b nginx "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp web
[root@localhost ~]# docker network inspect bridge #查看container IP
[
{
"Name": "bridge",
"Id": "ac664a242c917c931998806bfc970c0fb3c7c9c5b0cbed7769b5a71978ff9748",
"Created": "2019-07-19T02:40:37.951941039-07:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"c24bf84a9d3bdd46fa33f13032c27bbba22ff4cb24ba9cb2309d2570a41e2853": {
"Name": "web",
"EndpointID": "0eab9a9edb82d03bec9193175689159a2c8826ecfa70b183e84227251a5713fe",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": ""
},
"Labels": {}
}
]
[root@localhost ~]# curl http://172.17.0.2 #访问container IP可以访问
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 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>
[root@localhost ~]# curl http:/127.0.0.1:
curl: () Could not resolve host: http; Name or service not known
[root@localhost ~]# curl http://127.0.0.1:8080 #测试宿主机 8080端口
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 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>
[root@localhost ~]#
如果本机是公网IP,则可以直接互联网访问,我这是虚拟机,如果虚拟机网络模式是NAT,则在本地可以直接用虚拟机IP:8080访问,如果想用本地IP则需要配置NAT端口转换,比如下面:
配置生效后我可以用本地IP:8081访问 web


docker--container的port映射的更多相关文章
- Docker源码分析(七):Docker Container网络 (上)
1.前言(什么是Docker Container) 如今,Docker技术大行其道,大家在尝试以及玩转Docker的同时,肯定离不开一个概念,那就是“容器”或者“Docker Container”.那 ...
- 如何在查看docker container内进程信息,与宿主机上进程信息的映射关系
docker container内运行的进程,在宿主机上,通过ps也是能够查到的,但是在不熟悉命令的时候,无法快速找到他们的关系. 这里科普一个基础命令 docker top 1. 找到容器的id d ...
- expose a port on a living Docker container
if you have a container that with something running on its port 8000, you can run wget http://contai ...
- docker container(容器)
docker 容器 Docker容器类似于一个轻量级的沙箱,Docker利用容器来运行和隔离应用 容器是从镜像创建的应用运行实例.它可以启动,开始,停止,删除,而这些容器都是彼此相互隔离,互不可见的. ...
- Docker容器内部端口映射到外部宿主机端口的方法小结
转自:https://www.cnblogs.com/kevingrace/p/9453987.html Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务.容器启动之后,容器中可以 ...
- Docker源码分析(八):Docker Container网络(下)
1.Docker Client配置容器网络模式 Docker目前支持4种网络模式,分别是bridge.host.container.none,Docker开发者可以根据自己的需求来确定最适合自己应用场 ...
- Docker container 集装箱说明
容器操作 使用 docker 命令行操作 docker 容器 启动容器 core@localhost ~ $ docker run Usage: docker run [OPTIONS] IMAGE ...
- docker容器的端口映射
1.创建一个Nginx 容器,先不映射端口 [root@localhost ~]# docker run --name my_nginx -d nginx 7be3673a4c0f8f7ffe79a7 ...
- Docker容器内部端口映射到外部宿主机端口 - 运维笔记
Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务.容器启动之后,容器中可以运行一些网络应用,通过-p或-P参数来指定端口映射. 注意:宿主机的一个端口只能映射到容器内部的某一个端口 ...
- Docker容器内部端口映射到外部宿主机端口
Docker允许通过外部访问容器或者容器之间互联的方式来提供网络服务.容器启动之后,容器中可以运行一些网络应用,通过-p或-P参数来指定端口映射. 注意:宿主机的一个端口只能映射到容器内部的某一个端口 ...
随机推荐
- vue,一路走来(1)--构建vue项目
2016年12月--2017年5月,接触前端框架vue,一路走来,觉得有必要把遇到的问题记录下来. 那时,vux用的是1.0的vue,然而vue2.0已经出来了,于是我结合了mint-ui一起来做项目 ...
- Git--07 Gitlab备份与恢复
目录 Gitlab备份与恢复 01). 备份 02). 恢复 Gitlab备份与恢复 对gitlab进行备份将会创建一个包含所有库和附件的归档文件.对备份的恢复只能恢复到与备份时的gitlab相同 ...
- android采集设备
http://www.hingway.com/xindalu/xindalu-data.htm 安卓新大陆设备 wce优博讯设备
- Opacity函数-transparentize()、 fade-out()函数
transparentize() 和 fade-out() 函数所起作用刚好与 opacify() 和 fade-in() 函数相反,让颜色更加的透明.这两个函数会让透明值做减法运算,当计算出来的结果 ...
- 【抓包工具之Fiddler】增加IP列;session高亮
Fiddler 在处理每个session时,脚本文件CustomRules.js中的方法都会运行,该脚本使得你可以隐藏,标识或任意修改负责的session.规则脚本在运行状态下就可以修改并重新编译,不 ...
- svnkit 异常:Exception in thread "main" org.tmatesoft.svn.core.SVNException: svn: E200030: SQLite error
https://stackoverflow.com/questions/16063105/org-tmatesoft-sqljet-core-sqljetexception-busy-error-co ...
- 洛谷4721 【模板】分治 FFT
传送门 久违的多项式全家桶= =+ 分治NTT 用的就是cdq分治的思想 对于当前递归到的区间[l,r] 我们处理出[l,mid]对[mid+1,r]答案的贡献 然后分治递归求解就可以啦qwq 这个贡 ...
- 分布式架构的CAP原理
CAP 定理的含义 一.分布式系统的三个指标 1998年,加州大学的计算机科学家 Eric Brewer 提出,分布式系统有三个指标. Consistency Availability Parti ...
- Web核心之会话技术Cookie&Session
什么是会话技术? http协议是无状态协议.为了满足在多次请求之间数据进行交互,推出了会话技术. 会话概念:一次会话,指的是从客户端和服务器建立起连接开始,到客户端或服务器断开连接为止.中间可能进行多 ...
- boost Shared Memory
Shared Memory Shared memory is typically the fastest form of interprocess communicatioin. It provide ...