参考文章:如何修改运行中的docker容器的端口映射和挂载目录
这个解决方案最为简单,把原来的容器删掉,重新建一个。当然这次不要忘记加上端口映射。
优缺点:优点是简单快捷,在测试环境使用较多。缺点是如果是数据库镜像,那重新建一个又要重新配置一次,就比较麻烦了。
/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json
/var/lib/docker/containers/[hash_of_the_container]/config.v2.json
其中的hashofthecontainer是docker镜像的hash值,通过docker inspect [containername] | grep HostnamePath 查看。
docker inspect -f "{{.Mounts}}" [containername]
[root@ecs-5286 ~]# docker inspect gitlab-ce | grep HostnamePath
"HostnamePath": "/var/lib/docker/containers/f8511e56585fdd68d4bb5ad57250d829bac52626d722305a43dec1e5cf18406c/hostname",
[root@ecs-5286 ~]#
[root@ecs-5286 ~]#
#查看绑定目录
[root@ecs-5286 ~]# docker inspect -f "{{.Mounts}}" gitlab-ce
[{bind /cct_data/dpkg_data /data true rprivate} {bind /cct_data/dpkg_ksp /opt true rprivate} {volume 620ff1997b2482433ec3777d256327a9c4fb06c377381da31ab637913382978b /var/lib/docker/volumes/620ff1997b2482433ec3777d256327a9c4fb06c377381da31ab637913382978b/_data /sys/fs/cgroup local true }]
[root@ecs-5286 ~]#
[root@ecs-5286 ~]#
[root@ecs-5286 ~]# cat /var/lib/docker/containers/f8511e56585fdd68d4bb5ad57250d829bac52626d722305a43dec1e5cf18406c/hostconfig.json
{"Binds":["/srv/gitlab/logs:/var/log/gitlab","/srv/gitlab/data:/var/opt/gitlab","/srv/gitlab/config:/etc/gitlab"],"ContainerIDFile":"","LogConfig":{"Type":"journald","Config":{}},"NetworkMode":"default","PortBindings":{"22/tcp":[{"HostIp":"","HostPort":"10022"}],"443/tcp":[{"HostIp":"","HostPort":"10443"}],"80/tcp":[{"HostIp":"","HostPort":"10080"}]},"RestartPolicy":{"Name":"always","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"","Cgroup":"","Links":[],"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"docker-runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":null,"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":-1,"OomKillDisable":false,"PidsLimit":0,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0}
[root@ecs-5286 ~]#
[root@ecs-5286 ~]#
[root@ecs-5286 ~]# cat /var/lib/docker/containers/f8511e56585fdd68d4bb5ad57250d829bac52626d722305a43dec1e5cf18406c/config.v2.json
....
按需修改端口, 按需修改挂载目录(权限和属主属组要相同),然后重启docker服务,再启动容器服务就可以了。
systemctl restart docker
优缺点:这个方法的优点是没有副作用,操作简单。缺点是需要重启整个docker服务,如果在同一个宿主机上运行着多个容器服务的话,就会影响其他容器服务。