How to Copy File to Docker Container Easily
Use the
docker cp command to copy files from your host machine to a running Docker container. The syntax is docker cp <source_path> <container_name>:<destination_path>. This copies the file or folder into the container at the specified location.Syntax
The docker cp command copies files or folders between your host and a Docker container.
- source_path: Path to the file or folder on your host machine.
- container_name: The name or ID of the running container.
- destination_path: Path inside the container where you want to copy the file or folder.
Use a colon : to separate the container name and destination path.
bash
docker cp <source_path> <container_name>:<destination_path>
Example
This example copies a file named example.txt from your current host directory into the /tmp folder inside a container named mycontainer.
bash
docker cp example.txt mycontainer:/tmp/example.txt
Common Pitfalls
- Trying to copy files to a container that is not running will fail.
- Using incorrect container names or IDs causes errors.
- For folders, ensure the destination path exists or Docker will create it.
- Remember to use the colon
:between container name and path.
bash
docker cp example.txt mycontainer/tmp/example.txt # Wrong: missing colon docker cp example.txt mycontainer:/tmp/example.txt # Correct
Quick Reference
| Command | Description |
|---|---|
| docker cp file.txt container:/path/ | Copy file.txt into container at /path/ |
| docker cp container:/path/file.txt ./ | Copy file.txt from container to current host directory |
| docker cp folder container:/path/ | Copy folder and contents into container |
| docker cp file.txt container:/path/file.txt | Copy and rename file inside container |
Key Takeaways
Use
docker cp to copy files between host and container easily.Always include a colon
: between container name and destination path.The container must be running to copy files into it.
Check container name or ID carefully to avoid errors.
You can copy both files and folders with
docker cp.