Docker - Copy file between container and host using the docker cp command

This assumes you have installed Docker on Linux and Docker is running.

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

The docker cp command can be used to copy a file in a container to a directory on the host operating system, or to copy a file on the host operating system to a directory in the container.

You will want to first issue the docker ps or docker container ls command to get the list of containers. Let's say the docker ps command returns the following.

CONTAINER ID     IMAGE              COMMAND                 CREATED         STATUS     PORTS     NAMES
d937372c09ab9    b939aa938add9913   "/docker-entrypoin..."  6 minutes ago   Created              foo

 


Copy a single file from container to host

In this example, the /usr/local/bar.txt file in the "foo" container is copied to /tmp on the host operating system (the Docker system).

docker cp foo:/usr/local/bar.txt /tmp

 


Copy a directory from container to host

In this example, every file and directory below /usr/local in the foo container will be copied to /tmp on the host operating system (the Docker system).

docker cp foo:/usr/local /tmp

 


Copy from host to container

In this example, the /tmp/bar.txt file on the host operating system (the Docker system) is copied to /usr/local in the "foo" container.

docker cp /tmp/bar.txt foo:/usr/local

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 5cab7 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |