When running containers, you may have files that you need to copy from the host machine into the container. This can be for a number of reasons, such as configuration files that are specific to your environment and cannot be sourced from standard locations or if you want to leverage a private registry like Atlas as part of your workflow. If this sounds like something you need in your workflow, continue reading this blog post to learn more about how it’s done! There are multiple ways of doing this but the following is the most common. Keep reading to learn more…
Copy files to docker container from host
docker cp command is used to copy files to/from the docker container and host system. See the following example:
docker cp data.txt CONTAINER_ID:/var/files/data.txt
Change CONTAINER_ID with the read id of a running container. The above command will copy the data.txt file from the host machine to the docker container under the /var/files directory.
Copy files from docker container to host
The following command will copy /var/files/data.txt file from the docker container to the host machine.
docker cp CONTAINER_ID:/var/files/data.txt data.txt