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.
Interview question
What is the main purpose of using a Dockerfile in application development?
- a.To monitor the resource usage and performance metrics of a running container.
- b.To define the runtime configuration for a cluster of interconnected containers.
- c.To manage the underlying server infrastructure on which Docker containers are deployed.
- d.To automate the creation of a consistent and portable environment for an application.Correct
Why? this is the answer
The card explains that Dockerfiles exist to codify the process of creating a consistent, portable application environment, solving the 'it works on my machine' problem. Option B describes the function of orchestration tools like Docker Compose or Kubernetes, not the Dockerfile itself.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.
See open roles