Anna Carolina
Anna Carolina

Reputation: 395

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?

Upvotes: 12

Views: 25262

Answers (3)

vijayank8888
vijayank8888

Reputation: 31

use following commands:

docker ps -a

Get NAMES at end of columns which you run with docker.

docker cp CONTAINER-NAMES:CONTAINER_DIR-to-copy PATH-to-Copy

Example:

docker cp <xxx_yyy>:/folder/path/in/Container/to/copy /home/user/path/to/copy

Upvotes: 3

vivekyad4v
vivekyad4v

Reputation: 14853

Use docker cp on the host & not inside the container. In your case the command may look as below -

$ docker cp mycontainer:/Project/userfiles.txt userfiles.txt

After command completion you will get the file on host in the directory where you ran the docker cp command.

PS - I just checked that it works even if your container is in stopped state. For instance -

$ docker cp 9059e95cbe33:/etc/rc.local rc.local

9059e95cbe33 - Exited container ID/Name 

Upvotes: 15

VonC
VonC

Reputation: 1324917

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)

Upvotes: 14

Related Questions