最近有个需求需要修改docker容器的端口映射 在此记录一下常用的两种方法(不限于这两种)

此文档在Centos7.X 、Dcoekr:18.09.3环境下操作

方法一

删除原有容器 重新创建新容器

如果在容器做过修改而且还没有映射到本地的话 此操作可能会导致一些配置清除

原本容器

1
2
3
4
5
[root@k8s-77-40 ~]#  docker run -itd --name nginx-test -p 8100:80 -p 8200:443 nginx:latest /bin/bash
f1b3c43f7848b380197f72c310900a772a965d36f827ce97b197dacda158b8ca
[root@k8s-77-40 ~]# docker ps
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                         NAMES
f1b3c43f7848        nginx:latest                             "/bin/bash"              22 seconds ago      Up 21 seconds       0.0.0.0:8100->80/tcp, 0.0.0.0:8200->443/tcp   nginx-test

进行修改

注意:创建新容器时只需要更改-p后需要更改的端口,其他的参数不要更改

1
2
3
4
5
6
7
8
9
[root@k8s-77-40 ~]# docker stop nginx-test
nginx-test
[root@k8s-77-40 ~]# docker rm nginx-test
nginx-test
[root@k8s-77-40 ~]#  docker run -itd --name nginx-test -p 8300:80 -p 8400:443 nginx:latest /bin/bash
e9b7decf6d0c8d770cd3cf785355e3508fe9ead121652855031e0336aa11a1f3
[root@k8s-77-40 ~]# docker ps
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                         NAMES
e9b7decf6d0c        nginx:latest                             "/bin/bash"              7 seconds ago       Up 6 seconds        0.0.0.0:8300->80/tcp, 0.0.0.0:8400->443/tcp   nginx-test

方法二

修改容器配置文件,重启docker服务

原容器

1
2
3
4
5
[root@k8s-77-40 ~]#  docker run -itd --name nginx-test -p 8100:80 -p 8200:443 nginx:latest /bin/bash
a0623cc668aa0a841a4a2daffab5210c7fa5485517c544b443b19bb661905a54  
[root@k8s-77-40 ~]# docker ps   
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                         NAMES
a0623cc668aa        nginx:latest                             "/bin/bash"              5 minutes ago       Up 5 minutes        0.0.0.0:8100->80/tcp, 0.0.0.0:8200->443/tcp   nginx-test

进行修改

  • 查看容器的存储目录
1
2
3
#默认的目录是/var/lib/docker,如果修改过就去对应的目录修改
[root@k8s-77-40 ~]# sudo docker info | grep "Docker Root Dir" 
Docker Root Dir: /var/lib/docker
  • 停止对应容器

此步骤很重要 ,容器运行时修改 再重启docker后不会生效

1
2
[root@k8s-77-40 ~]# docker stop nginx-test
nginx-test
  • 进入容器配置文件目录 进行修改

/var/lib/docker/containers/[容器id]/hostconfig.json 容器ID用docker ps就能查看

1
2
3
4
5
[root@k8s-77-40 ~]# cd /var/lib/docker/containers/a0623cc668aa0a841a4a2daffab5210c7fa5485517c544b443b19bb661905a54
[root@k8s-77-40 a0623cc668aa0a841a4a2daffab5210c7fa5485517c544b443b19bb661905a54]#vim hostconfig.json
...      #修改HostPost的对应值
"PortBindings":{"443/tcp":[{"HostIp":"","HostPort":"8500"}],"80/tcp":[{"HostIp":"","HostPort":"8600"}]},
...
  • 重启docker 重启容器
1
2
3
4
5
6
[root@k8s-77-40 ~]# systemctl restart docker
[root@k8s-77-40 ~]# docker start nginx-test
nginx-test
[root@k8s-77-40 ~]# docker ps
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                                         NAMES
a0623cc668aa        nginx:latest                             "/bin/bash"              17 minutes ago      Up 3 seconds        0.0.0.0:8600->80/tcp, 0.0.0.0:8500->443/tcp   nginx-test