[英]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 run
或docker 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.