1. Overview

This quick tutorial will focus on how to copy files, folders from host to Docker container and vice versa by using the docker cp command.

2. The docker cp command

The docker cp command which allows us to copy files/folders between a container and the local filesystem, has the syntax as the following:

Where OPTIONS can be:

Name, shorthand Default Description
--follow-link, -L false Always follow symbol link in SRC_PATH

Note that to execute the command, we need to know exactly the container id, name on which we want to perform action. The command to list all available container is:

You can refer to my recent post to learn more about Docker commands.

3. Copy Files, Folders from Host to Docker Container

Let’s see several examples that we copy a file from host to a Docker container:

The above command copy the file abc.txt to the / directory in the my-alpine container.

Let’s see another example which we copy the file abc.txt to the /tmp folder in the my-alpine container and change the file name to bcd.txt.

4. Copy Files, Folders from Docker Container to Host

Let’s see the following example which we copy the file /root/data/xyz.txt from the my-alpine Docker container to the /tmp/ folder on the host machine:

Another example which copy the same file from container to the current directory on the host and then change the file name:

5. Conclusion

The quick tutorial has just illustrated us how to copy files, folders from host to Docker container and vice versa by using the docker cp command. Currently, there is no direct way to copy files, folders between containers, however we can copy data from containers to host machine a folder on host machine temporarily then copy them to other containers. Another approach related to shared volume will be shared on a next tutorial.

Below are other related tutorial for your references:

Copy Files, Folders from Host to Docker Container and Vice Versa

How to Get IP Address of a Docker Container

How To Pull A Docker Image And Run A Container

Basic Docker commands

Install Docker on CentOS 7

Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step

Introduction to Docker Compose

Vagrant Docker Provider Tutorial

Using Apache Kafka Docker

 

0 0 vote
Article Rating