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 /app

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
RUN cd app && pip install flask

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.