How to Copy Files from Host to Docker Container?

Better Stack Team
Updated on August 1, 2022

When creating a docker image, you may want to copy some files from the host machine to the docker image. These files may be native libraries, configuration files, or any other files that will be needed at runtime.

How to add files to Docker container

To copy a specific file from the host machine to the Docker container, you can use the docker cp command:

docker cp source/file.txt container_id:/destination/file.tx
  • source/file.txt - Is a path to the source file
  • container_id - Is the name of the docker container to which the file will be copied to
  • /destination/file.tx - Is a destination within the container to which the file will be copied to

You can copy multiple files by specifying a directory instead of a file

docker cp src/. container_id:/target

Commit changes to new image

Another command you may need is docker commit. It can be useful to commit a container’s file changes or settings into a new image. This allows you to debug a container by running an interactive shell, or to export a working dataset to another server.

docker commit container_id new_image_name
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →