How to copy files to and from a Docker container?

Published on Aug. 22, 2023, 12:18 p.m.

To copy files to and from a Docker container, you can use the docker cp command followed by the source path on the host machine and the destination path within the container, or vice versa. For example, to copy a file from the host machine to a running container called my_container, you can use the following command:

docker cp foo.txt my_container:/bar/foo.txt

This will copy the local file foo.txt to the container’s /bar directory as foo.txt. Similarly, to copy a file from a container to the host machine, you can use the command:

docker cp my_container:/bar/foo.txt foo.txt

This will copy the file foo.txt from the container’s /bar directory to the current directory on the host machine. For more information, refer to the Docker documentation on docker cp.