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
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.
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 cp is your savior!
The command format for copying a file into a docker container:
Conversely, the command format for copying a file out of a docker container:
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
docker cp commands.
How I use it
Just two reference commands showing how I copy files in and out of a
Copying files in
Combining this with my previous
on connecting to docker containers, I would use the following command
filename.txt to the
/tmp directory in the container:
Why copy to
/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:
of the container and into the current directory as
To get additional details on the
docker cp command, the official
documentation is the best place:
When I am in a bind and need to get a file in/out of a running docker
container, I use
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.