Copy a File Out With docker cp
Copy a file or folder between a container and your computer, even from a stopped container. After this, you can rescue a file before you delete the container.
docker cp copies files between a container and your computer. It works in both directions and it works on stopped containers, which makes it the easiest way to rescue a file from a container that is about to be deleted.
The syntax is the same as cp, except that a path inside a container starts with the container name and a colon: container:/path.
Start a container with a file in it
docker run -d --name cp-demo alpine:3.24 sleep 300
Unable to find image
Status: Downloaded newer image for alpine:3.24
docker exec cp-demo sh -c "echo report line one > /tmp/report.txt"
Copy a file out
The first argument is the source and the second is the destination. A dot means the current folder.
docker cp cp-demo:/tmp/report.txt .
Get-Content .\report.txt
report line one
In a terminal that is not interactive, docker cp prints nothing when it works. That is why the second command is there, and why it is the proof. You can also give the destination a new name, for example docker cp cp-demo:/tmp/report.txt .\copy-of-report.txt.
Copy a file in
Set-Content -Path local.txt -Value "from the host"
docker cp .\local.txt cp-demo:/tmp/local.txt
docker exec cp-demo cat /tmp/local.txt
from the host
A file copied in is owned by root inside the container, whatever its owner was on your computer.
Copy a whole folder
Give a folder as the source and you get the folder with everything in it.
docker cp cp-demo:/etc/apk .\apk-copy
Get-ChildItem .\apk-copy | Select-Object -ExpandProperty Name | Sort-Object
keys
repositories
If the destination folder already exists, Docker copies the source folder into it. Use a name that does not exist yet when you want a clean copy.
Error: the file is not there
docker cp cp-demo:/tmp/nope.txt .
Error response from daemon: Could not find the file /tmp/nope.txt in container cp-demo
The path is wrong, or the file was never created. List the folder first with docker exec cp-demo ls /tmp. The path is checked inside the container, so use forward slashes, even on Windows.
It works on a stopped container
docker stop --timeout 1 cp-demo
docker cp cp-demo:/tmp/local.txt .\rescued.txt
Get-Content .\rescued.txt
cp-demo
from the host
This is the rescue case. The container stopped, and the file is still there until you remove the container. Copy what you need before you run docker rm.
docker rm cp-demo
💡 Tip: docker cp copies the files once. If you want a folder that stays in sync, use a bind mount instead. See the tutorial "Mount a Host Folder Without Losing Data".
Related course: Docker for Beginners.
About
Copy a file or folder between a container and your computer, even from a stopped container. After this, you can rescue a file before you delete the container.
- Responsible
- Jay P
- Last Update
- 08/02/2026
- Reading Time
- 3 min
Reviews
Sign in to write a reviewComments (0)
Sign in to commentNo comments yet.