如何修改运行中的docker镜像的端口映射及挂载文件

在docker run创建并运行容器的时候,可以通过-p指定端口映射规则。但是,我们经常会遇到刚开始忘记设置端口映射或者设置错了需要修改。当docker start运行容器后并没有提供一个-p选项或设置,让你修改指定端口映射规则。那么这种情况我们该怎么处理呢?

方法一:删除原有容器,重新建新容器

这个解决方案最为简单,把原来的容器删掉,重新建一个。当然这次不要忘记加上端口映射。

优缺点:优点是简单快捷,在测试环境使用较多。缺点是如果是数据库镜像,那重新建一个又要重新配置一次,就比较麻烦了。

方法二:修改容器配置文件,重启docker服务
  • 容器的配置文件路径:
  1. /var/lib/docker/containers/[hash_of_the_container]/hostconfig.json

其中的hashofthecontainer是docker镜像的hash值,可以通过docker ps或者docker inspect containername查看。(CONTAINER ID就可以看出来)

[root@ecs-5286 ~]# docker ps
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS                  PORTS                                                                  NAMES
f8511e56585f        gitlab/gitlab-ce:latest   "/assets/wrapper"   8 months ago        Up 16 hours (healthy)   0.0.0.0:10022->22/tcp, 0.0.0.0:8080->80/tcp, 0.0.0.0:10443->443/tcp   gitlab-ce
[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 ~]#

按需修改端口, 按需修改挂载目录(权限和属主属组要相同),然后重启docker服务,再启动容器服务就可以了。

优缺点:这个方法的优点是没有副作用,操作简单。缺点是需要重启整个docker服务,如果在同一个宿主机上运行着多个容器服务的话,就会影响其他容器服务。

  • 如果之前没有端口映射, 应该有这样的一段:
"PortBindings":{}
// 增加一个映射, 这样写:
"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"8080"}]}
  • 前一个数字是容器端口, 后一个是宿主机端口。将宿主机的8080端口映射到容器的80端口
  • 而修改现有端口映射更简单, 把端口号改掉就行。

如果config.v2.json里面也记录了端口,也要修改

"Ports":{
    "80/tcp":[
        {
          "HostIp":"0.0.0.0",
          "HostPort":"8080"
        }
    ]
},
  • 如果之前没有挂载目录
"Binds":null,
// 增加一个挂载目录即可
"Binds":["/home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf","/home/nginx/html:/usr/share/nginx/html","/home/nginx/log:/var/log/nginx"],

你可能感兴趣的:(如何修改运行中的docker镜像的端口映射及挂载文件)