Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share file from docker container to host

I have a docker container which is running fine. It generates a few files in the directory /Project/userfiles.txt. I need these files for some testing purpose. But when this container is stopped everything is deleted. So I need to copy this file from the container to host.

I have a python application running in the container which is generating the file. I want some command or any way through which I can send the userfiles.txt to host at the end when the container is stopped.

To do this I can use docker cp command and I tried it but it gave me an error docker not found as docker is not installed.

How can I share files from container to host?

like image 382
Anna Carolina Avatar asked Dec 27 '17 12:12

Anna Carolina


People also ask

How do I transfer files from container to host?

To summarize, follow these steps to copy a file from a Docker container to a host machine: Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container.


1 Answers

Use a volume (a bind mount one) to make sure the files written in /Project are persisted.

volumes

That means you need to docker run your image with a -v /host/path:/Project option.
See "Use bind mounts" for the simplest approach (which is to share a host folder with your container)

like image 57
VonC Avatar answered Oct 04 '22 09:10

VonC