Red Green Repeat Adventures of a Spec Driven Junkie

docker cp to the Rescue!

tl;dr:

sudo docker cp filename.txt $(sudo docker ps | grep foo | head -1 | awk '{ print $1 }'):/tmp
sudo docker cp $(sudo docker ps | grep foo | head -1 | awk '{ print $1 }'):/tmp/filename.txt filename_out.txt

I am sharing one of the docker tools I use when I need to deal with a production problem and just enough data I need to store in files. I walk through the docker cp command to copy a file in and out of a running container.

You will get an idea of how to use docker cp and its syntax. This will take you less than three minutes to read this article.

Palette inscribed for Smendes, High Priest of Amun source and more information

Introduction

Do you need to get a file into or out of a running docker container?

For me, this is not a good sign when I have to think about how to get files in/out of a live container. Why can’t just rebuild the whole container with that file?

This usually means I am dealing with a problem on production and have to work with more data than I can derive or copy/paste.

At the same time, I just want to solve the problem at hand, learn from the problem to solve it next time.

docker cp to the Rescue!

If you are in a bind and need to get files into or out of a running docker container, docker cp is your savior!

The command format for copying a file into a docker container:

docker cp <file to copy in> <docker container SHA>:<container folder>

Conversely, the command format for copying a file out of a docker container:

docker cp <docker container SHA>:<path to file in container> <filename>

note: wildcard matchers like: * do not work with docker cp, so if you need to copy n files in/out of a container, you will need n docker cp commands.

How I use it

Just two reference commands showing how I copy files in and out of a container using docker cp

Copying files in

Combining this with my previous article on connecting to docker containers, I would use the following command to copy filename.txt to the /tmp directory in the container:

sudo docker cp filename.txt $(sudo docker ps | grep foo | head -1 | awk '{ print $1 }'):/tmp

Why copy to /tmp?

Using /tmp is an easy location to reference later when working inside the container and also there’s a low chance of overwriting a file that is essential to the container’s operation. (Remember, if you’re running this command, you don’t want to have another problem.)

Copying files out

This is the command I use to copy a file name: /tmp/filename.txt out of the container and into the current directory as filename_out.txt

sudo docker cp $(sudo docker ps | grep foo | head -1 | awk '{ print $1 }'):/tmp/filename.txt filename_out.txt

More information

To get additional details on the docker cp command, the official documentation is the best place:

https://docs.docker.com/engine/reference/commandline/cp/

Conclusion

When I am in a bind and need to get a file in/out of a running docker container, I use docker cp.

For me, using this command in practice is not ideal because it means I need to interact with production instances.

At the same time, the command lets me immediately solve the problem to give opportunity later to solve it better.