docker-02端口映射

docker-02端口映射

docker 仅在run的时候提供了-p参数来增加端口映射,这无法满足我们后续安装应用的需求,就需要增补端口映射,

特别提醒:为了格式正确,建议在执行run命令的时候,指定一个端口映射,后续增加的时候进行复制黏贴

1)停止docker 容器:
docker stop 容器id/容器名称

2)停止docker
systemctl stop docker
特别注意:一定要停止docker,否则修改了的配置文件会被还原

3)进入目录,修改以下文件:
/var/lib/docker/containers/[容器id]/hostconfig.json  
/var/lib/docker/containers/[容器id]/config.v2.json 

第一处修改:
hostconfig.json 在根节点下 加入以下配置:
    "PortBindings": {
        "8080/tcp": [{
            "HostIp": "",
            "HostPort": "8089"
        }]
    },

第二处修改:
config.v2.json 在Config节点下的ExposedPorts节点(如果没有可以添加ExposedPorts节点)下添加以下配置:
"8080/tcp": {}

最终Config如下:
"Config": {
        "Hostname": "e2040090cf57",
        "Domainname": "",
        "User": "",
        "AttachStdin": true,
        "AttachStdout": true,
        "AttachStderr": true,
        "ExposedPorts": {
            "8080/tcp": {}
        },
        "Tty": true,
        "OpenStdin": true,
        "StdinOnce": true,
        "Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
        "Cmd": ["/bin/bash"],
        "Image": "centos",
        "Volumes": null,
        "WorkingDir": "",
        "Entrypoint": null,
        "OnBuild": null,
        "Labels": {
            "org.label-schema.build-date": "20201204",
            "org.label-schema.license": "GPLv2",
            "org.label-schema.name": "CentOS Base Image",
            "org.label-schema.schema-version": "1.0",
            "org.label-schema.vendor": "CentOS"
        }
    },

第三处修改:
config.v2.json 在Config节点下的NetworkSettings节点的Ports节点加入以下配置:
            "8080/tcp": [{
                "HostIp": "0.0.0.0",
                "HostPort": "8089"
            }]
最终NetworkSettings如下:
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "7657e7557a4fc7faec2644e4b6e4dd89aa130b2d292cc79e8c5be11b45315e29",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "afcbb96c4ff2f8fcb46dcfc104d85975bf69aa281e4a8ef77ae08ff02af279a6",
                "EndpointID": "ae8c89d80cdfc6ac3c617be9595e723dba9f17464f08cfc6a29ea7b3d828947e",
                "Gateway": "172.17.0.1",
                "IPAddress": "172.17.0.3",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:11:00:03",
                "DriverOpts": null,
                "IPAMOperational": false
            }
        },
        "Service": null,
        "Ports": {
            "8080/tcp": [{
                "HostIp": "0.0.0.0",
                "HostPort": "8089"
            }]
        },
        "SandboxKey": "/var/run/docker/netns/7657e7557a4f",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "IsAnonymousEndpoint": false,
        "HasSwarmEndpoint": false
    },

5)重启docker
service docker restart
或
systemctl start docker

6)启动容器进行测试

 

posted @ 2021-02-23 06:58  守望一心  阅读(71)  评论(0编辑  收藏  举报