Dockerfile 中 COPY 和 ADD 命令的区别
Dockerfile 是包含用于构建 Docker 映像的所有命令的文本文档。 最近,我们已经看到 Docker 被广泛用作管理配置和自动化部署的默认工具。
使用 Dockerfile 的高级功能(例如自动构建)是开发人员采用 Docker 的原因。 其他包括将应用程序与其运行的环境隔离开来,并快速确保安全。
在创建 Docker 容器时,经常需要将文件从宿主机传输到 Docker 镜像中。 这些文件可以包括您的应用程序运行所需的库或属性文件。
Dockerfile 中 COPY 和 ADD 命令的区别
在 Dockerfile 中,我们可以使用 COPY 或 ADD 命令复制这些文件。 这些命令在功能上是相同的; 但是,存在一些差异。
COPY 和 ADD 命令遵循以下语法。
COPY <src> <dest>
ADD <src> <dest>
这两条指令都复制位于本地主机中 <src>
的文件或目录,并将它们添加到容器文件系统中的 <dest>
位置。 例如,在下面的 Dockerfile 中,我们将文件从当前目录复制到 Docker 镜像中的 /var/www 目录中。
# base image
FROM python
# Set your working directory
WORKDIR /var/www/
# Copy the necessary files
COPY ./app.py /var/www/app.py
ADD./requirements.txt /var/www/requirements.txt
# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
ADD mkdir -p /var/www/new_directory
# Run the app
CMD ["echo", "Hello, Developer"]
我们要复制 app.py 和 requirements.txt 文件。 现在,如果我们构建这个 Docker 镜像,并用它来创建一个 Docker 容器,那么我们一定能够在 Docker 容器的文件系统中找到这两个文件。
下面的示例将基于上面的 Dockerfile 构建一个 Docker 镜像。
~/my-app$ Docker build -t new-image .
有了镜像后,我们将使用 docker run
命令创建一个 Docker 容器。 除此之外,我们还将在容器内启动 bash。
代码:
~/my-app$ Docker run -it new-image bash
如果我们列出 /var/www 目录中的文件,您应该会同时看到 app.py 和 requirements.txt 文件。
代码:
root@841d1e8d8c25:/var/www# ls
app.py new_directory requirements.txt
在上面的 Docker 容器中,ADD
和 COPY
命令都允许我们将文件从主机目录复制到 Docker 目录。
但是,在将文件复制到 Docker 容器中时,我们建议使用 COPY
命令。
根据 docker-file 最佳实践,当我们不需要比复制本地文件更多的功能时,
COPY
命令更合适。
另一方面,ADD 命令具有更多功能。 例如,您可以使用此命令将本地 tar 文件提取到 Docker 映像中。
除此之外,ADD 命令还支持远程 URL,而这两个操作是 COPY 命令无法实现的。 如果您试图减小正在构建的 Docker 映像的大小,则前一个命令可能并不理想。
这是因为 ADD 命令可能会显着增加 Docker 映像的大小,尤其是当它从远程 URL 获取包时。
使用 ADD 命令仅从主机复制文件可能会导致文件被意外复制到 Docker 映像的文件系统中。
总之,虽然这两个命令有相似之处并且可以互换使用,但您应该坚持使用 COPY
命令。 另一方面,ADD
命令只能在需要时使用,并且要格外小心。
相关文章
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