docker容器新增端口映射
记忆中,docker并没有提供直接添加端口映射的接口,所以我们只能修改容器的配置文件了!期望新版本能够更新此功能吧。
当前docker版本:Docker version 19.03.12
1、查看容器哈希编码
$ docker inspect centos-desktop-vnc | grep Id
"Id": "22132e73736a5700dd5b4215a122310220fc3192b19754917caa1b83ecb89dd3",
这里的Id就是哈希编码。
2、停止目标容器
$ docker stop centos-desktop-vnc
3、修改配置文件
$ sudo vim /var/lib/docker/containers/<容器Id>/hostconfig.json
容器的本身端口映射:
{
"PortBindings":{
"22/tcp":[{"HostIp":"","HostPort":"10112"}],
"5901/tcp":[{"HostIp":"","HostPort":"10113"}],
},
}
容器本身的端口映射配置是:
{
"PortBindings":{
"22/tcp":[{"HostIp":"","HostPort":"10112"}],
"5901/tcp":[{"HostIp":"","HostPort":"10113"}],
"3306/tcp": [{"HostIp":"","HostPort":"10114"}],
"5672/tcp": [{"HostIp":"","HostPort":"10115"}],
"6379/tcp": [{"HostIp":"","HostPort":"10116"}],
"8080/tcp": [{"HostIp":"","HostPort":"10117"}],
"80/tcp": [{"HostIp":"","HostPort":"10118"}],
},
}
修改下一个配置:
$ sudo vim /var/lib/docker/containers/<容器Id>/config.v2.json
将下面字段的端口补齐,如我这里要添加3306、5672、6379、8080、80.
{
"ExposedPorts":{
"22/tcp":{},
"5901/tcp":{},
"3306/tcp":{},
"5672/tcp":{},
"6379/tcp":{},
"8080/tcp":{},
"80/tcp":{}
}
}
4、重启docker服务
$ service docker restart
这一步耗时比较久!
5、重启容器
$ docker start centos-desktop-vnc
访问相应的端口就可以使用了!
完