如何创建Docker卷设备/主机路径 [英] How to create docker volume device/host path
问题描述
我相信有一种简单的方法可以将文件复制到已经安装到容器的Docker卷中。
I believe there is an easy way to copy files into a docker volume that has already been mounted to a container.
docker cp / tmp / my_data /。 my_container:/ my_data
由如何将多个文件复制到Docker数据卷中
但是,如何使用 docker卷创建一个命名卷--name my-volume
已经有文件了?
我读过,将cp文件放入 {{。Mountpoint}}
不是一个好主意。
But, how does one create a named volume using docker volume create --name my-volume
that already has files in it?
I have read that it's not a good idea to cp files into the {{.Mountpoint}}
.
我是Docker的新手,并且都是nuancies,因此,如果我对卷的基本理解不正确,深表歉意。
I'm new to docker and all of it's nuancies, so apologies if my fundamental understanding of volumes is incorrect.
推荐答案
方法1-COPY
要将文件从主机复制到容器
To copy the file from the host to the container
docker cp /path/of/the/file <Container_ID>:/path/of/he/container/folder
上述方法的问题是,当您删除卷,文件或目录时,它不会持久化容器将丢失。建议仅针对临时目的。
Issue with the above approch is, it will not persists the volume or file or dir, as you remove the container it will be lost. This is suggested only for temporary pupose.
方法#2-卷挂载
将卷从主机移动到容器
第一步:使用自定义路径创建卷
Step1: Create the volume with the custom path
docker volume create --name my_test_volume --opt type=none --opt device=/home/jinna/Jinna_Balu/Test_volume --opt o=bind
Step2:安装到容器或群集服务
Step2 : Mount to the container or swarm service
docker run -d \
--name devtest \
--mount source=my_test_volume,target=/app \
nginx:1.11.8-alpine
我们可以使用以下.yaml文件完成上述两个步骤
We can do both of the above steps with below .yaml files
version: '3'
services:
nginx:
image: nginx:1.11.8-alpine
ports:
- "8081:80"
volumes:
- my_test_volume:/usr/share/app
volumes:
my_test_volume:
driver: local
driver_opts:
o: bind
type: none
device: /home/jinna/Jinna_Balu/Test_volume
使用docker-compose运行上述yml
RUN the above yml with docker-compose
docker-compose up -d
注意:在执行docker-compose之前先创建文件夹路径。
良好做法是将文件更改为维护文件持久性。
Good practice to have files mouted to maintain the persistency.
这篇关于如何创建Docker卷设备/主机路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!