docker中如何修改已创建的容器端口映射

我们平时在使用docker中

首先pull镜像

然后创建容器,由于各种原因,经常会漏了一些`端口映射到主机`

有什么办法呢?

当然最简单的办法,就是将当前的容器`commit到一个新的镜像`,然后再次生成容器,这个时候加上新的端口

感觉很麻烦

有没有别的办法呢?答案肯定是有

1 关闭docker

2 关闭容器

3 进入到容器对应的文件夹

以下是我的

>root@ip-172-31-4-167:/var/lib/docker/containers/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92#

然后修改两个json文件

>hostconfig.json
config.v2.json

```
{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"80"}],"8888/tcp":[{"HostIp":"","HostPort":"8888"}]},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"shareable","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"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,"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":0,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}
```

修改里面的`PortBindings`,新增`{"HostIp":"","HostPort":"80"}],`

```
{"StreamConfig":{},"State":{"Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"RemovalInProgress":false,"Dead":false,"Pid":5543,"ExitCode":0,"Error":"","StartedAt":"2021-03-26T16:04:56.670724302Z","FinishedAt":"2021-03-26T16:02:55.013963723Z","Health":null},"ID":"9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92","Created":"2021-03-18T03:39:33.891540269Z","Managed":false,"Path":"/bin/bash","Args":[],"Config":{"Hostname":"9da18ce89f2b","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":{"80/tcp":{},"8888/tcp":{}},"Tty":true,"OpenStdin":true,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/bin/bash"],"Image":"centos:centos7","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{"org.label-schema.build-date":"20201113","org.label-schema.license":"GPLv2","org.label-schema.name":"CentOS Base Image","org.label-schema.schema-version":"1.0","org.label-schema.vendor":"CentOS","org.opencontainers.image.created":"2020-11-13 00:00:00+00:00","org.opencontainers.image.licenses":"GPL-2.0-only","org.opencontainers.image.title":"CentOS Base Image","org.opencontainers.image.vendor":"CentOS"}},"Image":"sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf","NetworkSettings":{"Bridge":"","SandboxID":"a66c98cd44a37beda24a4214373bf76b0bee2d62c7dd715f39aad2821d8d4d30","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"314f543e598e399ae8c9f91d704b1a1f91727f0b555ed4e881f48704b6dfd8d9","EndpointID":"02b03de07b6f8fa4500fc5086ee423b67e2d16094b8ab8a48b9bc9ab5f366b71","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":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"80"}],"8888/tcp":[{"HostIp":"0.0.0.0","HostPort":"8888"}]},"SandboxKey":"/var/run/docker/netns/a66c98cd44a3","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"IsAnonymousEndpoint":false,"HasSwarmEndpoint":false},"LogPath":"/var/lib/docker/containers/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92-json.log","Name":"/centos7-bt","Driver":"overlay2","OS":"linux","MountLabel":"","ProcessLabel":"","RestartCount":0,"HasBeenStartedBefore":true,"HasBeenManuallyStopped":false,"MountPoints":{},"SecretReferences":null,"ConfigReferences":null,"AppArmorProfile":"docker-default","HostnamePath":"/var/lib/docker/containers/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92/hostname","HostsPath":"/var/lib/docker/containers/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92/hosts","ShmPath":"/var/lib/docker/containers/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92/mounts/shm","ResolvConfPath":"/var/lib/docker/containers/9da18ce89f2b24a61302272a24c4ae13311527bf007d7307f47c6ddd94ccca92/resolv.conf","SeccompProfile":"","NoNewPrivileges":false}
```

修改`ExposedPorts`新增`"80/tcp":{},`

然后重启docker,重启容器即可将自己想要的端口映射到主机

可以将文件的内容复制粘贴到`json.cn`方便自己的修改

    A+
发布日期:2021年03月27日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: