使用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 { 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> [root@localhost ~]#
我的宿主机是在VMware虚拟的centos,因为IP是绑定在nginx container网络空间, 想要让nginx能从外面访问,则需要做端口映射,把container上的80端口,映射到宿主机上
1 [root@localhost ~]# docker run -d --name web -p 8080:80 nginx #-p 8080:80 把本地8080端口映射到container 80端口 2 c24bf84a9d3bdd46fa33f13032c27bbba22ff4cb24ba9cb2309d2570a41e2853 3 [root@localhost ~]# docker container ps 4 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5 c24bf84a9d3b nginx "nginx -g 'daemon of…" 14 seconds ago Up 13 seconds 0.0.0.0:8080->80/tcp web 6 [root@localhost ~]# docker network inspect bridge #查看container IP 7 [ 8 { 9 "Name": "bridge", 10 "Id": "ac664a242c917c931998806bfc970c0fb3c7c9c5b0cbed7769b5a71978ff9748", 11 "Created": "2019-07-19T02:40:37.951941039-07:00", 12 "Scope": "local", 13 "Driver": "bridge", 14 "EnableIPv6": false, 15 "IPAM": { 16 "Driver": "default", 17 "Options": null, 18 "Config": [ 19 { 20 "Subnet": "172.17.0.0/16", 21 "Gateway": "172.17.0.1" 22 } 23 ] 24 }, 25 "Internal": false, 26 "Attachable": false, 27 "Ingress": false, 28 "ConfigFrom": { 29 "Network": "" 30 }, 31 "ConfigOnly": false, 32 "Containers": { 33 "c24bf84a9d3bdd46fa33f13032c27bbba22ff4cb24ba9cb2309d2570a41e2853": { 34 "Name": "web", 35 "EndpointID": "0eab9a9edb82d03bec9193175689159a2c8826ecfa70b183e84227251a5713fe", 36 "MacAddress": "02:42:ac:11:00:02", 37 "IPv4Address": "172.17.0.2/16", 38 "IPv6Address": "" 39 } 40 }, 41 "Options": { 42 "com.docker.network.bridge.default_bridge": "true", 43 "com.docker.network.bridge.enable_icc": "true", 44 "com.docker.network.bridge.enable_ip_masquerade": "true", 45 "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", 46 "com.docker.network.bridge.name": "docker0", 47 "com.docker.network.driver.mtu": "1500" 48 }, 49 "Labels": {} 50 } 51 ] 52 [root@localhost ~]# curl http://172.17.0.2 #访问container IP可以访问 53 <!DOCTYPE html> 54 <html> 55 <head> 56 <title>Welcome to nginx!</title> 57 <style> 58 body { 59 35em; 60 margin: 0 auto; 61 font-family: Tahoma, Verdana, Arial, sans-serif; 62 } 63 </style> 64 </head> 65 <body> 66 <h1>Welcome to nginx!</h1> 67 <p>If you see this page, the nginx web server is successfully installed and 68 working. Further configuration is required.</p> 69 70 <p>For online documentation and support please refer to 71 <a href="http://nginx.org/">nginx.org</a>.<br/> 72 Commercial support is available at 73 <a href="http://nginx.com/">nginx.com</a>.</p> 74 75 <p><em>Thank you for using nginx.</em></p> 76 </body> 77 </html> 78 [root@localhost ~]# curl http:/127.0.0.1:8080 79 curl: (6) Could not resolve host: http; Name or service not known 80 [root@localhost ~]# curl http://127.0.0.1:8080 #测试宿主机 8080端口 81 <!DOCTYPE html> 82 <html> 83 <head> 84 <title>Welcome to nginx!</title> 85 <style> 86 body { 87 35em; 88 margin: 0 auto; 89 font-family: Tahoma, Verdana, Arial, sans-serif; 90 } 91 </style> 92 </head> 93 <body> 94 <h1>Welcome to nginx!</h1> 95 <p>If you see this page, the nginx web server is successfully installed and 96 working. Further configuration is required.</p> 97 98 <p>For online documentation and support please refer to 99 <a href="http://nginx.org/">nginx.org</a>.<br/> 100 Commercial support is available at 101 <a href="http://nginx.com/">nginx.com</a>.</p> 102 103 <p><em>Thank you for using nginx.</em></p> 104 </body> 105 </html> 106 [root@localhost ~]#
如果本机是公网IP,则可以直接互联网访问,我这是虚拟机,如果虚拟机网络模式是NAT,则在本地可以直接用虚拟机IP:8080访问,如果想用本地IP则需要配置NAT端口转换,比如下面:
配置生效后我可以用本地IP:8081访问 web