π Day 08: Copy Files between Host and Docker Containers π³

Table of contents
- π¬ Video Demonstration
- π§ Set Example Container Name
- π§± 1. Copying Files using docker cp (Simple & Powerful)
- ποΈ 2. Copying Multiple Files or Directories
- π§° 3. Backup and Restore Hidden Files (.dotfiles)
- π¦ 4. Export Config Files from a Image
- β 5. Safer Approach β Bind Mounts or Volumes (Recommended)
- β οΈ 6. Advanced (Not Recommended) β Direct Access to Docker Filesystem
- β Summary Table
- π Final Tips
- π Additional Resources

Docker Zero to Hero Series - Copying Like a Pro
Welcome to Day 08 of our Docker Zero to Hero π³ series!
Today weβre diving into how to copy files β¬
οΈ From Docker Container to Host and β‘οΈ From Host to Docker Container.
Letβs break it down step-by-step with examples for:
β
Single files
β
Multiple files and directories
β
Using docker cp
β
Using docker exec
β
Exporting config from a temp container
β
Advanced tricks & troubleshooting
π¬ Video Demonstration
π§ Set Example Container Name
# Run Day 08 Container for demo
docker run -itd --name=day08 -p 8085:80 -v /home/user/html:/usr/share/nginx/html nginx:latest
mkdir -p day08
# Example container ID or name
container_name="day08"
or
container_name="d40fa919ae31"
π§± 1. Copying Files using docker cp
(Simple & Powerful)
π₯ Copy from Container β‘οΈ to Host
# Inspect container path/colume/bind-mount
docker inspect $container_name | jq '.[0].Mounts[] | select(.Type == "bind" or .Type == "volume") | {Type, Source, Destination}'
# Single file
docker cp $container_name:/path/in/container/file.txt /path/on/host/file.txt
# Example:
mkdir -p /home/user/html_file
docker cp $container_name:/usr/share/nginx/html/index.html /home/user/html_file/index.html
cat /home/user/html_file/index.html
# Directory (recursively)
docker cp $container_name:/usr/share/nginx/html /home/user/html_backup
ls -alsh /home/user/html_backup
π€ Copy from Host β‘οΈ to Container
# Single file
docker cp /path/on/host/file.txt $container_name:/path/in/container/file.txt
# Example:
docker cp /home/user/html_file/index.html $container_name:/usr/share/nginx/html/index.html
# Directory (recursively)
docker cp /home/user/html_backup $container_name:/usr/share/nginx/html
docker exec $container_name ls -alsh /usr/share/nginx/html
ποΈ 2. Copying Multiple Files or Directories
# Copy multiple files (from host)
docker cp ./test.sh $container_name:/usr/share/nginx/html
docker cp ./Desktop $container_name:/usr/share/nginx/html
docker exec $container_name ls -alsh /usr/share/nginx/html
β Tip: Use
.
to copy all contents of a directory
docker cp ./Desktop/. $container_name:/usr/share/nginx/html
π§° 3. Backup and Restore Hidden Files (.dotfiles)
π€ Backup from Container
docker cp $container_name:/.dockerenv ./day08/.dockerenv
ls -alsh ./day08/.dockerenv
π₯ Restore to Container
docker cp ./day08/.dockerenv $container_name:/.dockerenv_nedw
docker exec $container_name ls -alsh /
β οΈ Make sure to include the trailing
.
to copy contents, not the directory itself!
π¦ 4. Export Config Files from a Image
# Example: Exporting grafana.ini from Grafana container
docker run --rm -d --name temp-grafana grafana/grafana && docker cp temp-grafana:/etc/grafana/grafana.ini $PWD/day08/grafana.ini && docker stop temp-grafana
ls -alsh ./day08/
β 5. Safer Approach β Bind Mounts or Volumes (Recommended)
This is the safer and recommended way to interact with container files, using bind mounts or Docker volumes.
# Example of editing content via bind mount or volume
docker inspect $container_name | grep -A5 Mounts
# Modify file on HOST (affects container if using bind mount)
echo "Welcome to DevOps in Action" | sudo tee /home/user/html/index.html
# Access in browser
http://192.168.1.22:8085/
β οΈ 6. Advanced (Not Recommended) β Direct Access to Docker Filesystem
This method accesses container files directly via Dockerβs internal storage (e.g., OverlayFS). Use only for advanced troubleshooting or backup purposes. It bypasses Dockerβs APIs and may corrupt data or containers if misused.
# Create container
docker run -d --name test-nginx -p 8086:80 nginx
# Get container's merged directory path
CONT_PATH=$(docker inspect test-nginx --format='{{.GraphDriver.Data.MergedDir}}')
# π½ Copy file from container to host
sudo cp $CONT_PATH/usr/share/nginx/html/index.html $PWD/day08/index.html
cat $PWD/day08/index.html
# Access in browser
http://192.168.1.22:8086/
# πΌ Copy file from host into container's filesystem (dangerous)
echo "Welcome to DevOps in Action" | sudo tee "$PWD/day08/index.html"
sudo cp $PWD/day08/index.html $CONT_PATH/usr/share/nginx/html/index.html
sudo cat $CONT_PATH/usr/share/nginx/html/index.html
β οΈ Warning: Direct manipulation of container storage may cause data corruption. Always prefer Docker CLI commands or volume mounts unless absolutely necessary.
β Summary Table
Task | Command Format |
Copy file: Host β Container | docker cp file.txt container:/path/ |
Copy file: Container β Host | docker cp container:/file.txt /path/on/host/ |
Copy entire directory | docker cp /src/. container:/dest/ |
Export file from temp container | docker cp container:/path ./ |
π Final Tips
Always verify file permissions after copying.
Avoid copying into running containers' system paths (
/bin
,/lib
) unless you know what you're doing.Use
docker volume
if frequent file sharing is needed.
π Additional Resources
- π Docker cp command
Subscribe to my newsletter
Read articles from Ibrar Ansari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ibrar Ansari
Ibrar Ansari
Hello! I'm a DevOps Engineer dedicated to continuous learning and contributing to the tech community. If you find value in what I share, feel free to spread the word to others who might benefit as well. Your support is greatly appreciated!