CodeBlanks Logo Icon CodeBlanks

What is a Dockerfile?

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 image
  • WORKDIR: working directory
  • COPY: copying files
  • RUN: running a command during the build
  • ENV: environment variable
  • EXPOSE: 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 80

Practice 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

terminal
# buraya yaz

Sample solution

terminal
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/
EXPOSE 80

Expected 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.

Question
____ 80
Answer
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

All lessons in Beginner