Wednesday, January 19, 2022
Home CONTAINERIZATION How to copy files from host to docker container?

How to copy files from host to docker container?

The easiest way to copy files in a docker container to the host machine or vice versa is to use the docker cp command. This command is very similar to the ‘cp’ command in Unix systems. Directories are copied with permission preserved as much as possible. The syntax is as follows.

Docker copy command syntax

How docker cp command is working?

You can copy files from the container file system to the host machine or the other way around with the help of docker cp command. The container can be stopped or in running mode.

Before starting the demo, let’s list our containers running with the docker ps command.

List of active containers in demo host

Let’s create a sample.txt file on the host to copy. Then, using the above syntax copy this file to the /home folder in the running “nginx” container.

After copying the file, connect to the container with the command docker exec -it <container_name> /bin/bash. Check it! Here is the sample.txt file under the /home folder.

Docker copy process sample demo on the host

We can also use container IDs instead of their names. The first four characters of ID is enough:

docker cp /home/ubuntu/sample.txt d759:/home

How to copy entire directory on host machine to the container?

It is also possible to copy an entire directory instead of a single file. This example copies the entire sample directory which consists of three files from the local host to /etc folder of the Nginx container. This time I will use the ID of the container rather than the name.

Copying folder addressing container ID

How to copy files from container to the host machine?

To copy files from the Nginx container to any directory on the host machine, we just switch the order of the parameters:

docker cp d759:/etc/sample /ubuntu/home

Don’t forget docker cp command does have a limitation. You cannot use it to copy between two containers. It can only be used to copy files between the host system and a single container.

Conclusion

Normally, The best practice is making containers immutable, and even making the Copy on Write (COW) layer to Read-Only(RO). But under development to be able to curate an ultimate image, to resolve an issue, for troubleshooting, and debugging purposes this kind of intervention is forgivebale.

Let us know which kind of method are you using for moving files between containers and host during development stage.

Thanks.

Mustafa Gonen
DevOps engineer, helps companies to balance their needs throughout the software development life cycle, from coding and deployment to maintenance and updates by automation. He believes the beauty of diversity. Working in DevOps culture and being a part of this harmony makes him highly motivated and satisfied.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

How to pass environment variables to Docker containers?

Intro In application development, it is best practice to separate services from configurations. The Twelve-factor app methodology recommends...

How to rename files and folders in Amazon s3?

Intro: Is it possible to rename files and folders in Amazon S3? How to rename files or folders...

How can we make an S3 bucket public?

Amazon S3 buckets are by default private to avoid accidental exposure of personal data by everyone. But it is possible to...

The difference between public and private subnets in Amazon VPC

Intro Subnets, public and private subnets, these are all networking issues. Therefore, network basics are required to understand...

Recent Comments