Skip to content

Instantly share code, notes, and snippets.

@nanmi
Last active November 23, 2021 10:43
Show Gist options
  • Save nanmi/88d1b05cb3582b30b7ac134bc10e03cd to your computer and use it in GitHub Desktop.
Save nanmi/88d1b05cb3582b30b7ac134bc10e03cd to your computer and use it in GitHub Desktop.
How to modify port when container running

容器启动后增加 端口映射 和 挂载目录

增加 端口映射

停止docker服务(一定要,否则修改会自动还原)
sudo systemctl stop docker

修改/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json/var/lib/docker/containers/[hash_of_the_container]/config.v2.json

修改hostconfig.json
"NetworkMode":"default",
	"PortBindings":{
		"10002/tcp":[{             // "10002/tcp" 容器端口
			"HostIp":"",
			"HostPort":"10002"     // "10002" 宿主机端口
			}],
      	....
	}
修改config.v2.json

这个文件要修改俩个地方 第一个地方

"Config":{
	"Hostname":"f3d34c2e8a16",
	"Domainname":"",
	"User":"",
	"AttachStdin":false,
	"AttachStdout":false,
	"AttachStderr":false,
  -------------------------------
	"ExposedPorts":{
		"10002/tcp":{}        //增加这部分内容,如果没有的话,这个地方是暴露的容器的端口
		.....
		},
 --------------------------------
	"Tty":true,
	"OpenStdin":true,
	"StdinOnce":false,
}

第二个地方

"Service":null,
"Ports":{
	"10002/tcp":[{            //容器端口
		"HostIp":"0.0.0.0",
		"HostPort":"10002"    //宿主机端口
		}]
	},
"SandboxKey":"/var/run/docker/netns/b30d214a0bf1"
启动docker服务
sudo systemctl start docker

增加 挂载目录

停止docker服务(一定要,否则修改会自动还原)
sudo systemctl stop docker

修改/var/lib/docker/containers/[hash_of_the_container]/config.v2.json

修改config.v2.json
"MountPoints":{
	"/root/workspace":{                //容器目录
		"Source":"/home/nvidia/data/zhangbo",   //宿主机目录
		"Destination":"/root/workspace",        //容器目录
		"RW":true,
		"Name":"",
		"Driver":"",
		"Type":"bind",
		"Propagation":"rprivate",
		"Spec":{
			"Type":"bind",
			"Source":"/home/nvidia/data/zhangbo",  //宿主机目录
			"Target":"/root/workspace"             //容器目录
			},
		"SkipMountpointCreation":false
		}
	},
"SecretReferences":null, ...
启动docker服务
sudo systemctl start docker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment