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

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github