C.W.K.
Stream
Lesson 01 of 06 · published

Dockerfile Basics — FROM, WORKDIR, COPY, RUN, CMD

~16 min · dockerfile, basics

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Six instructions cover most Dockerfiles

  • FROM — the base image. Every Dockerfile starts with one.
  • WORKDIR — set the working directory (creates if absent). Use this instead of cd.
  • COPY — copy files from build context to image.
  • RUN — execute a command at build time (creates a new layer).
  • EXPOSE — document which port the app listens on. Pure metadata.
  • CMD — default command when the container starts.

A complete real Dockerfile

Below is a working FastAPI Dockerfile. It is intentionally simple — no multi-stage yet, no non-root user yet — just the six core instructions in their natural order. Track 2 expands on each.

Code

Minimal FastAPI Dockerfile·dockerfile
FROM python:3.12-slim

WORKDIR /app

# Copy dependency manifest first (cache-friendly)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the app
COPY . .

EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Build and run·bash
# Build (the . is the build context)
docker build -t myapi:1.0 .

# Run
docker run -d -p 8000:8000 --name api myapi:1.0

# Verify
curl http://localhost:8000/docs

External links

Exercise

Take any small project of yours (Python, Node, or Go). Write a 6-line Dockerfile using only FROM, WORKDIR, COPY, RUN, EXPOSE, CMD. Build it and run it. Capture the Dockerfile and the working curl or equivalent against the running container.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.