如何获取本地文件系统上 docker 容器生成的内容(最小失败示例)

2024-05-10

这个问题是另一个问题的最小失败版本:

如何获取本地文件系统上docker容器生成的内容 https://stackoverflow.com/questions/34924011/how-to-get-contents-generated-by-a-docker-container-on-the-local-fileystem

我有以下文件:

./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

Docker文件

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/

运行根据 Dockerfile 中定义的映像构建的容器后,我想要的是:

./html
-- file_from_local_filesystem
-- file_generated_inside_the_container

相反,我得到以下内容:

建立形象

$ 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

使用之前的镜像运行容器

$ docker-compose up -d

Creating test_test_1

列出本地计算机文件系统上的文件

$ 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

列出容器中的文件

$ 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

本地文件系统中的卷被安装到容器文件系统上,替换其中的内容。

这与我在本指南“权限和所有权”部分中的理解相反了解体积 http://container-solutions.com/understanding-volumes-docker/

我怎样才能得到想要的输出?

Thanks


EDIT:正如已接受的答案中所述,我在提出问题时并不理解体积。卷(如 mountponint)将容器内容替换为已安装的本地文件系统。

我需要的解决方案是使用 ENTRYPOINT 运行必要的命令,以在容器运行后初始化已安装卷的内容。

可以在此处看到引发问题的代码:https://github.com/MiquelAdell/composed_wordpress/tree/1.0.0 https://github.com/MiquelAdell/composed_wordpress/tree/1.0.0


这是来自您指出的指南

如果您为卷指定主机目录,则不会发生这种情况

您从其他容器或主机文件系统共享的卷会替换容器中的目录。

如果您需要将一些文件添加到卷中,您应该这样做after你启动容器。例如,您可以做一个入口点touch然后运行你的主进程。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取本地文件系统上 docker 容器生成的内容(最小失败示例) 的相关文章

随机推荐