Good Code
The good version uses an absolute WORKDIR, copies known files, and keeps installation inputs visible.
Lesson 05
Use WORKDIR and COPY intentionally so filesystem paths are clear and remote side effects are not hidden in ADD.
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"]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 . /The good version uses an absolute WORKDIR, copies known files, and keeps installation inputs visible.
The bad version relies on cd inside individual layers, fetches remote content through ADD, and copies the repository into the image root.