0
0
Dockerdevops~5 mins

Copying files to and from containers in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to move files between your computer and a running container. This helps when you want to add files to the container or get files out for inspection or backup.
When you want to add a configuration file to a running container without rebuilding the image
When you need to retrieve logs or output files generated inside a container
When you want to copy a script or data file into a container to run it there
When you want to backup files from a container to your local machine
When you want to inspect or modify files inside a container without using an interactive shell
Commands
Start a new container named 'example-container' running the nginx web server in the background.
Terminal
docker run -d --name example-container nginx:1.23.4
Expected OutputExpected
a1b2c3d4e5f67890123456789abcdef0123456789abcdef0123456789abcdef0
-d - Run container in detached mode (in background)
--name - Assign a custom name to the container
Copy the file 'local-file.txt' from your computer into the container's nginx web folder so it can be served by nginx.
Terminal
docker cp ./local-file.txt example-container:/usr/share/nginx/html/local-file.txt
Expected OutputExpected
No output (command runs silently)
Copy the nginx access log file from inside the container to your local machine for inspection.
Terminal
docker cp example-container:/var/log/nginx/access.log ./access.log
Expected OutputExpected
No output (command runs silently)
List files in the nginx web folder inside the container to verify the copied file is there.
Terminal
docker exec example-container ls /usr/share/nginx/html
Expected OutputExpected
50x.html index.html local-file.txt
exec - Run a command inside a running container
Stop and remove the container to clean up after the test.
Terminal
docker stop example-container && docker rm example-container
Expected OutputExpected
example-container example-container
stop - Stop a running container
rm - Remove a container
Key Concept

If you remember nothing else from this pattern, remember: use 'docker cp' to move files between your local machine and a running container easily.

Common Mistakes
Trying to copy files to a container that does not exist
The 'docker cp' command requires the container to exist. It can copy to running or stopped containers, but copying to a non-existent container will fail.
Make sure the container exists. It can be running or stopped, but the name must be correct.
Using relative paths inside the container without verifying the directory exists
If the target directory inside the container does not exist, the copy will fail or create a file with an unexpected name.
Verify the target directory exists inside the container before copying files there.
Confusing source and destination order in the 'docker cp' command
The command syntax is 'docker cp source destination'. Mixing these will copy files in the wrong direction or cause errors.
Always specify the source first, then the destination.
Summary
Use 'docker cp' to copy files to and from containers without rebuilding images.
Verify container names and paths carefully to avoid errors.
Use 'docker exec' to check files inside containers after copying.