Docker之容器端口映射

女爷i 2023-06-18 14:52 68阅读 0赞
1.启动一个nginx容器

如果没有下载ngin镜像的话,先下载一个

docker pull nginx

启动nginx容器

docker run —name web-nginx -d nginx

2.查看当前bridge的网络情况
  1. [root@localhost ~]# docker network inspect bridge
  2. [
  3. {
  4. "Name": "bridge",
  5. "Id": "19fa32c5bc3e1f64e4d2818d6899d65a73a6290b687a890166ded26460a5ab6c",
  6. "Created": "2019-03-01T08:47:29.847096345+08:00",
  7. "Scope": "local",
  8. "Driver": "bridge",
  9. "EnableIPv6": false,
  10. "IPAM": {
  11. "Driver": "default",
  12. "Options": null,
  13. "Config": [
  14. {
  15. "Subnet": "172.17.0.0/16",
  16. "Gateway": "172.17.0.1"
  17. }
  18. ]
  19. },
  20. "Internal": false,
  21. "Attachable": false,
  22. "Ingress": false,
  23. "ConfigFrom": {
  24. "Network": ""
  25. },
  26. "ConfigOnly": false,
  27. "Containers": {
  28. "1c709e2c5d8f99081f030c6fbdb92150fbd51d0133cd5e5066207b24b31fb611": {
  29. "Name": "web-nginx",
  30. "EndpointID": "de3a703e2c6808af069f97ee04f246924f4bdc1b1320b7d595d59284252c34fc",
  31. "MacAddress": "02:42:ac:11:00:02",
  32. "IPv4Address": "172.17.0.2/16",
  33. "IPv6Address": ""
  34. }
  35. },
  36. "Options": {
  37. "com.docker.network.bridge.default_bridge": "true",
  38. "com.docker.network.bridge.enable_icc": "true",
  39. "com.docker.network.bridge.enable_ip_masquerade": "true",
  40. "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
  41. "com.docker.network.bridge.name": "docker0",
  42. "com.docker.network.driver.mtu": "1500"
  43. },
  44. "Labels": {}
  45. }
  46. ]

发现web-nginx的ip地址为 172.17.0.2/16

3.测试地址发访问
  1. [root@localhost ~]# curl http://172.17.0.2:80
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx!</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx!</h1>
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required.</p>
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org</a>.<br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com</a>.</p>
  22. <p><em>Thank you for using nginx.</em></p>
  23. </body>
  24. </html>

如果用该地址去访问是可以的,但是如果用127.0.0.1:80去访问就不行了

4.端口映射(-p 参数)

(1).先停止移除掉刚创建的容器
停止以及移除

  1. docker stop web-nginx
  2. docker rm web-nginx

(2) 重新创建nginx容器,将nginx的80端口,映射到本机的80端口

  1. docker run --name web-nginx -d -p 80:80 nginx

重新使用127.0.0.1:80访问,发现可以访问了

  1. [root@localhost ~]# curl 127.0.0.1:80
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Welcome to nginx!</title>
  6. <style>
  7. body {
  8. width: 35em;
  9. margin: 0 auto;
  10. font-family: Tahoma, Verdana, Arial, sans-serif;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Welcome to nginx!</h1>
  16. <p>If you see this page, the nginx web server is successfully installed and
  17. working. Further configuration is required.</p>
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org</a>.<br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com</a>.</p>
  22. <p><em>Thank you for using nginx.</em></p>
  23. </body>
  24. </html>

地址转发结构图:
image

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Docker——端口映射容器互联

    端口映射实现容器访问 1. 从外部访问容器应用: 在启动容器的时候,如果不指定对应参数,在容器外部是无法通过网络来访问容器内的网络应用和服务的。 当容器中运行一些网络