Docker - way to modify folder mapping

why why 2022-08-06 18:33:01 阅读数:11


Docker After building the container,The configuration is not easy to modify,This article records the method of modifying the folder mapping of the established container.


  • The container has been created,Initial port mapping information is configured
  • But the folder mapping is wrong/多了/少了,需要调整



  • Pass the current container through docker commit 命令生成镜像
  • Rebuild the container from the image,Build with the correct folder mapping

The benefit is a cure for all ailments,正规途径 The disadvantage is that the process is cumbersome,不够优雅


Container information such as port mapping is stored somewhere,Managed by configuration files,We only need to modify the port information of the configuration file to complete the rewriting

  • Take my current nginx 容器为例
  • View the container information that needs to be modified
# docker ps
070465ca928b nextcloud "/ apac…" 2 weeks ago Up 6 minutes>80/tcp, :::8080->80/tcp,>443/tcp, :::8443->443/tcp nextcloud

  • The current container name nextcloud,CONTAINER ID070465ca928b
  • There are no mapped folders,Now I'm going to add one /data -> /data 的端口映射
  1. 停止容器
docker stop nextcloud

  1. 停止 docker 服务
systemctl stop docker
  1. 进入文件夹 /var/lib/docker/containers/{CONTAINER ID}

CONTAINER ID Container-related configuration is in it ID in the corresponding subdirectory

cd /var/lib/docker/containers/070465ca928b...

  1. 当前目录文件
# ls
5190850b4fcc...-json.log config.v2.json hostname mounts resolv.conf.hash
checkpoints hostconfig.json hosts resolv.conf

我们需要修改的是 hostconfig.jsonconfig.v2.json 两个文件

  1. 修改 hostconfig.json
  • 在查看内容时,可以发现 json 中 BindsThe content corresponding to the key value is the folder map,将其改为
  • 修改 config.v2.json
  • 同理在 MountPoints content under key:
  1. 开启docker 服务
systemctl start docker

  1. 运行容器
docker start nginx
  • At this point, enter the container and find that the folder has been successfully mapped


copyright:author[why why],Please bring the original link to reprint, thank you.