将文件从主机复制到 Docker 容器
本文将讨论和演示我们可以用来将文件从主机传输到 Docker 中正在运行的容器的方法。
使用 docker cp
将文件从主机复制到 Docker 容器
docker cp
命令是我们可以用来将文件和目录从主机复制到 Docker 容器的最简单方法之一。我们将使用 Ubuntu 20.4.5 和当时最新版本的 Docker,即 v 19.03。
语法:
docker cp {options} PATH_HOST CONTAINER:PATH_TO_CONTAINER
其中,
-
PATH_HOST
指定我们要复制的文件在主机上的源路径。 -
PATH_TO_CONTAINER
是我们要在容器上存储文件的目标路径。
例子:
我们将从创建目标容器开始。我们将在本文中使用官方 Nginx 镜像,我们可以通过运行以下命令从 Docker 桌面或终端拉取该镜像。
$ docker pull nginx
输出:
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
我们使用下面的命令来验证我们是否成功地从注册表中拉取了镜像。
$ docker images
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c316d5a335a5 2 weeks ago 142MB
我们现在可以创建一个容器。使用 run 命令,我们将创建一个容器并以分离模式部署它,以监听外部端口 8080 和内部端口 80。
下面的命令将使用 Nginx 基础镜像创建一个容器。
$ docker run -d -p 8080:80 nginx
我们可以通过运行 docker ps -a
命令来验证我们是否已成功创建新容器。这将列出我们最近和过去制作的所有容器,以及它们的容器 ID 和各自的镜像。
我们可以按照此处所示执行此操作。
docker ps -a
输出:
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
在使用 docker cp
命令之前,我们需要创建一个文件,我们将从主机复制到我们刚刚创建的容器。
我们将创建一个名为 new_file.txt
的文件,并使用 touch
命令将其存储在主目录中。
这是执行此操作的命令。
$ touch new_file.txt
我们可以查看目录以确保我们已使用 ls
命令成功创建文件,如下所示。
isaac@DESKTOP-HV44HT6:~$ ls
new_file.txt
当然,new_file.txt
是一个空文件;但是,我们希望专注于使用 docker cp
命令将其从主机移动到我们之前创建的容器。
然后我们将此文件复制到我们创建的容器中。
$ docker cp new_file.txt 0c57de10362b:/usr/share
就像这样,我们将 new_file.txt
复制到文件夹/usr/share
下 ID 为 0c57de10362b 的容器中。
我们可以通过使用命令 docker exec
和 -it
标志以交互方式运行命令通过交互式 SSH 会话进入正在运行的容器来确定这一点。
$ docker exec -it 0c57de10362b /bin/bash
现在我们已经登录到容器中,我们要列出文件以验证我们是否已将此文件复制到此容器中。我们可以再次使用 ls
命令,但指定我们要访问的文件夹。
root@0c57de10362b:/# ls -lh /usr/share/
输出:
同样,我们也可以通过简单地更改要从目录中读取的文件的位置来复制包含许多文件的目录,如下所示。
$ docker cp src/directory/. container-id:/target_location/directory
docker cp
命令虽然易于使用,但有一些限制。它的语法与 Unix cp
命令不同,只支持有限数量的标签。
此外,这只能用于将文件从主机复制到容器,而不是容器之间。
使用 docker volume
将文件从主机复制到 Docker 容器
在容器之间复制文件的更好方法是通过 docker volume
。
我们可以在容器创建过程中使用 -v
或 -mount
标志中的任何一个将包含我们想要复制的文件的卷附加到容器。
要在 Docker 中使用卷,我们首先使用 docker volume
命令创建一个,如下所示。
$ docker volume create volume_one
使用 docker volume ls
,我们可以验证我们是否已成功创建卷。这将列出现在和过去创建的卷的名称以及驱动程序的名称。
isaac@DESKTOP-HV44HT6:~$ docker volume ls
DRIVER VOLUME NAME
local volume_one
现在我们已经创建了一个卷,下一步涉及使用 run
命令创建一个新容器并附加我们使用 -v
标志创建的卷。
-v
标志允许我们将新卷附加到我们创建的容器。我们将使用最新版本的 Nginx 基础镜像制作这个容器。
$ docker run -d -v volume_one:/app nginx:latest
f2457d3eb8fe14645b9e3938017c02c46a657c9ad3323ff4c03924d2fddd7046
我们可以使用 Docker 检查命令返回有关 Docker 对象的低级信息。我们将调查我们的卷是否已成功附加到我们创建的容器。
由于我们没有为这个容器分配任何名称,我们将使用容器 ID 来检查它。我们将使用最新的容器 ID 而不是较早的容器 ID。
$ docker ps -a
输出:
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
我们现在可以使用这个容器 ID 来检查容器。
$ docker inspect f2457d3eb8fe
输出:
docker volume
命令的优点之一是创建与多个容器共享的单个目录。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类: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
发布时间:2025/03/26 浏览次数:165 分类: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
发布时间:2025/03/26 浏览次数:131 分类: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
发布时间:2025/03/26 浏览次数:107 分类: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
发布时间:2025/03/26 浏览次数:97 分类: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
发布时间:2025/03/26 浏览次数:124 分类: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
发布时间:2025/03/26 浏览次数:202 分类: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
发布时间:2025/03/26 浏览次数:87 分类: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
发布时间:2025/03/26 浏览次数:140 分类: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