Copy files from docker container to host

Here we will learn how to copy files from a Docker container to the host machine or from host machine to the container using docker cp command. Note that we can do the copy operation irrespective of the container is running or not.

1. Docker container to host

Here is the syntax for copying file from Docker container to it's host:

1docker cp <container-id-or-name>:/full/path/of/file ./ 

Suppose here is our docker containers:

1user@tiger:~$ docker ps
2CONTAINER ID    IMAGE     COMMAND                  CREATED          STATUS          PORTS               NAMES
3febc7aef0ce4    dynaads   "/go/bin/da"             26 hours ago     Up 26 hours     8099/tcp            cdynaads
42c1e9e4e830a    nginx     "nginx -g 'daemon of…"   5 months ago     Up 11 days      0.0.0.0:80->80/tcp  cnginx

So if we want to copy a file from the first container, we need to write following command:

1docker cp febc7aef0ce4:/go/bin/da ./

or

1docker cp cdynaads:/go/bin/da ./

Both the command will give same result.

2. Host to Docker container

Copying file from host machine to Docker container is just opposite of the above example. Here we have to give the host path reference first and then the container path reference.

Syntax is:

1docker cp ./filename <container-id-or-name>:/full/path/  

So considering our example container, the exact command will be:

1docker cp ./da febc7aef0ce4:/go/bin/ 

or

1docker cp ./da cdynaads:/go/bin/ 

References:

comments powered by Disqus