docker对已经启动的容器添加端口映射
已经存在一个容器,容器id为
7a26b01e9789
开放的端口:
4001
5001
8080
1.关闭docker
systemctl stop docker
2.切换到容器的配置目录
cd /var/lib/docker/containers/
ls
cd 7a26b01e978991fbef6e84e9c4081e29e98eb84540bd17ae9c801a67bc57e793/
3.编辑config.v2.json文件
1. 编辑Ports
vim -c ":%! python3 -m json.tool" config.v2.json
config.v2.json 中,输入: :/Ports 回车 可以找
添加:
"Ports": {
"4001/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "4001"
}
],
"5001/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5001"
}
],
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8080"
}
]
},
2. 编辑ExposedPorts
vim -c ":%! python3 -m json.tool" config.v2.json
config.v2.json 中,输入: :/ExposedPorts 回车 可以找
因为我开ipfs的:
添加:
"ExposedPorts": {
"4001/tcp": {},
"5001/tcp": {},
"8080/tcp": {}
},
结果:
4.编辑hostconfig.json文件
vim -c ":%! python3 -m json.tool" hostconfig.json
添加:
"PortBindings": {
"4001/tcp": [
{
"HostIp": "",
"HostPort": "4001"
}
],
"5001/tcp": [
{
"HostIp": "",
"HostPort": "5001"
}
],
"8080/tcp": [
{
"HostIp": "",
"HostPort": "8080"
}
]
},
这个的作用是用json的格式去看,要不然就是一连串字符串
:%! python3 -m json.tool
参考文献:
https://developer.aliyun.com/article/1080541