简体   繁体   English

"如何将主机目录挂载到正在运行的 docker 容器中"

[英]How to mount a host directory into a running docker container

I want to mount my usb drive into a running docker instance for manually backup of some files.我想将我的 USB 驱动器挂载到正在运行的 docker 实例中,以便手动备份一些文件。

I know of the -v feature of docker run , but this creates a new container.我知道docker run-v功能,但这会创建一个新容器。 Note: its a nextcloudpi container.注意:它是一个 nextcloudpi 容器。

As David Maze mentioned, it's almost impossible to change the volume location of an existing container by using normal docker commands.正如David Maze 所提到的,使用普通的 docker 命令几乎不可能更改现有容器的卷位置。

I found an alternative way that works for me.我找到了一种对我有用的替代方法。 The main idea is convert the existing container to a new docker image and initialize a new docker container on top of it .主要思想是将现有容器转换为新的 docker 镜像,并在其上初始化一个新的 docker 容器 Hope works for you too.希望也适用于你。

 // Create a new image from the container docker commit CONTAINERID NEWIMAGENAME // Create a new container on the top of the new image docker run -v HOSTLOCATION:CONTAINERLOCATION NEWIMAGENAME

You can only change a very limited set of container options after a container starts up.在容器启动后,您只能更改一组非常有限的容器选项 Options like environment variables and container mounts can only be set during the initial docker run or docker create .环境变量和容器挂载等选项只能在初始docker rundocker create期间设置。 If you want to change these, you need to stop and delete your existing container, and create a new one with the new mount option.如果要更改这些,则需要停止并删除现有容器,然后使用新的挂载选项创建一个新容器。

If there's data that you think you need to keep or back up, it should live in some sort of volume mount anyways.如果您认为需要保留或备份某些数据,则无论如何它都应该存在于某种卷安装中。 Delete and restart your container and use a -v option to mount a volume on where the data is kept.删除并重新启动容器,并使用-v选项在保存数据的位置安装卷。 The Docker documentation has an example using named volumes with separate backup and restore containers ; Docker 文档有一个示例,使用带有单独备份和恢复容器的命名卷 or you can directly use a host directory and your normal backup solution there.或者您可以直接在那里使用主机目录和您的常规备份解决方案。 (Deleting and recreating a container as I suggested in the first paragraph is extremely routine, and this shouldn't involve explicit "backup" and "restore" steps.) (正如我在第一段中所建议的那样,删除和重新创建容器是非常常规的,这不应该涉及明确的“备份”和“恢复”步骤。)

If you have data that's there right now that you can't afford to lose, you can docker cp it out of the container before setting up a more robust storage scheme.如果您现在拥有无法丢失的数据,则可以在设置更强大的存储方案之前将其从容器中docker cp出来。

use symlink to the already mounted drive.使用符号链接到已安装的驱动器。

ln -s Source_path targer_path_which_is_already_mounted_on_the_running_docker ln -s Source_path 目标路径_which_is_already_mounted_on_the_running_docker

"

I know the question is from May, but for future searchers:我知道问题来自五月,但对于未来的搜索者:

Create a mounting point on the host filesystem:在主机文件系统上创建一个挂载点:

sudo mkdir /mnt/usb-drive

Run the docker container using the --mount option and set the "bind propagation" to "shared":使用--mount选项运行--mount容器并将“绑定传播”设置为“共享”:

docker run --name mynextcloudpi -it --mount type=bind,source=/mnt/usb-drive,target=/mnt/disk,bind-propagation=shared nextcloudpi

Now you can mount your USB drive to the /mnt/usb-drive directory and it will be mounted to the /mnt/disk location inside the running container.现在您可以将 USB 驱动器挂载到/mnt/usb-drive目录,它将被挂载到正在运行的容器内的/mnt/disk位置。

Eg: sudo mount /dev/sda1 /mnt/usb-drive例如: sudo mount /dev/sda1 /mnt/usb-drive

Change the /dev/sda1, of course.当然,更改/dev/sda1。

More info about bind-propagation: https://docs.docker.com/storage/bind-mounts/#configure-bind-propagation有关绑定传播的更多信息: https : //docs.docker.com/storage/bind-mounts/#configure-bind-propagation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows 主机 Docker + WSL2 - 如何将 Windows 目录挂载到 ZEDC9F0A5A5D57797BF68E373674 容器 - Windows Host Docker + WSL2 - How to mount Windows directory to a Linux container 将 Docker buildx 构建器从主机“挂载”到容器 - "Mount" a Docker buildx builder from host to container 添加到主机的安装点不会显示在正在运行的Docker容器中 - mount points added to the host don't show up in a running docker container 在Docker容器中运行的应用程序无法在Docker主机中访问 - Application running in docker container not reachable in docker host 如何将 nginx docker 的 /etc/nginx/ 目录挂载到主机虚拟机? - How to mount nginx docker's /etc/nginx/ directory to host virtual machine? 在Docker容器中作为主机用户运行 - Running as a host user within a Docker container 将文件从主机复制到容器中正在运行的docker容器中? - Copy a file from host to a running docker container from within the container? 如何使docker运行以将目录从客户端计算机转移到主机容器? - How to get docker run to take the directory from the client machine to the host container? 如何在运行 docker 多容器应用程序的主机上使用 kubeadm 安装 kubernetes - How to install kubernetes using kubeadm on a host walready running docker multi container application 在 docker 容器中挂载 linux 镜像 - Mount linux image in docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM