DevOps

How to copy files from host to Docker container?


Introduction

When data, configurations, or scripts for testing, debugging, or further processing need to be transferred then copying files between a host and a Docker container is very important. Thus, it allows us to interact with the containerized environment without needing to rebuild images and to compromise container isolation.

Why do we need to copy files from host to Docker container?

To introduce new data or new config without restarting or recreating containers, we have to copy files from the host to a Docker container. It smoothes out workflows while working with development, debugging, data sharing between host system and the containerized application.

Solution

To copy files from a docker container to the host, use below commands

docker cp <containerId>:/file/path/within/container /host/path/target

 

Example:

Using Container Name

$ sudo docker cp test-container:/test.jpg .

 

Using Container ID

$ sudo docker cp h8fsd45fsd8fs:/out_read.jpg .

 

To copy files from a host to the docker container, use below commands

docker cp test.txt <containerId>:/test.txt

 

Example:

Using Container Name

$ sudo docker cp test.txt test-container:/test.txt

 

Using Container ID

$ sudo docker cp test.txt h8fsd45fsd8fs:/test.txt

 

 

Conclusion

Copying files between host and docker containers is a very simple yet powerful feature. This allows transferring data, files and configurations easier.

 

Ready to transform your business with our technology solutions?   Contact Us today to Leverage Our DevOps Expertise. 

0

Devops

Related Center Of Excellence