What is a Dockerfile?
- Docker & Kubernetes course
- Beginner
- Lesson 19/82
- 4 min
Learn that a Dockerfile is a recipe for an image.
What is a Dockerfile?
A Dockerfile is a plain text file describing step by step how an image is built. Each line is an instruction (written in UPPERCASE) and its arguments. Docker runs the instructions from top to bottom:
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]This file is written once, put under version control, and everyone produces the same image with docker build.
Overview of instructions
FROM: base imageWORKDIR: working directoryCOPY: copying filesRUN: running a command during the buildENV: environment variableEXPOSE: the port the application listens on (documentation)CMD: the default command run when the container starts
# comments start with #
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/
EXPOSE 80Practice The first Dockerfile
Write a Dockerfile for a static site: base image nginx:alpine; copy index.html into /usr/share/nginx/html/; document port 80.
Starter code
# buraya yazSample solution
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/
EXPOSE 80Expected output
[+] Building 4.2s (8/8) FINISHED => naming to docker.io/library/site:latest done
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What must the first instruction of a Dockerfile be (apart from comments and ARG)?
- FROM Correct answer
- RUN
- CMD
- COPY
Explanation: Every image is built on top of a base image.
2 Write the instruction documenting the port the application listens on.
____ 80
EXPOSE 80
Explanation: EXPOSE doesn't open the port; it only documents it. docker run -p is needed to publish.
3 Match the instructions with their jobs.
| Term | Meaning |
|---|---|
FROM |
Base image |
COPY |
Copies files |
RUN |
Command during build |
CMD |
Start command |