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

How to get contents generated by a docker container on the local fileystem (minimal failing example)

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

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

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/

运行从 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

来自本地文件系统的卷被装载到容器文件系统上并替换它的内容。

这与我在本指南 "Permissions and Ownership" 部分的理解相反 Understanding volumes

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

谢谢


编辑: 正如接受的答案中所说,我在问这个问题时不明白卷数。卷,作为 mountponint,用挂载的本地文件系统替换容器内容。

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

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

是的,很确定它应该是完整路径:

docker-compose.yml

test:
  image: test
  volumes:
     - ./html:/var/www/html/

./html 应该是 /path/to/html

编辑

更改为完整路径后的输出 运行 test.sh:

$ docker exec -ti dockervolumetest_test_1 bash
root@c0bd7a722b63:/var/www/html# ls -la
total 8
drwxr-xr-x 2 1000 adm  4096 Jan 21 15:19 .
drwxr-xr-x 3 root root 4096 Jan  7 18:05 ..
-rw-r--r-- 1 1000 adm     0 Jan 21 15:19 file_from_local_filesystem

编辑 2

抱歉,我误解了问题的全部前提:)

因此,您正在尝试将 file_generated_inside_the_container(仅在 docker 映像中创建)安装到主机上的某个位置 - 例如 "reverse mount".

使用任何 docker 命令都不可能做到这一点,但如果您只想访问主机上的 VOLUMEs 文件,您可以在docker 根目录(通常为 /var/lib/docker)。要找到文件的确切位置,您可以使用 docker inspect [container_id],或者在最新版本中使用 docker API.

参见 cpuguy 在这个 github 问题中的回答:https://github.com/docker/docker/issues/12853#issuecomment-123953258 了解更多详情。

这是来自您指向的指南

This won’t happen if you specify a host directory for the volume

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

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