首页 文章

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

提问于
浏览
2

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

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

取而代之的是我得到以下内容:

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

来自本地文件系统的卷将安装在容器文件系统上,替换它的内容 .

这与我在本指南"Permissions and Ownership"一节中所理解的相反Understanding volumes

How could I get the desired output?

谢谢


EDIT: 正如在接受的答案中所说,我在提问时并不理解卷 . 卷,作为mountponint,将容器内容替换为已挂载的本地文件系统 .

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

发现问题的代码可以在这里工作:https://github.com/MiquelAdell/composed_wordpress/tree/1.0.0

2 回答

  • 1

    这是你指出的指南

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

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

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

  • 0

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

    泊坞窗,compose.yml

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

    ./html 应为 /path/to/html

    Edit

    更改为完整路径并运行 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
    

    Edit 2

    对不起,我误解了问题的全部前提:)

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

    在访问主机上的 VOLUME s文件后,这不是't possible to do with any docker commands, but if all you',您可以在docker根目录中找到这些文件(通常为 /var/lib/docker ) . 要查找文件的确切位置,可以使用 docker inspect [container_id] ,或者在最新版本中使用docker API .

    有关更多详细信息,请参阅此github问题中的cpuguy答案:https://github.com/docker/docker/issues/12853#issuecomment-123953258 .

相关问题