If you need to copy files from the Docker host to your Docker container, this tutorial is for you.
In this tutorial, you will learn to copy files from the Docker host to a Docker container using various approaches.
Not a reader? Watch this related video tutorial! Not seeing the video? Make sure your ad blocker is disabled.
Let’s get to it!
If you’d like to follow along step-by-step, ensure you have the following:
- A Linux host. This tutorial uses Ubuntu 18.04.5 LTS.
- Docker installed on the Linux host. This tutorial uses Docker v19.03.11. You can confirm your Docker version by running docker version.
Related:How to Install and Use Docker on Ubuntu (In the Real World)
- Any Docker image downloaded and available. This tutorial uses the latest NGINX Docker image available on Docker Hub.
Copying Files with the docker cp Command
To start this tutorial, you will learn to copy files from Docker host to the containers using the docker cp command. The docker cp command copies files or folders between a container and the local filesystem of your Docker host and vice versa.
Let’s learn how to use Docker cp command with an example.
1. Open a terminal on your local machine.
2. Create a file named myfile.txt using the touch command. The myfile.txt will be copied from the docker host to the container.
3. Execute the docker run command. The below docker run will create a new container in the background. The below command contains three parameters described below:
- d flag that runs the container in the background and keeps it alive until deleted.
- p flag publishes a container’s port 80 to the host on port 80.
- nginx will be the image that will be used to run the container.
4. Verify if the Docker container is successfully created using the docker ps command. After you execute the docker ps command, you should see a new value generated under the attribute CONTAINER ID (In the below case ccae4670f030) using the NGINX image that confirms the container is successfully created.
Before you execute the Docker cp command, The syntax of the Docker cp command is:
- CONTAINER: SRC_PATH specifies the source path of the container.
- DEST_PATH is the destination path on the host.
- CONTAINER: DEST_PATH is the destination path on the container.
Additionally, you can also add the options with the below parameters in the command as follows:
- Using archive or a – Copies all the user and primary group permissions of the files and folders.
- Using L – Specifying the L option will allow any symbolic link in the Source Path to be copied to the destination path.
5. Next, execute the docker cp command. The docker cp command will copy the myfile.txt that you created earlier to the containers /usr/share directory. ccae4670f030 is the container ID to which the myfile.txt will be copied.
6. Finally, SSH into the running container by running the docker exec command with /bin/bash, which is used as the default shell for user login of the Linux system.
- The i flag indicating you’d like to open an interactive SSH session to the container. The i flag does not close the SSH session even if the container is not attached.
- The t flag allocates a pseudo-TTY which much be used to run commands interactively.sudo docker exec -it ccae4670f030 /bin/bash
You’ll see below that you’re now connected to the container’s shell when you run docker exec.
7. After logging in to the container, verify if myfile.txt has been copied to the container using the ls command.
- ls is a command to list computer files in Unix and Unix-like operating systems.
- grep will search for all files or folders, starting with string my inside the usr/share directory.
Copying Files using DockerFile
In the previous section, you learned how to copy the files into the container by running the Docker cp command. What if you need to copy multiple files in one go? Certainly, running multiple commands becomes an overhead! To solve running multiple cp commands, why not try copying files or folders into containers using Dockerfile with COPY commands?
Deploying a container and copying the files/folders using Dockerfile allows you to remove the manual copying steps you did in the previous section. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Let’s create a Dockerfile, run a container from it, and finally copy the files.
1. Create a folder named ~/host-to-container-copy-demo, then change (cd) the working directory to that folder. This folder will contain all of the files you’ll be creating in this demo.
2. Now, create two text files named myfile1.txt and myfile2.txt, copy and paste them into the files, and save them to the ~/host-to-container-copy-demo directory.
3. Create another file, copy/paste the below configuration, and save the file as Dockerfile inside the ~/host-to-container-copy-demo directory. When complete, Docker will use this DockerFile to run all the commands necessary to build a new Docker image on top of any base image.
The DockerFile below contains various steps/instructions that will build the new container:
- FROM– The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions.
- COPY– COPY command copies a file from the host machine (Docker host) to the container.
4. Verify all of the required files to build the new image by running the tree command. You should see Dockerfile, myfile1.txt, and myfile2.txt under the ~/host-to-container-copy-demo directory.
5. Next, build the image by running the docker build command. The t flag is used to tag the image updated_ubuntu_image with the latest and. allows docker to pick all the necessary files from the present working directory.
6. Now, verify the newly built image updated_ubuntu_image by running the docker images command. Note the REPOSITORY attribute. This attribute is the tag created with the -t flag in the previous step.
7. Finally, run the docker container using the newly built image by running the docker run command. The -it flag instructs Docker to allocate a pseudo-terminal connected to the container’s stdin. bash provides the default shell for user login of the Linux system.
You’ll see below that you’re now in the Docker container’s Bash shell.
8. Next, verify if files were successfully copied on the container in the directories /tmp and /usr/share by running the ls command.
Mounting a Storage Volume and Accessing Files with the Docker Volume Command
Up to now, you have learned how to copy files from host to container using two different approaches by using the docker cp command and a DockerFile. This time, let’s learn how to easily share file systems between host and containers using the docker volume command.
Assuming you are still logged into the terminal:
1. Create a volume on Docker host by running docker volume create command. The below command will create a volume named my-vol. sudo docker volume create my-vol
2. Verify the volume has successfully been created by running the docker volume ls command. docker volume ls command lists the volume. After running the docker volume ls command, you will see my-vol in the VOLUME NAME attribute that confirms that volume is created successfully.
3. Next, run the container using the docker run command.
- The container named volume_testing uses the nginx: latest image.
- d flag runs the container in the background and keeps it alive until deleted.
- v flag mounts the volume my-vol created on Docker host to the container’s destination /app directory.
4. Verify if the volume my-vol you created previously is mounted properly with the container using the docker inspect command. Docker inspect command provides the information of the container. After running the docker inspect command, it will display all the details of the specified container (volume_testing) details, including the mount details, as shown below.
Below, the Image snapshot confirms that the volume (my-vol) you created on the host is successfully mounted with the container’s /app directory.
In this tutorial, you learned different ways of copying files or folders from Docker host to containers, such as using Docker cp command, Docker volume commands, and Dockerfile.
So which approach are you going to use next while copying data from the host to Docker containers?