使用Dockerfile将文件从Container复制到主机 [英] Copy files from Container to host using Dockerfile

查看:121
本文介绍了使用Dockerfile将文件从Container复制到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Docker File,我的要求是将容器内文件夹的内容复制到本地主机。我该如何实现?

I am writing a Docker File and my requirement is to copy the content of a folder inside the container to local host. How can I acheive this?

FROM ubuntu

RUN apt-get update && apt-get install -y apache2 && apt-get  install nginx  -y


#COPY resources   /var/www/html/

#VOLUME /var/www/html:/var/www/html

COPY /var/www/html/ /var/www/html/
CMD ["nginx", "-g", "daemon off;"]


推荐答案

将文件从容器复制到主机可能不是一个好主意在构建期间。您应该认真考虑您的用例。

It is probably a bad idea to copy files from the container to the host during build. You should seriously consider your use case.

但是,可以做到,我将与您分享一个过程,因为这对我来说是展示我的Docker知识的机会-不是因为我认为您应该这样做。还有其他方法可以做到这一点。我的方法不是好是坏-它们都是弊端。

However, it can be done and I will share with you a procedure because it is an opportunity for me to show off my Docker knowledge - not because I think you should do this. There are other ways to do this. My way is not better or worse - they are all kludges.


  1. 按照 https://success.docker.com/article/how-do-我为dockerd启用远程api 。基本上添加 -H tcp://0.0.0.0:2376 。这是一个非常危险的过程,因为它打开后会被网络上的任何人扎根。有很多方法可以通过身份验证来减轻这种风险,但实际上最好的方法是不要这样做

  2. 修改Dockerfile:

  1. Modify your dockerd configuration as explained in https://success.docker.com/article/how-do-i-enable-the-remote-api-for-dockerd. Basically add -H tcp://0.0.0.0:2376. This is a very risky procedure b/c it opens you open to be rooted by anyone on your network. There are ways to mitigate this risk with authentication, but really the best way is to JUST DON'T DO IT.
  2. Modify your Dockerfile:

  1. 在RUN块之前添加 ARG DOCKER_HOST

  2. 运行中块:

  1. Add a ARG DOCKER_HOST before the RUN blocks.
  2. In the run blocks:

  1. 安装docker。

  2. 添加ʻexport DOCKER_HOST = $ {DOCKER_HOST}。

  3. 添加 docker容器运行--mount type = bind,source = /,destination = / srv / host alpine:3.4 ...

  1. Install docker.
  2. Add `export DOCKER_HOST=${DOCKER_HOST}.
  3. Add docker container run --mount type=bind,source=/,destination=/srv/host alpine:3.4 ...



  • 确定主机的IP地址。让我们假设它是10.10.20.100。

  • 通过添加--build-arg DOCKER_HOST = 10.10.20.100来修改构建命令。

  • 在步骤2.2.3中,您已经根主机计算机,并且可以做任何您想做的事情-包括写入任何文件。

    In step 2.2.3 you have rooted the host computer and you can do whatever you want - including writing to any file.

    这是一个愚蠢的主意,但它表明,由于您可以从内部构建运行docker,因此实际上没有什么是您无法在内部进行的。如果要从内部版本运行gui应用程序,则可以执行此操作。

    This is a dumb idea, but it shows that since you can run docker from within a build, there really is not anything you can not do from inside a build. If you want to run a gui app from inside a build you can do it.

    这篇关于使用Dockerfile将文件从Container复制到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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