14

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;"]
2
  • 1
    Have you tried with VOLUME <your_folder_inside>, after build docker run -v <your_folder_inside>, and after that your host dir /var/lib/docker/volumes/<your_folder_inside>`? Commented Jun 13, 2018 at 15:45
  • This question is similar to: How to copy files from dockerfile to host?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 12, 2024 at 13:05

3 Answers 3

6

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

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. 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. Add a ARG DOCKER_HOST before the RUN blocks.
    2. In the run blocks:
      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 ...
  3. Determine the IP address of your host computer. Let us assume it is 10.10.20.100.
  4. Modify your build command by adding --build-arg DOCKER_HOST=10.10.20.100.

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

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.

Sign up to request clarification or add additional context in comments.

Comments

2

There is no specific way to do this during the build process. At runtime you can copy files out via a mounted volume, but this is not available during the build process. If you just mean at run time then you can do things like docker run -v .:/out myimage -- cp -r /from/somewhere /out or similar.

2 Comments

Thanks, I am getting error as below . Iwant to copy the content of :/var/www/html/ from container to host location of :/var/www/html/ root@ionapp:~/nginx# docker run -v /var/www/html/:/var/www/html/ ngnxvolume1 -- cp -r /var/www/html/ /var/www/html/ docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"--\": executable file not found in $PATH": unknown. ERRO[0002] error waiting for container: context canceled root@ionapp:~/nginx#
You need to use a different mount point, and possibly you need a different command (possibly using docker cp as mentioned elsewhere) if the cp binary isn't available. Try /bin/cp.
2

There's no support for writing a Dockerfile with a statement to modify files on the host during docker build. You should keep in mind that the actual image build might happen in another Docker engine than the one where you kick off the build (the Dockerfile along with its build context are uploaded to the Docker Engine).

There are at least two options here:

1) Write a RUN statement that uses a BUILD_ARG to reach out to another host and triggers some action there. I consider this as a very bad hack, so I prefer not to be more specific about how that statement might look like.

2) Perform the desired actions an a normal docker run, where you can mount a host's directory as volume into the container.

I haven't looked into the https://github.com/genuinetools/img tool, yet. It also aims at building images, but might provide more possibilities during build time.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.