Cover image for Copy files into a running Docker container

Copy files into a running Docker container

matthias profile image Matthias ๐Ÿค– ใƒป2 min read


Sometimes you want to copy files between a running docker container and your local filesystem. This might be helpful for example if you're running a local webserver and you want to update a HTML file.

The command docker cp gives you the ability to copy files from your local filesystem into a running Docker container and vice versa.

Let's assume you have a running NGINX container. If not you can run a container with docker run -d -p 8000:80 nginx.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
3d1fb2f49f1a        nginx               "nginx -g 'daemon ofโ€ฆ"   9 hours ago         Up 9 hours>80/tcp   nifty_mirzakhani

You can now copy a HTML file into your NGINX root folder (in this case /usr/share/nginx/html) with docker cp:

docker cp test.html 3d1fb2f49f1a:/usr/share/nginx/html
          ^         ^            ^
          |         |            |= destination path within the docker container
          |         |
          |         |= the id of the container in which you want to transfer the file
          |= path to file you want to transfer

You can now access your uploaded file in your browser (http://localhost:8000/test.html).

Download files

It is also possible to copy files from your container to your local filesystem.


docker cp 3d1fb2f49f1a:/etc/nginx/conf.d/default.conf ~/
          ^            ^                              ^
          |            |                              |= destination path on your local machine
          |            |
          |            |= path to the file in your container
          |= container id

Path information

The container paths are relative to the container's root directory (/), hence you can omit the leading slash.
Both paths are valid: /etc/nginx/conf.d/default.conf and etc/nginx/conf.d/default.conf.

For your local machine paths you can use absolute and relative values.

If you like my content, you might want to follow me on Twitter?! @fullstack_to

Cover Image by Photo by frank mckenna on Unsplash

Posted on by:

matthias profile

Matthias ๐Ÿค–


๐Ÿ‘จโ€๐Ÿ’ป Software Engineer ๐Ÿš€ Working on all stacks ๐Ÿท๏ธ #typescript #react #docker #kubernetes #devops #cloud #webdev


markdown guide

Cool! Didn't know about this feature..
My question is: does the copied file (from filesystem to container) persists in case of rerun ofthe container (as it happens with a copy instruction in the dockerfile)?


Glad that I could show you something new!

To answer your question, if you stop the container (docker stop abcdef) and rerun (docker start abcdef) it, the file will be still available.

If you remove the container (docker rm abcdef), the file will be deleted too, of course.