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
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
Publish Date:2025/03/26 Views:97 Category:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
Publish Date:2025/03/26 Views:125 Category:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
Publish Date:2025/03/26 Views:202 Category:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
Publish Date:2025/03/26 Views:88 Category:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
Publish Date:2025/03/26 Views:140 Category:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application