Contents
Docker cp Tutorial: Copy Files Between a Host and Container
docker cp copies files between a host and a container. You can use it whether the container is running or stopped.
Syntax
docker cp [OPTIONS] CONTAINER:SOURCE-PATH HOST-DESTINATION # container → host
docker cp [OPTIONS] HOST-SOURCE-PATH CONTAINER:DESTINATION-PATH # host → container
| Option | Description |
|---|---|
-a, --archive | Preserve file ownership (uid/gid) information |
-L, --follow-link | If the source is a symbolic link, copy the file it points to |
-q, --quiet | Do not show copy progress |
Set the source or destination to - to read from standard input or write to standard output in tar format.
Examples
Run these commands on an nginx container named web:
$ docker cp web:/etc/nginx/nginx.conf ./nginx.conf
$ head -n 3 nginx.conf
user nginx;
worker_processes auto;
$ echo "<h1>copied in</h1>" > page.html
$ docker cp page.html web:/usr/share/nginx/html/page.html
$ docker exec web cat /usr/share/nginx/html/page.html
<h1>copied in</h1>
$ docker cp web:/etc/nginx/conf.d ./confd
$ ls confd
default.conf
$ docker cp web:/no/such/file .
Error response from daemon: Could not find the file /no/such/file in container web
| Command | Description |
|---|---|
docker cp web:/etc/nginx/nginx.conf ./nginx.conf | Copy the container's configuration file to the host, often to get the default configuration before editing it |
docker cp page.html web:/usr/share/nginx/html/page.html | Copy a file from the host into the container |
docker cp web:/etc/nginx/conf.d ./confd | Copy an entire folder. If the destination confd does not exist, Docker creates it and puts the folder's contents inside |
docker cp web:/no/such/file . | Error shown when the source does not exist |
Folder copy rules:
- Destination does not exist: Create the destination folder and copy the source folder's contents into it (as in the example above).
- Destination already exists: Put the entire source folder inside the destination as
destination/source-folder-name. - Source path ends with
/.(for example,web:/etc/nginx/conf.d/.): Copy only the contents, not the folder itself.
When to use docker cp
Good fit:
- Retrieve a container's default configuration files, logs, or generated files.
- Temporarily copy a file into a container for testing.
- Back up and restore a database in Windows PowerShell: first export it to a file inside the container, then use
docker cpto copy it out. This avoids issues with PowerShell not supporting<and with encoding (see Common error messages).
Not a good fit:
- Sync code during development: use a Bind mount or
docker compose watch(see Node.js app). - Data you need to keep: use a Volume.
Related sections
Exercises
Exercise: Copy the nginx welcome page /usr/share/nginx/html/index.html from the web container to the host, change the title, and copy it back. Check the result in a browser (start the container with -p 8080:80).
Answer
docker cp web:/usr/share/nginx/html/index.html ./index.html
# Edit index.html with an editor
docker cp ./index.html web:/usr/share/nginx/html/index.html
Refresh http://localhost:8080 to see the change. The change disappears when you remove the container.
FAQ
Can I use docker cp with a stopped container?
Yes. docker cp reads and writes directly to the container's filesystem, so you can copy files even when the container is stopped. This is useful when a container fails to start and you need to retrieve a configuration file or log for inspection.
Will a configuration file I change with docker cp persist after I recreate the container?
No. Files copied into the container live in its writable layer and disappear when you remove the container. For files you need to keep, use a Bind mount or add them to the image.
Is there an equivalent command in docker compose?
Yes: docker compose cp service-name:path destination. The syntax is the same, but use the service name instead of the container name.
Updated October 14, 2026