• 如何修改运行中的docker容器的端口映射和挂载目录


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

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

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

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

    方法二:修改容器配置文件,重启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服务,再启动容器服务就可以了。

    systemctl restart docker

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

  • 相关阅读:
    20172303 2017-2018-2 《程序设计与数据结构》实验一报告
    20172303 2017-2018-2 《程序设计与数据结构》第3周学习总结
    20172303 2017-2018-2 《程序设计与数据结构》第2周学习总结
    20172303 2017-2018-2 《程序设计与数据结构》第1周学习总结
    预备作业03
    预备作业02
    预备作业01
    日语学习笔记
    [Redux] redux之combineReducers
    [Vue] vue跳转外部链接
  • 原文地址:https://www.cnblogs.com/linyouyi/p/10715408.html
Copyright © 2020-2023  润新知