1. Introduction
1.绪论
As more of our applications are deployed to cloud environments, working with Docker is becoming a necessary skill for developers. Often when debugging applications, it is useful to copy files into or out of our Docker containers.
随着我们越来越多的应用程序被部署到云环境中,使用Docker工作正成为开发人员的一项必要技能。通常在调试应用程序时,将文件复制到我们的Docker容器中或从容器中复制出来是非常有用的。
In this tutorial, we’ll look at some different ways we can copy files to and from Docker containers.
在本教程中,我们将研究一些不同的方法,可以将文件复制到Docker容器中或从Docker容器中复制出来。
2. Docker cp Command
2.Docker cp 命令
The quickest way to copy files to and from a Docker container is to use the docker cp command. This command closely mimics the Unix cp command and has the following syntax:
将文件复制到Docker容器或从Docker容器中复制文件的最快速方法是使用docker cp命令。该命令密切模仿了Unix的cp命令,其语法如下。
docker cp <SRC> <DEST>
docker cp <SRC> <DEST>
Before we look at some examples of this command, let’s assume we have the following Docker containers running:
在我们看这个命令的一些例子之前,让我们假设我们有以下Docker容器在运行。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1477326feb62 grafana/grafana "/run.sh" 2 months ago Up 3 days 0.0.0.0:3000->3000/tcp grafana
8c45029d15e8 prom/prometheus "/bin/prometheus --c…" 2 months ago Up 3 days 0.0.0.0:9090->9090/tcp prometheus
The first example copies a file from the /tmp directory on the host machine into the Grafana install directory in the grafana container:
第一个例子从主机上的/tmp目录复制一个文件到grafana容器中的Grafana安装目录。
docker cp /tmp/config.ini grafana:/usr/share/grafana/conf/
We can also use container IDs instead of their names:
我们还可以使用容器的ID,而不是它们的名字。
docker cp /tmp/config.ini 1477326feb62:/usr/share/grafana/conf/
To copy files from the grafana container to the /tmp directory on the host machine, we just switch the order of the parameters:
要从grafana容器中复制文件到主机上的/tmp目录,我们只需切换参数的顺序。
docker cp grafana:/usr/share/grafana/conf/defaults.ini /tmp
We can also copy an entire directory instead of single files. This example copies the entire conf directory from the grafana container to the /tmp directory on the host machine:
我们也可以复制整个目录而不是单个文件。这个例子将整个conf目录从grafana容器复制到主机上的/tmp目录。
docker cp grafana:/usr/share/grafana/conf /tmp
The docker cp command does have some limitations. First, we cannot use it to copy between two containers. It can only be used to copy files between the host system and a single container.
docker cp命令确实有一些限制。首先,我们不能用它来在两个容器之间复制。它只能被用来在主机系统和单个容器之间复制文件。
Second, while it does have the same syntax as the Unix cp command, it does not support the same flags. In fact, it only supports two:
第二,虽然它与Unix cp命令具有相同的语法,但它不支持相同的标志。事实上,它只支持两个。
-a: Archive mode, which preserves all uid/gid information of the files being copied
-L: Always follow symbolic links in SRC
-a。归档模式,保留被复制文件的所有 uid/gid 信息
。
-L。始终遵循SRC中的符号链接
3. Volume Mounts
3.卷轴安装
Another way to copy files to and from Docker containers is to use a volume mount. This means we make a directory from the host system available inside the container.
另一种复制文件到Docker容器或从Docker容器复制文件的方法是使用卷装载。这意味着我们让主机系统的一个目录在容器内可用。
To use volume mounts, we have to run our container with the -v flag:
要使用卷挂载,我们必须用-v标志运行我们的容器:。
docker run -d --name=grafana -p 3000:3000 grafana/grafana -v /tmp:/transfer
The command above runs a grafana container and mounts the /tmp directory from the host machine as a new directory inside the container named /transfer. If we wanted to, we could provide multiple -v flags to create multiple volume mounts inside the container.
上面的命令运行一个grafana容器,并将主机上的/tmp目录挂载为容器内的一个新目录,名为/transfer。如果我们愿意,我们可以提供多个-v标志来在容器内创建多个卷挂载。
There are several advantages to this approach. First, we can use the Unix cp command, which has many more flags and options over the docker cp command.
这种方法有几个优点。首先,我们可以使用Unix cp命令,它比docker cp命令有更多的标志和选项。
The second advantage is that we can create a single shared directory for all Docker containers. This means we can copy directly between containers as long as they all have the same volume mount.
第二个优点是,我们可以为所有Docker容器创建一个单一的共享目录。这意味着我们可以在容器之间直接复制,只要它们都有相同的卷装载。
Keep in mind this approach has the disadvantage that all files have to go through the volume mount. This means we cannot copy files in a single command. Instead, we first copy files into the mounted directory, and then into their final desired location.
请记住,这种方法有一个缺点,即所有的文件都必须经过卷挂载。这意味着我们不能用一条命令复制文件。相反,我们首先要把文件复制到挂载的目录中,然后再复制到它们最终想要的位置。
Another drawback to this approach is we may have issues with file ownership. Docker containers typically only have a root user, which means files created inside the container will have root ownership by default. We can use the Unix chown command to restore file ownership if needed on the host machine.
这种方法的另一个缺点是我们可能会有文件所有权的问题。Docker 容器通常只有一个根用户,这意味着在容器内创建的文件默认具有根所有权。如果需要,我们可以使用Unix chown命令来恢复主机上的文件所有权。
4. Dockerfile
4.Dockerfile
Dockerfiles are used to build Docker images, which are then instantiated into Docker containers. Dockerfiles can contain several different instructions, one of which is COPY.
Dockerfiles被用来构建Docker镜像,然后将其实例化为Docker容器。Dockerfiles可以包含几个不同的指令,其中一个是COPY。。
The COPY instruction lets us copy a file (or files) from the host system into the image. This means the files become a part of every container that is created from that image.
COPY指令让我们从主机系统复制一个(或多个)文件到镜像中。这意味着这些文件将成为从该镜像创建的每个容器的一部分。
The syntax for the COPY instruction is similar to other copy commands we saw above:
COPY指令的语法与我们上面看到的其他复制命令相似。
COPY <SRC> <DEST>
Just like the other copy commands, SRC can be either a single file or a directory on the host machine. It can also include wildcard characters to match multiple files.
就像其他复制命令一样,SRC可以是一个单独的文件或主机上的一个目录。它还可以包括通配符来匹配多个文件。
Let’s look at some examples.
让我们看看一些例子。
This will copy a single from the current Docker build context into the image:
这将从当前的Docker构建环境中复制一个单体到镜像中。
COPY properties.ini /config/
And this will copy all XML files into the Docker image:
而这将把所有XML文件复制到Docker镜像中。
COPY *.xml /config/
The main downside of this approach is that we cannot use it for running Docker containers. Docker images are not Docker containers, so this approach only makes sense to use when the set of files needed inside the image is known ahead of time.
这种方法的主要缺点是,我们不能将其用于运行Docker容器。Docker镜像不是Docker容器,所以这种方法只有在提前知道镜像内部所需的文件集时才有意义。
5. Conclusion
5.总结
In this tutorial, we’ve seen how to copy files to and from a Docker container. Each has some pros and cons, so we must pick the approach that best suits our needs.
在本教程中,我们已经看到了如何将文件复制到Docker容器中或从Docker容器中复制出来。每种方法都有一些优点和缺点,所以我们必须选择最适合我们需求的方法。