如何获取本地文件系统上的docker容器生成的内容(最小的失败示例) [英] How to get contents generated by a docker container on the local fileystem (minimal failing example)
问题描述
这个问题是另一个问题的最小化版本:
我有以下文件:
./ test
-rw-r - r - 1 miqueladell工作人员114 Jan 21 15:24 Dockerfile
-rw-r - r-- 1 miqueladell staff 90 Jan 21 15:23 docker-compose.yml
drwxr-xr-x 3 miqueladell工作人员102 Jan 21 15:25 html
./test/html:
-rw-r - r-- 1 miqueladell staff 0 Jan 21 15:22 file_from_local_filesystem
DockerFile
FROM php:7.0.2-apache
RUN touch / var / www / html / file_generated_inside_the_container
VOLUME / var / www / html /
docker-compose.yml
code> test:
image:test
卷:
- ./html:/var/www/html/
运行从Dockerfile中定义的图像构建的容器,我想要的是:
./ html
- file_from_local_filesystem
- file_generated_inside_the_container
而不是这样我得到以下内容:
构建图片
$ docker build --no-cache -t test。
发送构建上下文到Docker守护进程4.096 kB
步骤1:FROM php:7.0.2-apache
---> 2f16964f48ba
步骤2:RUN touch / var / www / html / file_generated_inside_the_container
--->运行在b957cc9d7345
---> 5579d3a2d3b2
删除中间容器b957cc9d7345
第3步:VOLUME / var / www / html /
--->运行在6722ddba76cc
---> 4408967d2a98
删除中间容器6722ddba76cc
成功构建4408967d2a98
运行容器与以前的图像
$ docker-compose up -d
创建test_test_1
本地机器文件系统上的列表文件
$ ls -al html
总计0
drwxr-xr-x 3 miqueladell工作人员102 Jan 21 15:25。
drwxr-xr-x 5 miqueladell staff 170 Jan 21 14:20 ..
-rw-r - r-- 1 miqueladell staff 0 Jan 21 15:22 file_from_local_filesystem
容器中的列表文件
$ docker exec -i -t test_test_1 ls -alR / var / www / html
/ var / www / html:
total 4
drwxr-xr-x 1 1000 staff 102 Jan 21 14:25。
drwxr-xr-x 4 root root 4096 Jan 7 18:05 ..
-rw-r - r-- 1 1000 staff 0 Jan 21 14:22 file_from_local_filesystem
来自本地文件系统的卷被装载在容器文件系统上,代替它的内容。
这与本指南的权限和所有权一节中所了解的相反,了解卷
如何获得所需的输出?
谢谢
编辑:正如接受的答复所说,我没有了解卷的问题。卷,作为mountponint,将容器内容替换为已安装的本地文件系统。
我需要的解决方案是使用ENTRYPOINT运行必要的命令来初始化内容容器运行后的安装量。
可以看到发起问题的代码在这里工作:
https://github.com/MiquelAdell/composed_wordpress/tree/1.0.0
这是从您指出的指南
如果您为卷指定主机目录,则不会发生
从其他容器或主机文件系统共享的卷从容器中替换目录。 / p>
如果您需要将一些文件添加到卷,您应该在之后执行启动容器。您可以做一个入口点,例如触摸
,然后运行您的主要流程。
This question is a minimal failing version of this other one:
How to get contents generated by a docker container on the local fileystem
I have the following files:
./test
-rw-r--r-- 1 miqueladell staff 114 Jan 21 15:24 Dockerfile
-rw-r--r-- 1 miqueladell staff 90 Jan 21 15:23 docker-compose.yml
drwxr-xr-x 3 miqueladell staff 102 Jan 21 15:25 html
./test/html:
-rw-r--r-- 1 miqueladell staff 0 Jan 21 15:22 file_from_local_filesystem
DockerFile
FROM php:7.0.2-apache
RUN touch /var/www/html/file_generated_inside_the_container
VOLUME /var/www/html/
docker-compose.yml
test:
image: test
volumes:
- ./html:/var/www/html/
After running a container built from the image defined in the Dockerfile what I want is having:
./html
-- file_from_local_filesystem
-- file_generated_inside_the_container
Instead of this I get the following:
build the image
$ docker build --no-cache -t test .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM php:7.0.2-apache
---> 2f16964f48ba
Step 2 : RUN touch /var/www/html/file_generated_inside_the_container
---> Running in b957cc9d7345
---> 5579d3a2d3b2
Removing intermediate container b957cc9d7345
Step 3 : VOLUME /var/www/html/
---> Running in 6722ddba76cc
---> 4408967d2a98
Removing intermediate container 6722ddba76cc
Successfully built 4408967d2a98
run a container with previous image
$ docker-compose up -d
Creating test_test_1
list files on the local machine filesystem
$ ls -al html
total 0
drwxr-xr-x 3 miqueladell staff 102 Jan 21 15:25 .
drwxr-xr-x 5 miqueladell staff 170 Jan 21 14:20 ..
-rw-r--r-- 1 miqueladell staff 0 Jan 21 15:22 file_from_local_filesystem
list files from the container
$ docker exec -i -t test_test_1 ls -alR /var/www/html
/var/www/html:
total 4
drwxr-xr-x 1 1000 staff 102 Jan 21 14:25 .
drwxr-xr-x 4 root root 4096 Jan 7 18:05 ..
-rw-r--r-- 1 1000 staff 0 Jan 21 14:22 file_from_local_filesystem
The volume from the local filesystem gets mounted on the container file system replacing the contents of it.
This is contrary at what I understand in the section "Permissions and Ownership" of this guide Understanding volumes
How could I get the desired output?
Thanks
EDIT: As is said in the accepted answer I did not understand volumes when asking the question. Volumes, as mountponint, replace the container content with the local filesystem that is mounted.
The solution I needed was to use ENTRYPOINT to run the necessary commands to initialize the contents of the mounted volume once the container is running.
The code that originated the question can be seen working here: https://github.com/MiquelAdell/composed_wordpress/tree/1.0.0
This is from the guide you've pointed to
This won’t happen if you specify a host directory for the volume
Volumes you share from other containers or host filesystem replace directories from container.
If you need to add some files to volume, you should do it after you start container. You can do an entrypoint for example which does touch
and then runs your main process.
这篇关于如何获取本地文件系统上的docker容器生成的内容(最小的失败示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!