What is a Dockerfile? What are Dockerfile Commands?

In Devops, Docker
August 14, 2023


A Dockerfile is a configuration file used to create Docker containers. It specifies how the application will run and which dependencies will be installed. Docker uses this Dockerfile to create a container and turns this container into a file called an image. This image can then be run on any system. In this article, we will discuss what Dockerfile is and the commands used in Dockerfile.

What Is Docker Image?

A Docker Image is a read-only file with a bunch of instructions. When these instructions are executed, it creates a Docker container.

What Is Dockerfile?

A Dockerfile is a file that defines the file system. It is made up of configuration commands that are executed line by line, and each command performs an operation such as creating a file or directory, changing the contents of a file or directory, and so on. Dockerfile specifies how the application will run and which dependencies will be installed, ensuring that when creating a Docker container, everything required for the application to function is taken into account. Furthermore, Dockerfile describes the processes running within the container and how they will be managed.

A Dockerfile consists of various configuration commands. For instance, the FROM command specifies the base image that a container will run on. The RUN command specifies the commands to be executed inside the container. The COPY command is used to copy files or directories into the container. The EXPOSE command specifies which ports of the container can be accessed.

How to Create a Dockerfile?

You can follow the steps below to create a simple Dockerfile:

  1. Create a file and name it Dockerfile.
  2. Write the FROM command at the beginning of the file to specify the base image that the container will run on. For example, using the command FROM ubuntu:latest indicates the use of the latest version of Ubuntu.
  3. Use RUN commands to specify the commands that will be executed inside the container. For instance, with the RUN apt-get update command, you can update the apt-get package manager inside the container.
  4. Use COPY commands to copy files or directories into the container. For example, the command COPY index.html /var/www/html can copy the index.html file into the /var/www/html directory within the container.
  5. Use the EXPOSE command to specify which ports of the container can be accessed. For example, using EXPOSE 80 indicates that the container can be accessed through port 80.
  6. Use the CMD command to specify the commands that will be executed when the container is run. For example, when the container is run, we execute the nginx command and use the parameters “-g” and “daemon off;” to configure it.
FROM ubuntu:latest

RUN apt-get update

COPY index.html /var/www/html/


CMD ["nginx", "-g", "daemon off;"]

After completing these steps, the Dockerfile will be created, and a container can be built using the docker build command with this Dockerfile. For instance, a container can be created in the directory where the Dockerfile is located using the command docker build -t my-app . The name of this container will be my-app, and it can be run using the docker run command.

Dockerfile Commands

Dockerfile commands are instructions that are used to create and configure Docker images. Each command performs a specific action in the process of building a Docker image. Here are some commonly used Dockerfile commands along with explanations:

  1. FROM: Explanation: Specifies the base image for the new image. It’s the starting point of your Docker image. Example: FROM ubuntu:latest
  2. RUN: Explanation: Executes a command inside the image during the build process. Used to install software, update packages, etc. Example: RUN apt-get update && apt-get install -y nginx
  3. COPY: Explanation: Copies files or directories from the host into the image’s filesystem. Example: COPY app.jar /app/
  4. ADD: Explanation: Similar to COPY but also supports URLs and extracts tar archives. Example: ADD http://example.com/file.tar.gz /
  5. WORKDIR: Explanation: Sets the working directory for the subsequent commands. Example: WORKDIR /app
  6. EXPOSE: Explanation: Informs Docker that the container listens on a specified network port at runtime. Example: EXPOSE 8080
  7. ENV: Explanation: Sets environment variables for the container. Example: ENV PORT 3000
  8. ARG: Explanation: Defines build-time variables that can be used in the Dockerfile. Example: ARG version=latest
  9. CMD: Explanation: Specifies the default command to run when the container starts. Example: CMD ["npm", "start"]
  10. ENTRYPOINT: Explanation: Sets the primary command for the container. Overrides the CMD command. Example: ENTRYPOINT ["java", "-jar", "app.jar"]
  11. VOLUME: Explanation: Creates a mount point to link the container with the host’s filesystem or other containers. Example: VOLUME /data
  12. USER: Explanation: Sets the user and optionally the user group to use when running the image. Example: USER appuser

These are just some of the commonly used Dockerfile commands. By combining these commands in different ways, you can define the steps needed to create a Docker image that includes your application and its dependencies.