Docker

How to Copy Files with Docker cp to your Docker Container

Docker is a well-liked, free, and open-source tool that is utilized to encapsulate projects and software in isolated environments. This can be done by Docker containers. While working with a docker container, the user is mostly required to add or copy the file to the container after the container’s deployment for debugging purposes, adding dependencies files, or enhancing the program.

This post has illustrated the method for copying files through the “docker cp” command to the container.

How to Copy or Add Files to Docker Container Through “docker cp”?

The “docker cp” command is the robust command of Docker that is utilized for copying the file to and from the container. To copy or move the file to the container through the “docker cp” command, take a look at the provided steps.

Step 1: Make Dockerfile
First, make a file and set its name as “Dockerfile”. After that, add the instructions to containerize the program. For demonstration, we are containerizing the HTML program:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Let’s move ahead to create the container’s image from Dockerfile.

Step 2: Generate Image
Next, generate the image or snapshot for the container through the given command:

docker build -t html-img .

In the above command, the “-t” flag is utilized to set the image name:

Step 3: Execute the Container
Now, execute the container on port 80 by running the given command. Here “–name” flag sets the container’s name, and the “-p” defines the exposing port for the container:

docker run -p 80:80 --name html-cont html-img

Next, navigate to the localhost and check if the container is executing or not:

Step 4: Run Container’s Shell
Utilize the “docker exec” command to run the container shell. In the below command “html-cont” is a container’s name:

docker exec -it html-cont sh

Next, navigate to the “html” directory where we have set the HTML program through Dockerfile:

cd 'usr/share/nginx/html/'

To view all the directories and files of the current folder, use “ls”:

ls

Here, you can see we currently have two files “50.html” and “index.html”:

Let’s move ahead to create another HTML program file and copy this file to the container’s path.

Step 5: Create a New HTML File
Create a new HTML file named “index1.html” and paste the given snippet into the file:

<html>
 <head>
  <style>
   body{
    background-color:rgb(106, 103, 103);
   }
   h1{
    color:rgb(221, 219, 226);
    font-style: italic;
   }
  </style>
 </head>
 <body>
  <h1> This is Second HTML page </h1>
 </body>
</html>

Step 6: Copy the New “index1.html” File to the Container
Now, simply use the “docker cp <path-to-new-file> <container-name>:<path-to-copy-file>” command to copy the new file to the container’s path as shown below:

docker cp ./index1.html html-cont:/usr/share/nginx/html

The output indicates that the file is copied successfully to the container’s path:

Step 7: Verification
Open the command line tab where the container shell is executing in step 4. Again, open the path where the new HTML file is copied:

cd 'usr/share/nginx/html/'

To view the current directory, use the given command:

pwd

Next, display all the files and directories of the current folder:

ls

The output shows that we have copied the new HTML file to the specified container path:

In order to execute the new file, open the “localhost/<file-name>” in the browser:

That is all about copying the file to the container through “docker cp”.

Conclusion

To copy or add the file to the container’s path by utilizing the “docker cp” command, first, run the container with the help of the “docker run” command. After that, utilize the “docker cp <path-to-new-file> <container-name>:<path-to-copy-file>” command to copy the file to the container. This blog has illustrated the method for copying the file to the container through “docker cp”.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.