Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy files from Container to host using Dockerfile

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;"]
like image 463
Vetrivelkumar Pandi Avatar asked Jun 13 '18 09:06

Vetrivelkumar Pandi


People also ask

How do you copy files from docker container to the host and vice versa?

Using the Docker cp Command. The Docker cp command can be used to copy files and directories from the host machine to a container and vice-versa.

How do I copy files from one container to another?

You can use the docker cp command to copy the file. The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).


2 Answers

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.

like image 102
coderanger Avatar answered Oct 01 '22 02:10

coderanger


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.

like image 21
emory Avatar answered Oct 01 '22 02:10

emory