简体   繁体   English

如何将Docker镜像内创建的目录导出到主机?

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

相关问题 将 docker 容器内的变量导出到主机 - Export a variable from inside the docker container to host 如何使docker运行以将目录从客户端计算机转移到主机容器? - How to get docker run to take the directory from the client machine to the host container? 如何将 nginx docker 的 /etc/nginx/ 目录挂载到主机虚拟机? - How to mount nginx docker's /etc/nginx/ directory to host virtual machine? 如何在节点 js docker 容器外(即在 linux 主机内)保存上传的文件? - How to save uploaded files outside node js docker container (i.e. inside linux host machine)? Docker:卷中映射的目录不是与主机的同一用户创建的 - Docker : directory mapped in volume is not created with same user of host Docker容器内部和主机中的其他文件所有者 - Different file owner inside Docker container and in host machine "如何将主机目录挂载到正在运行的 docker 容器中" - How to mount a host directory into a running docker container 如何阻止与在主机上的 docker 容器中创建的 IP 的连接 - How to block the connection to an IP which is created in docker container on host 如何确定linux机器目录中最新创建的文件? - How do I determine the newest created file in a directory on a linux machine? 从主机Web浏览器访问驻留在Docker容器内的apache2 - Accessing apache2 residing inside Docker container from Host machine web browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM