docker 修改挂载目录

After create container,if you want to change the mount directory.

方式一:修改原始 container’s config

首先看一看你容器现在有哪些挂载路径

docker inspect -f "{{.Mounts}}" <CONTAINER_NAME>

然后用 docker ps -a 看看你的容器 ID:

docker ps -a

在这里插入图片描述

可以看到我的容器的 ID 为 2f18dce34e92。

把 docker 服务停止了:

systemctl stop docker.service

/var/lib/docker/containers/container-ID/config.v2.json 中找到 MountPoints,并修改挂载路径

vim /var/lib/docker/containers/container-ID/config.v2.json
"MountPoints":{"/home":{"Source":"/path/to/your/host","Destination":"/home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/path/to/your/host","Target":"/home"}}}

按照如上配置后:查看docker inspect <container_name> | grep Mounts -A 20

img

重启 docker 服务

systemctl start docker.service

重启 docker 容器

docker start <container-name/ID>
docker attach <container-name/ID>

大功告成~

方式二:提交现有容器为新镜像,然后重新运行它

$ docker ps  -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                          PORTS               NAMES
   5a3422adeead        ubuntu:14.04          "/bin/bash"              About a minute ago   Exited (0) About a minute ago                       agitated_newton
$ docker commit 5a3422adeead newimagename
$ docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash

然后停止旧容器,并使用这个新容器,如果由于某种原因需要新容器使用旧名称,请在删除旧容器后使用docker rename

方式三:export容器为镜像,然后import为新镜像

$docker container export -o ./myimage.docker 容器ID
$docker import ./myimage.docker newimagename
$docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash

然后停止旧容器,并使用这个新容器,如果由于某种原因需要新容器使用旧名称,请在删除旧容器后使用docker rename。

参考 https://blog.csdn.net/m0_37870649/article/details/120470550

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐