Home » DevOps » Docker » Docker Copy Files

About Mohammad Meraj Zia

Senior Java Developer

Docker Copy Files

1. Introduction

In this example, we will find out how to use copy files inside Docker. Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow you to run many containers simultaneously on a given host. Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host. You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.

2. Docker Copy file

Use the docker cp command to copy files/folders between a container and the local filesystem.

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

The docker cp utility copies the contents of SRC_PATH to the DEST_PATH. You can copy a file or a folder from a docker container to a local file system or vice-versa. If - is specified for either the SRC_PATH or DEST_PATH, you can also stream a tar archive from STDIN or to STDOUT. You can use the docker cp command on a stopped container. The docker cp command assumes container paths are relative to the container’s / (root) directory. This means supplying the initial forward slash is optional; The command sees <my-container>:/opt/example.txt and <my-container>:opt/example.txt as identical. Local machine paths can be an absolute or relative value. The command interprets a local machine’s relative paths as relative to the current working directory where docker cp is run.

The cp command behaves like the Unix cp -a command in that directories are copied recursively with permissions preserved if possible. Ownership is set to the user and primary group at the destination.

3. Example

In this section we will look at a working example of docker cp command. . This example assumes that you have docker installed on your machine. To check if docker is installed run the docker -v command on your terminal:

~$ docker -v
Docker version 20.10.8, build 3967b7d

We will use the MySQL docker image for this example. MySQL is a widely used, open-source relational database management system (RDBMS). With its proven performance, reliability, and ease of use, MySQL has become the leading database choice for web-based applications, covering the entire range from personal projects and websites, via e-commerce and information services, all the way to high-profile web properties including Facebook, Twitter, YouTube, Yahoo! and many more.

Run the below command to start the MySQL in docker:

~$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:latest
6d442460ee26f70582be3e618cb81be93b1ab318a3f40ce5b0951aa94d28f6db
Figure 1

The long id is the container id. The -d option tells the docker to run in detached mode. To check if the image was downloaded correctly and the container is running run the below command:

~$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS      NAMES
6d442460ee26   mysql:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp   mysql
~$
Figure 2

The docker exec command allows you to run commands inside a Docker container. The following command line will give you a bash shell inside your mysql container:

~$ docker exec -it mysql /bin/bash
root@6d442460ee26:/#
Figure 3

Let us create a file in the tmp folder then copy this file from the docker container to the local host:

root@a76b64877ee5:/# cd tmp
root@a76b64877ee5:/tmp# ls -lart > test.txt
root@a76b64877ee5:/tmp# cat test.txt 
total 8
drwxr-xr-x 1 root root 4096 Jun 15 10:17 ..
-rw-r--r-- 1 root root    0 Jun 15 10:24 test.txt
drwxrwxrwt 1 root root 4096 Jun 15 10:24 .
Figure 4

Now let us copy this file onto the local host, we need to exit from the container

~$ cat test.txt 
total 8
drwxr-xr-x 1 root root 4096 Jun 15 10:17 ..
-rw-r--r-- 1 root root    0 Jun 15 10:24 test.txt
drwxrwxrwt 1 root root 4096 Jun 15 10:24 .

Now let us copy a file from localhost into the docker container:

~$ ls -lart > test2.txt
~$ docker cp test2.txt mysql:/tmp/test2.txt
~$ docker exec -it mysql /bin/bash
root@a76b64877ee5:/# cd tmp
root@a76b64877ee5:/tmp# ls -lart
total 20
-rw-r--r-- 1 root root     145 Jun 15 10:24 test.txt
-rw-r--r-- 1  501 dialout 5011 Jun 15 10:29 test2.txt
drwxr-xr-x 1 root root    4096 Jun 15 10:29 ..
drwxrwxrwt 1 root root    4096 Jun 15 10:29 .

4. Summary

In this article, we looked at how to copy a file from localhost into a docker container and vice-versa using docker copy. First, we discussed what Docker is and why it is so useful. Then in the end we looked at a working example where we started a MySQL docker container – then copied the files from and into this container. It is not possible to copy certain system files such as resources under /proc, /sys, /dev, tmpfs, and mounts created by the user in the container. However, you can still copy such files by manually running tar in docker exec.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments