docker cp — copy files between container and host
Quick Answer
# Host → container
docker cp ./local-file.txt my-container:/app/file.txt
# Container → host
docker cp my-container:/app/logs/error.log ./error.log
# Copy a whole directory
docker cp my-container:/app/dist ./dist
Usage
You need to copy config files, logs, or build artifacts between your host and a container.
Other causes & fixes
Copy works on stopped containers too
# Even if the container is not running
docker ps -a | grep my-container # Exited
docker cp my-container:/app/config.json ./config.json
Directory trailing slash behavior
Adding a trailing / to the source copies the directory contents (not the directory itself).
# Copies the dist/ directory itself
docker cp my-container:/app/dist ./
# Copies the contents of dist/ into ./dist-output/
docker cp my-container:/app/dist/. ./dist-output/
Use volumes for persistent sharing instead
# Bind mount — changes are reflected instantly in both directions
docker run -v $(pwd)/data:/app/data my-image
Related