Dockerfile Instructions (ADD, COPY, RUN, CMD, ENTRYPOINT, etc.)

What are Dockerfile Instructions (ADD, COPY, RUN, CMD, ENTRYPOINT, etc.)?

Dockerfile instructions are commands used to assemble a Docker image. ADD and COPY add files to the image, RUN executes commands, CMD provides defaults for executing a container, and ENTRYPOINT configures the container to run as an executable. Understanding these instructions is crucial for creating effective Dockerfiles.

In the world of software engineering, Dockerfile instructions play an integral role in the process of containerization and orchestration. These instructions, which include ADD, COPY, RUN, CMD, ENTRYPOINT, among others, are the building blocks that define the behavior of a Docker container. They are written in a Dockerfile, a text file that contains all the commands needed to assemble an image that can be used to create a Docker container.

Understanding Dockerfile instructions is key to creating efficient, reliable, and secure Docker images. Each instruction adds a new layer to the image, and understanding how these layers interact can help you optimize your Dockerfiles for faster build times and smaller image sizes. This article provides a deep dive into Dockerfile instructions, their history, use cases, and specific examples.

Definition of Dockerfile Instructions

Dockerfile instructions are commands that are written in a Dockerfile to define how a Docker image should be built. Each instruction creates a new layer in the Docker image, and these layers are stacked on top of each other to create the final image. The instructions are processed in the order they appear in the Dockerfile, from top to bottom.

There are several different Dockerfile instructions, each with its own specific function. Some of the most commonly used instructions include ADD, COPY, RUN, CMD, and ENTRYPOINT. Each of these instructions plays a different role in the construction of a Docker image, and understanding how to use them effectively is key to creating efficient and reliable Docker images.

ADD Instruction

The ADD instruction copies new files, directories, or remote file URLs from  and adds them to the filesystem of the image at the path . This command optionally can unpack compressed files, but it is generally recommended to use COPY, as it is more transparent.

ADD has two forms: ADD ... and ADD ["",... ""] (this form is required for paths containing whitespace). The  is an absolute path, or a path relative to WORKDIR, that is being added to the Docker image.

COPY Instruction

The COPY instruction copies new files or directories from  and adds them to the filesystem of the container at the path . Unlike ADD, COPY does not have the ability to handle URLs or to unpack compressed files.

COPY also has two forms: COPY ... and COPY ["",... ""]. The COPY instruction is often preferred to ADD, as it is more transparent and does not include the additional features of ADD, which can lead to unexpected results.

Explanation of Dockerfile Instructions

Each Dockerfile instruction serves a specific purpose in the construction of a Docker image. The instructions are processed in the order they appear in the Dockerfile, and each instruction adds a new layer to the Docker image. This layered approach allows Docker to cache the results of each instruction, which can significantly speed up the build process for Docker images.

However, because each instruction adds a new layer to the Docker image, it's important to understand how these layers interact. For example, if you use the RUN instruction to install a software package in one layer, and then use another RUN instruction in a later layer to remove that package, the package will still be present in the earlier layer and will take up space in the final image. Understanding these interactions can help you write more efficient Dockerfiles.

RUN Instruction

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile. The RUN instruction is one of the most powerful Dockerfile instructions, as it allows you to install software, set up environment variables, and perform other setup tasks.

RUN has two forms: RUN  (shell form) and RUN ["executable", "param1", "param2"] (exec form). The shell form causes the command to be run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows. The exec form does not use a shell, which means that you can't use shell processing features like variable substitution and wildcard expansion.

CMD Instruction

The CMD instruction provides defaults for an executing container. These can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction. The CMD instruction can be used to provide default arguments to the ENTRYPOINT instruction or to execute a specific command when the container is run.

CMD has three forms: CMD ["executable","param1","param2"] (exec form, this is the preferred form), CMD ["param1","param2"] (as default parameters to ENTRYPOINT), and CMD command param1 param2 (shell form).

ENTRYPOINT Instruction

The ENTRYPOINT instruction allows you to configure a container that will run as an executable. This instruction defines the executable that will be run when a container is started from the image. If the CMD instruction is used without ENTRYPOINT, CMD specifies the command that is executed when the container is run. However, if ENTRYPOINT is specified, the CMD instruction provides additional default arguments that can be overridden by the command line arguments when docker run is used.

ENTRYPOINT has two forms: ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred) and ENTRYPOINT command param1 param2 (shell form).

History of Dockerfile Instructions

The history of Dockerfile instructions is closely tied to the history of Docker itself. Docker was first released in 2013 as an open-source project by a company called dotCloud, which later became Docker, Inc. The initial release of Docker included a basic set of Dockerfile instructions, including ADD, COPY, RUN, CMD, and ENTRYPOINT.

Over time, the Dockerfile instruction set has been expanded and refined to provide more functionality and flexibility. New instructions have been added, and existing instructions have been updated to provide more options and better performance. The Dockerfile instruction set continues to evolve as the Docker community identifies new use cases and requirements for building Docker images.

Evolution of Dockerfile Instructions

The evolution of Dockerfile instructions has been driven by the needs of the Docker community. As Docker has grown in popularity, the community has identified new use cases and requirements for building Docker images, and the Dockerfile instruction set has been updated to meet these needs.

For example, the ADD instruction was originally designed to copy files from the build context into the Docker image. However, the Docker community found that this functionality was often used to copy files from URLs or to unpack compressed files, which led to the introduction of the COPY instruction. The COPY instruction provides a more transparent way to copy files into a Docker image, without the additional features of the ADD instruction that can lead to unexpected results.

Current State of Dockerfile Instructions

The current state of Dockerfile instructions reflects the maturity of the Docker platform. The instruction set is comprehensive and flexible, allowing developers to build a wide range of Docker images. However, the Docker community continues to identify new use cases and requirements, and the Dockerfile instruction set continues to evolve to meet these needs.

For example, recent additions to the Dockerfile instruction set include the HEALTHCHECK instruction, which allows you to specify a command to check the health of your application, and the ARG instruction, which allows you to define build-time variables. These new instructions provide additional flexibility and functionality for building Docker images, and demonstrate the ongoing evolution of the Dockerfile instruction set.

Use Cases of Dockerfile Instructions

Dockerfile instructions are used in a wide range of use cases, from building simple, single-process containers to complex, multi-service applications. The flexibility and power of the Dockerfile instruction set allows developers to build Docker images that meet their specific needs.

Some common use cases for Dockerfile instructions include installing software, setting up environment variables, copying files into the Docker image, and defining the command that is run when the container is started. However, the Dockerfile instruction set is flexible enough to support a wide range of other use cases as well.

Installing Software

The RUN instruction is commonly used to install software in a Docker image. This can include system packages, software libraries, or custom software. The RUN instruction allows you to execute any command in a new layer on top of the current image, which makes it a powerful tool for installing software.

For example, you might use the RUN instruction to install a web server, a database server, or a programming language runtime in your Docker image. The installed software becomes part of the Docker image, and is available to any containers that are created from the image.

Setting Up Environment Variables

The ENV instruction is used to set environment variables in a Docker image. These variables can be used to configure the behavior of the software that is installed in the image. The ENV instruction adds a new layer to the Docker image, and the environment variables that are set in this layer are available to any containers that are created from the image.

For example, you might use the ENV instruction to set the PATH environment variable, which determines the directories that are searched for executable programs. Or you might use the ENV instruction to set configuration variables for a web server or a database server.

Copying Files

The ADD and COPY instructions are used to copy files from the build context into the Docker image. These files can include configuration files, application code, or any other files that are needed by the software that is installed in the image.

For example, you might use the ADD or COPY instruction to copy a web application's code into the Docker image. Or you might use these instructions to copy configuration files for a web server or a database server into the image.

Defining the Startup Command

The CMD and ENTRYPOINT instructions are used to define the command that is run when a container is started from the Docker image. This command can start a service, run a script, or perform any other action that is needed to start your application.

For example, you might use the CMD or ENTRYPOINT instruction to start a web server, a database server, or a background job. Or you might use these instructions to run a script that initializes your application.

Examples of Dockerfile Instructions

Let's look at some specific examples of Dockerfile instructions to see how they can be used in practice. These examples will demonstrate how to use the ADD, COPY, RUN, CMD, and ENTRYPOINT instructions to build a simple web application Docker image.

Please note that these examples are simplified for illustrative purposes, and may not represent best practices for building Docker images in a production environment.

Example: Installing Software with RUN

Here's an example of how to use the RUN instruction to install software in a Docker image. In this example, we're installing the Apache web server:


# Use an existing Docker image as the base
FROM ubuntu:18.04

# Install Apache
RUN apt-get update && apt-get install -y apache2

In this Dockerfile, the RUN instruction is used to execute the apt-get update and apt-get install commands, which update the package list and install the Apache web server. The -y option is used to automatically answer yes to any prompts that are displayed by the apt-get install command.

Example: Copying Files with ADD and COPY

Here's an example of how to use the ADD and COPY instructions to copy files into a Docker image. In this example, we're copying a web application's code and configuration files into the image:


# Use an existing Docker image as the base
FROM ubuntu:18.04

# Install Apache
RUN apt-get update && apt-get install -y apache2

# Copy the web application's code into the image
ADD app /var/www/html/app

# Copy the Apache configuration file into the image
COPY apache2.conf /etc/apache2/apache2.conf

In this Dockerfile, the ADD instruction is used to copy the app directory from the build context into the /var/www/html/app directory in the Docker image. The COPY instruction is used to copy the apache2.conf file from the build context into the /etc/apache2/apache2.conf file in the Docker image.

Example: Defining the Startup Command with CMD and ENTRYPOINT

Here's an example of how to use the CMD and ENTRYPOINT instructions to define the command that is run when a container is started from the Docker image. In this example, we're starting the Apache web server:


# Use an existing Docker image as the base
FROM ubuntu:18.04

# Install Apache
RUN apt-get update && apt-get install -y apache2

# Copy the web application's code into the image
ADD app /var/www/html/app

# Copy the Apache configuration file into the image
COPY apache2.conf /etc/apache2/apache2.conf

# Define the command to start the Apache web server
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

In this Dockerfile, the CMD instruction is used to define the command that is run when a container is started from the Docker image. The command /usr/sbin/apache2ctl -D FOREGROUND starts the Apache web server in the foreground, which allows Docker to manage the web server process.

Conclusion

Dockerfile instructions are the building blocks that define the behavior of a Docker container. Understanding these instructions is key to creating efficient, reliable, and secure Docker images. By mastering Dockerfile instructions, you can take full advantage of the power and flexibility of Docker, and create Docker images that meet your specific needs.

Whether you're building simple, single-process containers or complex, multi-service applications, Dockerfile instructions provide the tools you need to build Docker images that are optimized for your use case. With Dockerfile instructions, you can install software, set up environment variables, copy files into the Docker image, and define the command that is run when the container is started, among other tasks.

High-impact engineers ship 2x faster with Graph
Ready to join the revolution?
High-impact engineers ship 2x faster with Graph
Ready to join the revolution?

Code happier

Join the waitlist