How to Copy Files with Docker cp to your Docker Container

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

This is a guide to copying files using Docker.

The “docker cp example” is a command that allows users to copy files from their Docker Container to the host machine.

How to Copy Files with Docker cp to your Docker Container

This guide is for you if you need to transfer files from your Docker host to your Docker container.

You’ll learn how to transfer files from a Docker host to a Docker container using several methods in this article.

Let’s get started!

Prerequisites

If you want to follow along, make sure you have the following items:

  • On the Linux host, Docker is installed. Docker v19.03.11 is used in this lesson. Docker version may be used to verify your Docker version.

Installation and Use of Docker on Ubuntu (In the Real World)

Using the docker cp command to copy files

To begin, you’ll learn how to use the docker cp command to transfer files from the Docker host to the containers. The docker cp command moves files or directories from a container to your Docker host’s local filesystem and vice versa.

Let’s look at an example of how to utilize the Docker cp command.

1. On your local computer, open a terminal.

2. Using the touch command, create a file called myfile.txt. From the docker host to the container, myfile.txt will be copied.

3. Run the docker service command. In the background, the docker run below will construct a new container. The command below has three arguments, which are detailed below:

  • d flag that keeps the container alive in the background until it is removed.
  • The p flag broadcasts the port 80 of a container to the host.
  • The image that will be used to operate the container will be nginx.

docker run -d -p 80:80 nginx sudo

The Docker run command is executed. The Docker run command is executed.

4. Use the docker ps command to see whether the Docker container was successfully built. Following the docker ps command, a new value should be produced under the attribute CONTAINER ID (in this example, ccae4670f030) using the NGINX image, indicating that the container was successfully built.

Using the docker ps command to verify the docker container Using the docker ps command to verify the docker container

The Docker cp command has the following syntax before it is executed:

  • CONTAINER: SRC PATH defines the container’s source path.
  • DEST PATH is the host’s destination path.
  • CONTAINER: DEST PATH is the container’s destination path.

You may additionally add the following arguments to the command with the following parameters:

  • Using archive or a — Copies all file and folder permissions for all users and main groups.
  • Specifying L – Specifying L allows any symbolic link in the Source Path to be transferred to the destination path.

# Copying from a Container to a Docker Host: docker cp options CONTAINER:SRC PATH DEST PATH # Copying from a Docker Host to a Container: docker cp options SRC PATH CONTAINER:DEST PATH

5. Finally, run the docker cp command. The myfile.txt file you prepared previously will be copied to the containers /usr/share directory using the docker cp command. The container ID to which myfile.txt will be transferred is ccae4670f030.

ccae4670f030:/usr/share sudo docker cp myfile.txt

6. Finally, SSH into the operating container using the docker exec command and /bin/bash, which is the Linux system’s default shell for user login.

  • The I parameter indicates that you want to start a live SSH session with the container. Even if the container is not connected, the I flag does not end the SSH connection.
  • The t option creates a virtual terminal that may be used to execute commands interactively. /bin/bash sudo docker exec -it ccae4670f030

/bin/bash sudo docker exec -it ccae4670f030

When you run docker exec, you’ll see that you’re now connected to the container’s shell.

Using the docker exec command, SSH into the operating container. Using the docker exec command, SSH into the operating container.

7. Using the ls command, check that myfile.txt has been transferred to the container after signing in.

  • In Unix and Unix-like operating systems, the ls command is used to list computer files.
  • grep will look for all files or folders in the usr/share directory, beginning with string my.

Verifying the contents of the container's files Verifying the contents of the container’s files

Using DockerFile to Copy Files

You learnt how to put files into the container using the Docker cp command in the previous section. What if you need to copy many files at the same time? Running numerous instructions is unquestionably inefficient! Why not use Dockerfile with Transfer commands to copy files or directories into containers instead of doing numerous cp commands?

You may skip the manual copying procedures in the preceding section by deploying a container and copying the files/folders using Dockerfile. A Dockerfile is a text file that includes all of the instructions that may be used to create an image from the command line.

Let’s start by making a Dockerfile, then running a container from it, and then copying the files.

1. Create a folder called /host-to-container-copy-demo, then change (cd) to it. All of the files you’ll be making in this example will be in this folder.

/host-to-container-copy-demo/mkdir cd ~/host-to-container-copy-demo

2. Create two text files, myfile1.txt and myfile2.txt, and save them to the /host-to-container-copy-demo directory.

myfile1.txt # Hello, and welcome to my first file! This file will be transferred from the Docker host to the Docker Container’s /usr/share directory.

# mysecond.txt Hello This is my second submission! This file will be transferred from the Docker host to the Docker Container’s /tmp directory.

3. In the /host-to-container-copy-demo directory, create a new file and copy/paste the following settings. Save the file as Dockerfile. When Docker is finished, it will utilize this DockerFile to perform all of the tasks required to create a new Docker image from any base image.

The DockerFile below provides the following stages and directions for creating the new container:

  • FROM– The FROM command establishes the Base Image for following instructions and starts a new construction stage.
  • COPY– The COPY command copies a file from the host computer to the container.

# Dockerfile instructions for creating a new image based on the base image (ubuntu) # Using the base image # Copying myfile1.txt to the containers /usr/share directory using ubuntu:latest FROM ubuntu:latest COPY /usr/share/myfile1.txt # Copying myfile2.txt to the /tmp directory of the container COPY /tmp/myfile2.txt

4. Using the tree command, verify that all of the essential files to generate the new image are there. Under the /host-to-container-copy-demo directory, you should see Dockerfile, myfile1.txt, and myfile2.txt.

The tree command is used to verify files and directories. The tree command is used to verify files and directories.

5. Run the docker build command to create the image. The updated ubuntu image image is tagged with the latest and using the t flag. enables docker to choose all required files from the current working directory.

docker build -t updated ubuntu image:latest sudo docker build -t updated ubuntu image:latest

Using the docker build command to create the Docker image Using the docker build command to create the Docker image

6. Run the docker images command to check the newly created image updated ubuntu image. The REPOSITORY property should be noted. The tag formed with the -t option in the preceding step is this attribute.

Attribute for RepositoryAttribute for Repository

7. Finally, use the docker run command to start the docker container using the freshly created image. The -it argument tells Docker to create a pseudo-terminal attached to the container’s stdin. Bash is the Linux system’s default shell for user login.

updated ubuntu image bash sudo docker run -it

You’re now in the Bash shell of the Docker container, as seen below.

Using the docker run command to start the container. Using the docker run command to start the container.

8. Run the ls command to see whether files in the folders /tmp and /usr/share were successfully moved to the container.

Checking to see whether the files were properly transferred to the containerChecking to see whether the files were properly transferred to the container

Using the Docker Volume Command to Mount a Storage Volume and Access Files

You’ve learned how to transfer files from the host to the container using two distinct methods: the docker cp command and a DockerFile. This time, we’ll use the docker volume command to effortlessly share file systems across hosts and containers.

If you’re still logged into the terminal, do the following:

1. Run the docker volume create command on the Docker host to create a volume. The command below will create a volume called my-vol. my-vol sudo docker volume create

my-vol sudo docker volume create

2. Run the docker volume ls command to see whether the volume was successfully built. The docker volume ls command displays the volume information. After executing the docker volume ls command, my-vol will appear in the VOLUME NAME attribute, indicating that the volume was successfully created.

a list of docker volumes a list of docker volumes

3. Use the docker run command to start the container.

  • The nginx: newest image is used by the container volume testing.
  • The d flag keeps the container alive in the background until it is erased.
  • The v flag mounts the volume my-vol generated on the Docker host to the target /app directory of the container.

nginx:latest sudo docker run -d —name volume testing -v my-vol:/app

Docker command and volume attachment to the container Docker command and volume attachment to the container

4. Use the docker inspect command to see whether the volume my-vol you generated earlier is mounted correctly in the container. The inspect command in Docker displays container information. Following the execution of the docker inspect command, all information of the selected container (volume testing) will be shown, including the mount details, as seen below.

examine volume testing with sudo docker

The Image snapshot below shows that the volume (my-vol) you established on the host is mounted successfully with the container’s /app directory.

Verifying the container's volumes Verifying the container’s volumes

Conclusion

You learned how to transfer files or directories from a Docker host to a container using the Docker cp command, Docker volume commands, and Dockerfile in this lesson.

So, when moving data from the host to Docker containers, which strategy will you choose next?

The “docker copy file from container to host” is a command that allows users to copy files with Docker. The tool is useful for copying files between the host and the container.

Related Tags

  • docker cp command
  • docker cp folder
  • docker cp no such container:path
  • docker cp wildcard
  • docker cp overwrite

Table of Content