Skip to the workspace
Pebellslearn by building
Basics of Docker  ›  Capstone Project

Your First Dockerfile

Basics of Docker 16 minA Dockerfile lists the steps to build an image: start FROM a base, set a WORKDIR, COPY files in, RUN setup commands, and name the start command with CMD.

docker / Your First Dockerfile

Explore each scene at your own pace.

01 · The rule

The rule

Your First Dockerfile

Everything so far has used an image called quicknotes:1.0. Time to make it. The quicknotes folder holds app.py, which serves the notes page on port 5000, and requirements.txt, which lists its libraries. One more small text file, the Dockerfile, turns that folder into an image anyone can run.

The rule

FROM a base, COPY your code, RUN the setup, CMD the start.

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]

Line by line:

  • FROM picks the starting image: here a slim Debian with Python 3.12 already installed. Every Dockerfile begins with it.
  • WORKDIR /app creates /app if needed and makes it the current directory for everything after.
  • COPY requirements.txt . copies a file from your folder (the build context) into /app.
  • RUN executes a command during the build and saves the result as a layer: here, installing the libraries.
  • COPY . . copies the rest of the code.
  • EXPOSE 5000 documents which port the app listens on. It does not publish anything; -p still does that.
  • CMD sets the default command that runs when a container starts. The bracketed "exec form" runs Python directly, without a shell in between.

One detail about app.py: inside a container the app must listen on all interfaces (0.0.0.0), not just 127.0.0.1, or published ports cannot reach it.

Build and run it from the quicknotes folder:

docker build -t quicknotes:1.0 .
docker run -d --name notes -p 8080:5000 quicknotes:1.0

The final . is the build context: the folder whose files COPY can see. A .dockerignore file keeps things like .git or local virtual environments out of it.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.