需求
建立一个网站,希望这个网站能被别人访问到。如果我们在本地创建一个Nginx服务器。
iie4bu@hostdocker:~$ docker run --name web -d nginx
d6edf032ce6ee4126124b89d30c6d11793a120865a004abb64fe20fad2e3a43b
iie4bu@hostdocker:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6edf032ce6e nginx "nginx -g 'daemon of…" 3 minutes ago Up 3 minutes 80/tcp web
但是我们的Nginx的服务目前是访问不了的。只能通过docker exec -it web /bin/bash来访问。
因此需要把Nginx服务暴露到外面。
我们知道这个Nginx容器是一个独立的network namespace,有ip地址,先查看他的网络情况
iie4bu@hostdocker:~$ docker network inspect bridge
[
{
"Name": "bridge",
"Id": "b7c11f829aacbfe6578b556865d2bbd6d2276442cb58099ae2edb7167b85b365",
"Created": "2019-06-26T09:52:55.529849773+08: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": {
"d6edf032ce6ee4126124b89d30c6d11793a120865a004abb64fe20fad2e3a43b": {
"Name": "web",
"EndpointID": "d03b95f55185e75908cf04fd3075a5cc4d91fa1df214643b2013d7964f19fb0c",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/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": "1500"
},
"Labels": {}
}
]
Nginx默认是连到bridge上面的。可以看到ip是172.17.0.4
在外面是可以ping到这个ip的:
iie4bu@hostdocker:~$ ping 172.17.0.4
PING 172.17.0.4 (172.17.0.4) 56(84) bytes of data.
64 bytes from 172.17.0.4: icmp_seq=1 ttl=64 time=0.624 ms
64 bytes from 172.17.0.4: icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from 172.17.0.4: icmp_seq=3 ttl=64 time=0.048 ms
^C
--- 172.17.0.4 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.048/0.241/0.624/0.270 ms
iie4bu@hostdocker:~$
因为他是连到我们的docker0上面的。
iie4bu@hostdocker:~$ telnet 172.17.0.4 80
Trying 172.17.0.4...
Connected to 172.17.0.4.
Escape character is '^]'.
说明是能访问的。
使用curl访问:
iie4bu@hostdocker:~$ curl http://172.17.0.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
curl成功访问。
我们希望外面也可以访问。
目前Nginx端口只绑定到了172.17.0.4的网络空间。如果把端口映射到本地的服务器上80端口,是不是就可以了。
如何把Nginx的80端口映射到本地?
先将Nginx停止:docker container stop web并且删除docker container rm web
从新添加一个Nginx container,并添加参数-p,将容器里面的80端口,映射到本地的80端口
iie4bu@hostdocker:~$ docker run --name web -d -p 80:80 nginx
94f9176f55a6ea9180d571bbb2f13319f9b5024bc4e4aae8ff28d0a92394cc79
iie4bu@hostdocker:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94f9176f55a6 nginx "nginx -g 'daemon of…" 18 minutes ago Up 18 minutes 0.0.0.0:80->80/tcp web
这样可以通过curl 127.0.0.1访问Nginx了。
iie4bu@hostdocker:~$ curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
通过本地的80端口就可以访问容器中的80端口的服务了。