πŸš€ Day 08: Copy Files between Host and Docker Containers 🐳

Ibrar AnsariIbrar Ansari
4 min read

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

Watch on Youtube

πŸ”§ 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/

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/

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

TaskCommand Format
Copy file: Host β†’ Containerdocker cp file.txt container:/path/
Copy file: Container β†’ Hostdocker cp container:/file.txt /path/on/host/
Copy entire directorydocker cp /src/. container:/dest/
Export file from temp containerdocker 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

0
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!