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.
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
You can now copy a HTML file into your NGINX root folder (in this case
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).
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
The container paths are relative to the container's root directory (
/), hence you can omit the leading slash.
Both paths are valid:
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