Skip to content
Log In Sign Up info@muvcloud.com

The Syntax

docker cp SOURCE CONTAINER:PATH
docker cp CONTAINER:PATH DESTINATION

One side is a path on the server, the other is a container name or ID, a colon, and a path inside the container. Paths inside the container are read from its root folder, so the leading slash is optional; writing absolute paths keeps commands clear.

Copy a File into a Container

docker cp ./nginx.conf web:/etc/nginx/nginx.conf

If the destination is an existing folder, the file keeps its name inside it. If the destination does not exist, the file is saved under that name. The parent folder must exist; docker cp does not create it.

Copy a File out of a Container

docker cp web:/var/log/nginx/error.log ./error.log
docker cp db:/tmp/dump.sql /var/backups/

This is the quickest way to pull a log, a dump, or a generated file out of a container that has no SSH and no shared folder.

Copy Whole Folders

Folders are copied recursively without an extra option. Two forms behave differently:

docker cp ./site web:/usr/share/nginx/
docker cp ./site/. web:/usr/share/nginx/html/
  • Without a trailing dot: the folder itself is copied, so the first command creates /usr/share/nginx/site when the destination exists.
  • With /. at the end: only the contents of the folder are copied into the destination.

To check the result, list the folder with docker exec web ls -la /usr/share/nginx/html.

Stopped Containers Work Too

docker cp reads and writes the container's file system directly, so the container does not have to run. This helps when a container keeps crashing: copy its configuration out, fix it on the server, and copy it back before you start the container again.

docker cp app:/app/config.yml ./config.yml
nano config.yml
docker cp ./config.yml app:/app/config.yml
docker start app

Some locations cannot be copied: /proc, /sys, and /dev inside the container, tmpfs mounts, and mounts created inside the container. For those, run tar through docker exec instead.

Ownership and Permissions

Files copied out of a container belong to the user who ran docker cp. Files copied into a container are owned by root by default. If the application inside runs as another user and cannot read or write the file, either copy with -a, which keeps the user and group IDs of the source, or fix the owner afterward:

docker cp -a ./uploads/. app:/app/uploads/
docker exec -u 0 app chown -R 1000:1000 /app/uploads

Check the user ID the application runs as with docker exec app id. By default a symbolic link in the source is copied as a link; -L copies the file it points to instead.

The Compose Form

docker compose cp web:/etc/nginx/nginx.conf ./nginx.conf
docker compose cp ./nginx.conf web:/etc/nginx/nginx.conf

Run it in the project folder and use the service name from compose.yaml instead of the container name.

docker cp, a Bind Mount, or a Volume?

MethodGood for
docker cpOne-off copies and rescue
Bind mountConfig you edit on the server
Named volumeData the app writes

Keep one limit in mind: a file you copy into the container's own file system is lost when the container is removed and created again, for example during an update. Files that must stay belong in a bind mount or a volume, as described in Docker volumes vs bind mounts and how to update a Docker container.

Common Errors

  • No such container: check the name with docker ps -a.
  • Could not find the file: the path inside the container is wrong; list it with docker exec and ls.
  • No such directory: the parent folder of the destination does not exist; create it first.
  • Permission denied on the server side: the destination folder on the host is not writable by your user; copy to your home folder and move the file with sudo.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations