Copy files from host to Docker container
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker.
docker cp
Copy the file from the host to the Docker container
using
docker cp
The command is one of the simplest methods we can use to copy files and directories from the host to the Docker container. We will be using Ubuntu 20.4.5 and the latest version of Docker at the time, which is v 19.03.
grammar:
docker cp {options} PATH_HOST CONTAINER:PATH_TO_CONTAINER
in,
-
PATH_HOST
Specify the source path on the host where we want to copy the files. -
PATH_TO_CONTAINER
is the destination path where we want to store the file on the container.
example:
We will start by creating the target container. We will use the official Nginx image in this article, which we can pull from the Docker desktop or terminal by running the following command.
$ docker pull nginx
Output:
Using default tag: latest
latest: Pulling from library/nginx
5eb5b503b376: Pull complete
1ae07ab881bd: Pull complete
78091884b7be: Pull complete
091c283c6a66: Pull complete
55de5851019b: Pull complete
b559bad762be: Pull complete
Digest: sha256:2834dc507516af02784808c5f48b7cbe38b8ed5d0f4837f16e78d00deb7e7767
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
We use the following command to verify that we have successfully pulled the image from the registry.
$ docker images
Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c316d5a335a5 2 weeks ago 142MB
We can now create a container. Using the run command, we will create a container and deploy it in detached mode listening on external port 8080 and internal port 80.
The following command will create a container using the Nginx base image.
$ docker run -d -p 8080:80 nginx
We can verify that we have successfully created a new container by running docker ps -a
the command. This will list all of the containers we have made recently and in the past, along with their container IDs and respective images.
We can do this as shown here.
docker ps -a
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c57de10362b nginx "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp lucid_booth
Before using docker cp
the command, we need to create a file that we will copy from the host machine to the container we just created.
We will create a new_file.txt
file called and touch
store it in our home directory using the command.
Here is the command to do this.
$ touch new_file.txt
We can check the directory to make sure that we have ls
successfully created the file using the command as shown below.
isaac@DESKTOP-HV44HT6:~$ ls
new_file.txt
is , of course, new_file.txt
an empty file; however, we want to focus on using docker cp
the command to move it from the host to the container we created earlier.
We then copy this file into the container we created.
$ docker cp new_file.txt 0c57de10362b:/usr/share
Just like this, we will new_file.txt
copy to the container /usr/share
with ID 0c57de10362b under the folder.
We can determine this by running the command interactively through an interactive SSH session into the running container
using the -h docker exec
and -h flags.-it
$ docker exec -it 0c57de10362b /bin/bash
Now that we are logged into the container, we want to list the files to verify that we copied this file into this container. We can do this using ls
the command again, but specifying the folder we want to access.
root@0c57de10362b:/# ls -lh /usr/share/
Output:
Similarly, we can also copy a directory containing many files by simply changing the location of the file to be read from the directory as shown below.
$ docker cp src/directory/. container-id:/target_location/directory
docker cp
Although the command is easy to use, it has some limitations. Its syntax cp
is different from Unix commands and only a limited number of tags are supported.
Also, this can only be used to copy files from the host to the container, not between containers.
docker volume
Copy the file from the host to the Docker container
using
A better way to copy files between containers is via docker volume
.
We can attach the volume containing the files we want to copy to the container using either the -v
or flags
during container creation .-mount
To use volumes in Docker, we first docker volume
create one using the command as shown below.
$ docker volume create volume_one
Using docker volume ls
, we can verify that we have successfully created the volume. This will list the names of volumes created now and in the past, along with the name of the driver.
isaac@DESKTOP-HV44HT6:~$ docker volume ls
DRIVER VOLUME NAME
local volume_one
Now that we have created a volume, the next step involves run
creating a new container using the command and attaching -v
the volume that we created using the flag.
-v
The -v flag allows us to attach new volumes to the container we create. We will use the latest version of the Nginx base image to make this container.
$ docker run -d -v volume_one:/app nginx:latest
f2457d3eb8fe14645b9e3938017c02c46a657c9ad3323ff4c03924d2fddd7046
We can use the Docker inspect command to return low-level information about Docker objects. We will investigate whether our volume was successfully attached to the container we created.
Since we have not assigned any name to this container, we will use the container ID to check it. We will use the latest container ID instead of the earlier ones.
$ docker ps -a
Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2457d3eb8fe nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp brave_kirch
0c57de10362b nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp lucid_booth
We can now use this container ID to inspect the container.
$ docker inspect f2457d3eb8fe
Output:
docker volume
One of the advantages of the command is the creation of a single directory that is shared with multiple containers.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Tagging images with Docker and Docker Compose
Publish Date:2025/03/25 Views:70 Category:Docker
-
When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images. This article discusses tagging ima
Creating a database user with Docker Postgres
Publish Date:2025/03/24 Views:166 Category:Docker
-
When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save
Adding unsafe registry keys in Docker
Publish Date:2025/03/24 Views:130 Category:Docker
-
While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer
List all images in Docker Registry V2
Publish Date:2025/03/24 Views:131 Category:Docker
-
After several iterations, Docker Registry was upgraded from version 1 to version 2. Especially being new, some commands needed to be included or adequately documented on its official documentation website. An example is getting a list of im
Executing multiple commands in Docker-Compose
Publish Date:2025/03/24 Views:152 Category:Docker
-
Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Docker-compose is an advanced, must-have tool for managing multi-co
View logs for a specific Docker Compose service
Publish Date:2025/03/24 Views:60 Category:Docker
-
When using docker-compose up, we can see the logs of all containers in the YAML file; however, if we specify a specific container service, the output will not show any service dependencies in the log. Therefore, this article will explore ho
Differences between Docker and Docker Compose
Publish Date:2025/03/24 Views:147 Category:Docker
-
Both docker and docker compose docker run Docker containers. The two Docker constructs are comparable, but that's about it. This article will discuss docker compose the main differences between docker and . Differences between Docker and Do
Recreate the container from the new image using Docker-Compose
Publish Date:2025/03/24 Views:178 Category:Docker
-
As we develop our applications, we often make changes or add more features to make the application more effective for the different users who interact with the application. When using Docker, we need to ensure that the changes or features w
Exposing multiple ports in a Docker container
Publish Date:2025/03/24 Views:187 Category:Docker
-
There are different types of communication on the Internet, the most common ones include file transfers, sending emails, and serving web pages. To make this communication possible, we make use of port numbers that help identify the type of