When working with Docker, you may often need to copy files—such as configuration files, scripts, or assets—from your host machine into a running Docker container. Docker provides simple and effective commands to handle this task.
In this guide, we’ll walk you through multiple methods to copy files from host to container using real-world examples and best practices.
📦 Method 1: Using docker cp
Command
The docker cp
command allows you to copy files or folders from the host system into a running container, and vice versa.
Syntax:
docker cp <host-path> <container-id>:<container-path>
✅ Example: Copy a file into a container
docker cp ./config.json my_container:/app/config.json
This copies config.json
from your local folder into /app
directory inside the running container named my_container
.
✅ Example: Copy an entire folder
docker cp ./myfolder my_container:/app/
🔍 How to Get Container ID or Name
To find the container’s ID or name:
docker ps
This will list all running containers. Use the CONTAINER ID
or NAMES
column value in your docker cp
command.
📦 Method 2: Copy Files During Docker Image Build (Dockerfile
)
If you want files to be present in the container from the beginning, use the COPY
or ADD
instruction in your Dockerfile.
Example Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package.json .
COPY ./src ./src
Then build your Docker image:
docker build -t my-app .
✅ Best for:
- Repeatable builds
- Including static assets or source code in the image
📦 Method 3: Use Docker Volumes (For Syncing Files Dynamically)
If you want your local files to be accessible and updated in real time inside a container, mount a volume instead of copying.
Example:
docker run -v $(pwd)/html:/usr/share/nginx/html -d nginx
This mounts the local ./html
folder to the Nginx container’s default HTML directory.
✅ Best for:
- Development environments
- Live editing of code or configuration
📦 Method 4: Using docker exec
and Shell Commands
Another method is to manually create or write files inside the container using docker exec
.
Example:
docker exec -i my_container sh -c 'cat > /app/newfile.txt' < localfile.txt
This sends the contents of localfile.txt
into /app/newfile.txt
inside the container.
🧠 Summary: File Copy Options
Method | Use Case | Works With Running Container? |
---|---|---|
docker cp | Quick, manual copy from host | ✅ Yes |
COPY in Dockerfile | Build-time inclusion of files | ❌ No (only during build) |
Volume Mounting | Live file sync during development | ✅ Yes |
docker exec + shell | Advanced, stream-based file creation | ✅ Yes |
🛡 Pro Tips
- Ensure correct file permissions inside the container after copying.
- Avoid copying sensitive data unless necessary; prefer using Docker secrets for production.
- When using volumes, keep in mind that mounted files may override files in the image.
🚀 Final Thoughts
Copying files into a Docker container is a fundamental but powerful capability. Whether you’re manually pushing config files, automating builds, or setting up live development environments, Docker offers flexible ways to move data across the container boundary.