How to Copy Files Between Host and Container
One of the most common tasks when working with Docker is moving files in and out of containers. Whether you need to extract a configuration file, inject a script, or back up data, the docker cp command is your go-to tool.
This guide covers how to copy files from a container to your host, and vice versa.
The docker cp Command
The syntax for docker cp is similar to the standard Unix cp command (copy), but one of the paths will include a container ID or name.
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copying from Container to Host
To copy a file from a running (or stopped) container to your local machine:
docker cp <container_id>:/path/to/file /local/destination/path
Example
Suppose you have a container named my-web-server and you want to copy the nginx.conf file to your current directory.
- Find the container name or ID:
docker ps - Copy the file:
docker cp my-web-server:/etc/nginx/nginx.conf ./nginx.conf
Now, nginx.conf is saved in your current working directory.
Copying from Host to Container
To copy a file from your local machine into a container:
docker cp /local/source/path <container_id>:/path/to/destination
Example
If you edited your local nginx.conf and want to push it back into the container:
docker cp ./nginx.conf my-web-server:/etc/nginx/nginx.conf
Note: You might need to restart the container for changes to take effect, depending on the application.
docker restart my-web-server
Copying Directories
docker cp works with directories exactly the same way as files. It copies the directory recursively.
From Container to Host:
docker cp my-app:/var/log/app_logs ./local_logs
From Host to Container:
docker cp ./src my-app:/app/src
Common Issues & Troubleshooting
1. Permissions
Files copied into a container will retain the UID/GID (User ID / Group ID) from the source, or inherit permissions depending on the destination.
If you copy a file into a container and the application can't read it, check the ownership inside the container:
docker exec -it my-container ls -l /path/to/file
You might need to fix ownership inside the container:
docker exec -u 0 -it my-container chown appuser:appgroup /path/to/file
2. Path Not Found
Ensure you are using absolute paths inside the container. While relative paths work on the host side, it's safer to specify the full path for the container side to avoid confusion about the container's working directory.
3. Container Does Not Exist
Double-check your container name or ID with docker ps -a. docker cp works on stopped containers too!
Alternative: Bind Mounts
For development, constantly copying files back and forth is tedious. Instead, use Bind Mounts (or Volumes) to map a host directory directly into the container.
In docker-compose.yml:
services:
web:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
With this setup, any change you make to ./nginx.conf on your host is immediately visible inside the container.
Deploying with Ease
Managing files and volumes is easier when your platform handles it for you.
On Hostim.dev, you can define your volumes in docker-compose.yml, and we handle the persistent storage for you. No manual copying required.