What’s a Dockerfile?

Understand common Dockerfile instructions and how to combine them to write a Dockerfile.

There are two ways to create Docker images:

  1. We can create a Docker image by automatically building the image following the instructions in a text file called a Dockerfile.
  2. We can create a Docker image by modifying a running container content and committing the changes. It’s like taking a snapshot of a running container.

Making changes to a running container and committing the change to create a new image works well for some use cases. However, it’s more practical and effortlessly reproducible to use a Dockerfile.

Before we get started with a Dockerfile and creating a Docker image using the docker build command, let’s first define a Dockerfile.

Dockerfile

A Dockerfile is a text file that contains step-by-step instructions for Docker to build an image. When we run a docker build command while specifying a Dockerfile, Docker creates an image based on the instructions in the Dockerfile.

Just as a Docker image is immutable, so are the images created from Dockerfiles. The same Dockerfile will always produce the same Docker image as long as the instructions in the Dokerfile and the container dependencies don’t change.

Common Dockerfile commands

A Dockerfile must, at the very least, include the FROM, COPY, and CMD. commands. Commands are the building blocks of a Dockerfile. ...