Difference between COPY and ADD commands in Dockerfile
A Dockerfile is a text document that contains all the commands used to build a Docker image. Recently, we have seen Docker being widely used as the default tool for managing configurations and automating deployments.
Advanced features such as automated builds using Dockerfiles are reasons developers are adopting Docker. Others include isolating applications from the environment they run in and quickly ensuring security.
When creating a Docker container, you often need to transfer files from the host machine to the Docker image. These files can include libraries or property files that your application needs to run.
Difference between COPY and ADD commands in Dockerfile
In a Dockerfile, we can copy these files using the COPY or ADD commands. These commands are functionally identical; however, there are some differences.
The COPY and ADD commands follow the following syntax.
COPY <src> <dest>
ADD <src> <dest>
Both instructions copy <src>
files or directories located in the local host and add them to a location in the container's file system <dest>
. For example, in the following Dockerfile, we copy files from the current directory to the /var/www directory in the Docker image.
# 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"]
We want to copy the app.py and requirements.txt files. Now, if we build this Docker image and use it to create a Docker container, we will definitely be able to find these two files in the file system of the Docker container.
The following example will build a Docker image based on the above Dockerfile.
~/my-app$ Docker build -t new-image .
Now that we have the image, we will docker run
create a Docker container using the command. In addition to this, we will also start bash inside the container.
Code:
~/my-app$ Docker run -it new-image bash
If we list the files in the /var/www directory, we should see both app.py and requirements.txt files.
Code:
root@841d1e8d8c25:/var/www# ls
app.py new_directory requirements.txt
In the Docker container above, both the ADD
and COPY
commands allow us to copy files from a host directory to a Docker directory.
However, when copying files into a Docker container, we recommend using COPY
the command.
COPY
According to docker-file best practices, commands are more suitable when we don't need more functionality than copying local files .
On the other hand, the ADD command has more features. For example, you can use this command to extract a local tar file into a Docker image.
In addition to this, the ADD command supports remote URLs, two operations that are not possible with the COPY command. The previous command may not be ideal if you are trying to reduce the size of the Docker image you are building.
This is because the ADD command can significantly increase the size of a Docker image, especially when it fetches packages from a remote URL.
Using the ADD command to simply copy files from the host machine can result in files being accidentally copied into the Docker image's file system.
In conclusion, while the two commands have similarities and can be used interchangeably, you should stick with COPY
the command. ADD
The command, on the other hand, should be used only when required and with extreme caution.
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
The --rm flag in Docker
Publish Date:2025/03/24 Views:151 Category:Docker
-
Typically, when most developers start using docker, after going through all the processes of pulling images, building them, and running containers, removing a container defeats the purpose of doing so. However, for experienced developers, t
Setting environment variables in Docker
Publish Date:2025/03/24 Views:188 Category:Docker
-
Environment variables are used to add additional configuration or metadata to aid in the development of an application, and can exist in different forms. For example, when developing a Java application, we usually set an environment variabl
在 Linux 中托管 Docker Internal
Publish Date:2023/04/18 Views:221 Category:Docker
-
Docker 允许开发人员通过将应用程序包装在称为容器的标准化单元中来高效地构建、测试和部署应用程序。 在使用 Docker 容器时,您可能会遇到需要将容器与主机连接的场景。
在 Docker 中设置工作目录
Publish Date:2023/04/18 Views:234 Category:Docker
-
在 Docker 中,我们可以通过编辑 Dockerfile 并添加密钥 WORKDIR 来设置我们的工作目录。本文将讨论在 Docker 中更改我们当前和默认的工作目录。
在 Docker 容器中公开多个端口
Publish Date:2023/04/18 Views:466 Category:Docker
-
Docker 容器使用端口来实现万维网上不同设备之间的通信。 在本篇文章中,我们将学习如何使用 Nginx 应用程序在 Docker 容器中公开多个端口。
将用户添加到 Docker 容器
Publish Date:2023/04/18 Views:206 Category:Docker
-
在本文中,我们将学习如何通过实现返回产品数组的 Express 应用程序将用户添加到 Docker 容器。
使用 Docker 网络主机命令
Publish Date:2023/04/18 Views:143 Category:Docker
-
在本文中,我们将学习如何使用 --network 命令将容器添加到主机网络。 如果我们不使用此命令指定网络,我们还将了解如何将容器添加到默认网络。
清除 Docker 容器日志
Publish Date:2023/04/18 Views:363 Category:Docker
-
本文介绍了我们可以用来清除 docker 容器中日志的不同方法。日志是应用程序在特定事件或状态发生时记录的信息,它们帮助我们监控应用程序并采取必要的措施。
Docker 中的守护进程日志位置
Publish Date:2023/04/18 Views:319 Category:Docker
-
本文将讨论守护进程事件以及我们通常可以在哪里找到每个操作系统 (OS) 的守护进程日志。