[英]How to export directory created inside the Docker image to the host machine?
The program I'm running inside the Docker image, first creates a directory and writes some file into the directory. 我在Docker镜像中运行的程序首先创建一个目录并将一些文件写入目录。
To transfer the directory onto the host machine, I've mounted a datadir/
and then moved the directory created inside the image into the mounted directory, eg: 为了将目录传输到主机上,我安装了一个
datadir/
然后将图像内创建的目录移动到已安装的目录中,例如:
mkdir datadir
DATADIR=datadir/
docker run -i \
-v $(pwd)/$DATADIR:/$DATADIR/ ubuntu \
bash -c "mkdir /x1 && echo 'abc' > x1/test.txt && mv x1 $DATADIR"
But when I tried to access datadir/x1
, it has root
as the owner and it comes with read-only permissions: 但是当我尝试访问
datadir/x1
,它以root
身份作为所有者,并且它具有只读权限:
$ mv datadir/x1/ .
mv: cannot move 'datadir/x1/' to './x1': Permission denied
$ ls -lah datadir/x1/
total 12K
drwxr-xr-x 2 root root 4.0K Jun 28 16:38 .
drwxrwxr-x 3 alvas alvas 4.0K Jun 28 16:38 ..
-rw-r--r-- 1 root root 4 Jun 28 16:38 test.txt
Is mounting the additional volume and copying the created directory inside the image the right approach to move files between the Docker image and the host machine? 是否正在安装附加卷并在图像中复制创建的目录,以便在Docker镜像和主机之间移动文件? If not, what's the "canonical" way to perform the same operation?
如果没有,那么执行相同操作的“规范”方式是什么?
About the directory permissions, what should be the correct way to assign the host machine permission to any files inside the mounted volume? 关于目录权限, 将主机权限分配给已装入卷内的任何文件的正确方法是什么?
I've tried to chmod -R 777
inside the Docker image but I don't think that's the safe approach, ie: 我试过在Docker镜像中
chmod -R 777
,但我不认为这是安全的方法,即:
$ docker run -i -v $(pwd)/$DATADIR:/$DATADIR/ -i ubuntu bash -c "mkdir /x1 && echo 'abc' > x1/test.txt && mv x1 $DATADIR && chmod -R 777 $DATADIR"
$ mv datadir/x1/ .
$ ls -lah x1
total 12K
drwxrwxrwx 2 root root 4.0K Jun 28 16:47 .
drwxrwxr-x 12 alvas alvas 4.0K Jun 28 16:47 ..
-rwxrwxrwx 1 root root 4 Jun 28 16:47 test.txt
To avoid permission issues use docker cp 要避免权限问题,请使用docker cp
For example: 例如:
# This is the directory you want to save the outputs
mkdir datadir
# We create a directory and file inside it, inside the Docker image.
# And we are naming the Docker image "thisinstance"
docker run -i --name thisinstance ubuntu \
bash -c "mkdir /x1 && echo 'abc' > x1/test.txt"
# Copies the new directory inside the Docker image to the host.
docker cp thisinstance:/x1 datadir/
# Destroy the temporary container
docker rm thisinstance
# Check the ownership of the directory and file
ls -lah datadir/x1/
[out]: [OUT]:
drwxr-xr-x 3 alvas 679754705 102B Jun 29 10:36 ./
drwxr-xr-x 3 alvas 679754705 102B Jun 29 10:36 ../
-rw-r--r-- 1 alvas 679754705 4B Jun 29 10:36 test.t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.