Docker之容器端口映射

1.启动一个nginx容器

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

docker pull nginx

启动nginx容器

docker run --name web-nginx -d nginx

2.查看当前bridge的网络情况
[root@localhost ~]# docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "19fa32c5bc3e1f64e4d2818d6899d65a73a6290b687a890166ded26460a5ab6c",
        "Created": "2019-03-01T08:47:29.847096345+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": {
            "1c709e2c5d8f99081f030c6fbdb92150fbd51d0133cd5e5066207b24b31fb611": {
                "Name": "web-nginx",
                "EndpointID": "de3a703e2c6808af069f97ee04f246924f4bdc1b1320b7d595d59284252c34fc",
                "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": "1500"
        },
        "Labels": {}
    }
]

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

3.测试地址发访问
[root@localhost ~]# curl http://172.17.0.2:80



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

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

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

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

docker stop web-nginx
docker rm web-nginx

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

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

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

[root@localhost ~]# curl 127.0.0.1:80



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

地址转发结构图:
Docker之容器端口映射_第1张图片

你可能感兴趣的:(#,docker)