如何获取本地文件系统上的docker容器生成的内容 [英] How to get contents generated by a docker container on the local fileystem

查看:620
本文介绍了如何获取本地文件系统上的docker容器生成的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢所有对于建议!


TL; DR:你能指出一个简单的例子将
的更改写入容器和主机之间共享的卷
文件系统的映像?

TL;DR: can you point me to a simple example of an image that writes changes to a volume shared between the container and the host filesystem?

我有一些麻烦,了解卷的工作原理。

I have some trouble understanding how volumes work.

我已经阅读了 Dockerfile reference 以及了解卷


  • 我有一个图像,让作曲家在RUN上安装wordpress到/ var / tmp / html

  • 然后ONBUILD我使用一些信息泊坞窗,康波se.yml ENVIRONMENT变量进行一些更多操作到/ var / tmp / html

  • 作为最后一步,我希望将所有内容复制到/ var / www / html并将其从ac作曲家(能够运行指向这些文件的apache)和本地文件系统(我将代码到这个html目录)

Dockerfile

docker-compose.yml

上面的链接是这些文件的github上的特定版本的链接

这个测试只是安装wordpress的所有东西,没有任何卷信息

This test is just everything to install wordpress without any volume information

Resu lt; / em>

Result

打开图像并列出/ var / www / html

Bashing into the image and listing /var/www/html

我得到:

root@4484ccdb63ca:/var/www/html# ls -al
total 32
drwxr-xr-x 6 www-data www-data 4096 Jan 21 11:45 .
drwxr-xr-x 6 root     root     4096 Jan 21 11:45 ..
-rw-r--r-- 1 www-data www-data   10 Jan 21 10:47 .gitignore
-rw-r--r-- 1 www-data www-data  138 Jan 21 10:47 composer.json
-rw-r--r-- 1 www-data www-data 3718 Jan 21 10:47 composer.lock
-rw-r--r-- 1 www-data www-data  428 Jan 21 10:47 index.php
drwxr-xr-x 6 www-data www-data 4096 Jan 21 11:45 vendor
drwxr-xr-x 8 www-data www-data 4096 Jan 21 11:45 wordpress

确定

并列出本地文件系统上的./html

and listing ./html on the local filesystem I get

total 0
drwxr-xr-x   3 miqueladell  staff  102 Jan 21 10:33 .
drwxr-xr-x  11 miqueladell  staff  374 Jan 21 13:04 ..
-rw-r--r--   1 miqueladell  staff    0 Jan 21 10:33 foo

这是错误,但完全预期。文件在容器上确定,但在容器外没有任何可用的东西,因为我没有提供任何卷信息。

which is wrong but totally expected. The files are ok on the container but have nothing to do outside the container because I did not provide any volume information.

Dockerfile

docker-compose.yml

上面的链接是这些文件的github上的特定版本的链接

在Dockerfile和docker-compose.yml上添加VOLUME

这是一个粘贴的diff

this is a paste of the diff

Dockerfile

- # VOLUME /var/www/html/
+ VOLUME /var/www/html/

docker-compose.yml

-  # volumes:
-  #    - .html:/var/www/html/
+  volumes:
+     - ./html:/var/www/html/

构建映像并运行cointainer的结果是空的容器上的空html目录,并且不改变本地文件系统

The result of building the image and running the cointainer is both empty an empty html directory on the container and no change to the local filesystem

谢谢!

编辑:我创建了一个最小的失败版本的问题:如何获取本地的码头集装箱产生的内容文件系统(最小失败示例)

I've created a minimal failing version of this question: How to get contents generated by a docker container on the local fileystem (minimal failing example)

推荐答案

查看Dockerfile和docker-compose.yml文件的版本2建议以下编辑:

Looking over version 2 of your Dockerfile and docker-compose.yml file I suggest the following edits:

Dockerfile

围绕第70行:从

WORKDIR /tmp/html

ONBUILD RUN rsync --ignore-existing -a /tmp/html/ .

WORKDIR /var/www/html

ONBUILD RUN rsync --ignore-existing -a /tmp/html/ .

WORKDIR 设置脚本的默认路径运行(当您登录到容器时)。

WORKDIR sets the default path for scripts being run (and when you log into the container).

在第一个例子中,您可以使用rsync有效地将 / tmp / html 的内容复制到自身,然后后来你bin目录。加上这个目录没有暴露在容器外面。

In your first example you are effectively copying the contents of /tmp/html to itself with rsync, and then later you bin that directory. Plus this directory isn't exposed outside the container.

根据上述更改,您将将 / tmp / html 的内容复制到 / var / www / html ,然后将其暴露在容器外部,然后使用docker-compose文件映射到本地计算机。

With the above change, you will copy the contents of /tmp/html to the /var/www/html, which is then exposed outside the container, and later mapped to your local machine using the docker-compose file.

Docker-compose.yml

尝试以下文件:

wordpress:
image: miqueladell/composed_wordpress_test
links:
    - wordpress_db:mysql
environment:
    - VIRTUAL_HOST=miqueladell.dev
    - WORDPRESS_DB_NAME=wordpress
ports:
    - "80:80"
  volumes:
   - ./html:/var/www/html

wordpress_db:
  image: miqueladell/mariadb-utf8mb4
  environment:
     - MYSQL_ROOT_PASSWORD=password

这篇关于如何获取本地文件系统上的docker容器生成的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆