Docker

Lesson 05

COPY, ADD, and WORKDIR

Use WORKDIR and COPY intentionally so filesystem paths are clear and remote side effects are not hidden in ADD.

Good Code

Dockerfile
FROM python:3.12-slim

# WORKDIR gives every following path one clear base directory.
WORKDIR /app

# Copy known inputs before installing dependencies.
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY app ./app
CMD ["python", "-m", "app"]

Bad Code

Dockerfile
FROM python:latest

RUN mkdir app
# cd inside RUN does not set the working directory for later layers.
RUN cd app && pip install flask

# ADD can download or extract content, hiding extra behavior in one line.
ADD https://example.com/app.tar.gz /app
COPY . /

Review Notes

What to review

Good Code

The good version uses an absolute WORKDIR, copies known files, and keeps installation inputs visible.

Bad Code

The bad version relies on cd inside individual layers, fetches remote content through ADD, and copies the repository into the image root.

Takeaways

  • Prefer explicit paths and explicit download or extraction steps over surprising Dockerfile side effects.