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
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