DEV Community

Cover image for Copy files into a running Docker container
Matthias πŸ€–
Matthias πŸ€–

Posted on

22 6

Copy files into a running Docker container

asciicast

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          0.0.0.0:8000->80/tcp   nifty_mirzakhani
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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.

asciicast

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

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

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (2)

Collapse
 
giuliovn profile image
giuliovn β€’

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)?

Collapse
 
matthias profile image
Matthias πŸ€– β€’

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.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay