Zhou Study hard, improve every day.

docker修改已经运行的容器的端口

2020-04-27
本文 5145 字,阅读全文约需 15 分钟

docker修改已经运行的容器的端口

一定要主机操作顺序,我操作了很久没成功 主要就是因为操作顺序问题

1.停止容器

$ docker stop containerId

2.停止docker服务

$ service docker stop

3.修改文件

#/var/lib/docker/containers/[name]/hostconfig.json

#修改里面的端口设置(也需要修改config.v2.json文件)

hostconfig.json(主要要修改PortBindings HostIp为宿主机ip即可)

{
	"Binds": null,
	"ContainerIDFile": "",
	"LogConfig": {
		"Type": "json-file",
		"Config": {}
	},
	"NetworkMode": "default",
	"PortBindings": {
		"28080/tcp": [{
			"HostIp": "10.90.0.1",
			"HostPort": "28080"
		}],
		"28080/udp": [{
			"HostIp": "10.90.0.1",
			"HostPort": "28080"
		}],
		"5432/tcp": [{					//需要开放的端口
			"HostIp": "10.90.0.1",
			"HostPort": "5432"			//宿主机对应的端口
		}],
		"5432/udp": [{
			"HostIp": "10.90.0.1",
			"HostPort": "5432"
		}]
	},
	"RestartPolicy": {
		"Name": "no",
		"MaximumRetryCount": 0
	},
	"AutoRemove": false,
	"VolumeDriver": "",
	"VolumesFrom": null,
	"CapAdd": null,
	"CapDrop": null,
	"Capabilities": null,
	"Dns": [],
	"DnsOptions": [],
	"DnsSearch": [],
	"ExtraHosts": null,
	"GroupAdd": null,
	"IpcMode": "private",
	"Cgroup": "",
	"Links": null,
	"OomScoreAdj": 0,
	"PidMode": "",
	"Privileged": true,
	"PublishAllPorts": false,
	"ReadonlyRootfs": false,
	"SecurityOpt": ["label=disable"],
	"UTSMode": "",
	"UsernsMode": "",
	"ShmSize": 67108864,
	"Runtime": "runc",
	"ConsoleSize": [0, 0],
	"Isolation": "",
	"CpuShares": 0,
	"Memory": 0,
	"NanoCpus": 0,
	"CgroupParent": "",
	"BlkioWeight": 0,
	"BlkioWeightDevice": [],
	"BlkioDeviceReadBps": null,
	"BlkioDeviceWriteBps": null,
	"BlkioDeviceReadIOps": null,
	"BlkioDeviceWriteIOps": null,
	"CpuPeriod": 0,
	"CpuQuota": 0,
	"CpuRealtimePeriod": 0,
	"CpuRealtimeRuntime": 0,
	"CpusetCpus": "",
	"CpusetMems": "",
	"Devices": [],
	"DeviceCgroupRules": null,
	"DeviceRequests": null,
	"KernelMemory": 0,
	"KernelMemoryTCP": 0,
	"MemoryReservation": 0,
	"MemorySwap": 0,
	"MemorySwappiness": null,
	"OomKillDisable": false,
	"PidsLimit": null,
	"Ulimits": null,
	"CpuCount": 0,
	"CpuPercent": 0,
	"IOMaximumIOps": 0,
	"IOMaximumBandwidth": 0,
	"MaskedPaths": null,
	"ReadonlyPaths": null
}

config.v2.json(如果需要修改 主要为Ports、ExposedPorts)

{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":21012,"ExitCode":0,"Error":"","StartedAt":"2020-04-24T02:06:20.62012849Z","FinishedAt":"2020-04-24T02:05:35.758052158Z","Health":null},"ID":"fe47b64de329b21d2f69d789151248d24e01a30dbcbd03a962955b42f2c65af7","Created":"2020-04-23T14:36:34.469962803Z","Managed":false,"Path":"/usr/sbin/init","Args":[],"Config":{"Hostname":"master1","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":{"28080/tcp":{},"28080/udp":{},"5432/tcp":{},"5432/udp":{}},"Tty":true,"OpenStdin":true,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/usr/sbin/init"],"Image":"centos-gpdb","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{"org.label-schema.build-date":"20191001","org.label-schema.license":"GPLv2","org.label-schema.name":"CentOS Base Image","org.label-schema.schema-version":"1.0","org.label-schema.vendor":"CentOS"}},"Image":"sha256:64d7d2136f6c802e5d4fb06f0be46ef668f8c04291d748fe849ec1af2f6baa13","NetworkSettings":{"Bridge":"","SandboxID":"456dad6faf8ba36e07c70f805cca9d8d50e43341f4d0fc65eb76cd6394b6f850","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"2bee103b8441cc7e2c84e090ab66649600477f54270aebd3b5c229d2db88030b","EndpointID":"6627431afefe8f1bff2769036eae08e8355fd5676a77954256ce2a2dbc3a38d1","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null,"IPAMOperational":false}},"Service":null,"Ports":{"28080/tcp":[{"HostIp":"10.90.0.1","HostPort":"28080"}],"28080/udp":[{"HostIp":"10.90.0.1","HostPort":"28080"}],"5432/tcp":[{"HostIp":"10.90.0.1","HostPort":"5432"}],"5432/udp":[{"HostIp":"10.90.0.1","HostPort":"5432"}]},"SandboxKey":"/var/run/docker/netns/456dad6faf8b","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/fe47b64de329b21d2f69d789151248d24e01a30dbcbd03a962955b42f2c65af7/fe47b64de329b21d2f69d789151248d24e01a30dbcbd03a962955b42f2c65af7-json.log","Name":"/master1","Driver":"overlay2","OS":"linux","MountLabel":"","ProcessLabel":"","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{},"SecretReferences":null,"ConfigReferences":null,"AppArmorProfile":"unconfined","HostnamePath":"/var/lib/docker/containers/fe47b64de329b21d2f69d789151248d24e01a30dbcbd03a962955b42f2c65af7/hostname","HostsPath":"/var/lib/docker/containers/fe47b64de329b21d2f69d789151248d24e01a30dbcbd03a962955b42f2c65af7/hosts","ShmPath":"","ResolvConfPath":"/var/lib/docker/containers/fe47b64de329b21d2f69d789151248d24e01a30dbcbd03a962955b42f2c65af7/resolv.conf","SeccompProfile":"","NoNewPrivileges":false}

4.启动docker服务 启动容器

$ service docker stop
$ docker stop containerId

https://stackoverflow.com/questions/19335444/how-do-i-assign-a-port-mapping-to-an-existing-docker-container


上一篇 docker安装gp

Comments

Content