Dockerfile: The Recipe for Your Container

A Dockerfile is a recipe for building a container image. It's a text file of commands that automates an application's environment setup, ensuring it runs identically anywhere. The footgun is creating bloated images with unnecessary build tools.
WHY IT EXISTS Dockerfiles exist to codify the process of creating a consistent, portable application environment. Before containers, setting up an application on a new server was often a manual, error-prone checklist. A Dockerfile automates this process, ensuring that the environment is perfectly replicated every time, solving the classic "it works on my machine" problem.
THE MENTAL MODEL Think of a Dockerfile as a recipe for baking a cake. The recipe lists all the ingredients and the exact steps to follow. The 'FROM' instruction is your base ingredient, like a pre-made cake mix. 'COPY' adds your own ingredients, like your application code. 'RUN' instructions are the mixing and baking steps, like installing dependencies. Finally, 'CMD' is how you serve the cake—the command to start your application. Anyone with this recipe can produce the exact same cake.
HOW IT WORKS A Dockerfile is a text file named 'Dockerfile' with no extension. Docker reads this file and executes its commands in order to build a container image. Each command creates a new layer in the image. These layers are cached, so if you only change a later step, Docker can reuse the cached layers from previous steps, making builds much faster. Key instructions include: FROM to specify a base image; COPY to add files from your host machine; RUN to execute shell commands; EXPOSE to document which network ports the container will listen on; and CMD to provide the default command to run when the container starts.
WHEN TO USE IT Use a Dockerfile for almost any application you want to containerize. It's the foundation of modern software development for creating consistent local development environments, building artifacts in CI/CD pipelines, and deploying microservices or monolithic applications. It makes your application portable across different cloud providers and on-premises servers.
WHEN NOT TO USE IT A Dockerfile is for building a container image, not for managing live infrastructure or configuring a host machine. For orchestrating multiple containers, you'd use a tool like Docker Compose or Kubernetes. For configuring the underlying server itself, you would use an infrastructure-as-code tool like Ansible or Terraform.
ONE CANONICAL EXAMPLE A simple Dockerfile for a Node.js web application might look like this: FROM node:18-alpine This starts from a lightweight base image with Node.js pre-installed. WORKDIR /app This sets the working directory inside the container. COPY package*.json ./ This copies the dependency manifests. RUN npm install This installs the application's dependencies. COPY . . This copies the rest of the application code. EXPOSE 3000 This informs Docker the container listens on port 3000. CMD ["node", "server.js"] This specifies the command to run the app.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.