发现一个正在跑的容易少定义了一个端口映射,删除重建肯定是不可取的。
docker命令目前还没有动态添加的功能,只能自己去查阅资料解决了。
目前,有三种办法:
$ docker stop httpd-container
httpd-container
$ docker commit httpd-container httpd-image
sha256:d43a3fcad051c90ac9b7dea9b2ddaodsa9o3d6dbdsadsc6893ef4r430
然后删除老的,再运行出一个新的:
$ docker rm httpd-container
httpd-container
$ docker run -d -p 83:80 --name httpd-container httpd-image
8ds35c477ad74e80b3642abcrf45f91bf20cd3f0cbf3asd
查看防火墙上目前的规则:
sudo iptables -t nat -vnL
可以看到:
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
1265 68587 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:3307 to:10.244.192.59:3306
24283 1440K DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:6379 to:10.244.192.65:6379
1238 69268 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9300 to:10.244.192.66:9300
3759 201K DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:9200 to:10.244.192.66:9200
282 14700 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:27018 to:10.244.192.67:27017
29 1568 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:6080 to:10.244.192.68:80
再获得容器的IP地址:
docker inspect containerId |grep IPAddress
然后添加端口映射:
sudo iptables -t nat -A DOCKER -p tcp --dport 9222 -j DNAT --to-destination 10.244.192.68:9222
sudo vim /var/lib/docker/containers/<容器Id>/hostconfig.json
{
...
...
"PortBindings": {"9222/tcp":[{"HostIp":"","HostPort":"9222"}]},
...
...
}
再修改/var/lib/docker/containers/<容器Id>/config.v2.json文件:
{
...
"Config":
{
...
"ExposedPorts":
{
"80/tcp":{},
"82/tcp":{},
"8080/tcp":{}
},
...
}
}
再重启docker服务:
systemctl start docker
最后重启容器即可。