/Dockerfile & Build System
Concept Detail

Dockerfile & Build System

Difficulty: medium

Overview


A Dockerfile is a text script of instructions that Docker executes sequentially to build an image.

Core Instructions:

InstructionCreates Layer?Purpose
FROMYesBase image — must be first instruction
RUNYesExecute shell commands during build
COPYYesCopy files from build context
ADDYesLike COPY + URL download + tar extraction
CMDNoDefault command (overridable at docker run)
ENTRYPOINTNoFixed executable; CMD becomes its args
ENVNo*Persistent environment variable
ARGNoBuild-time variable; not in final image
EXPOSENoDocument listening port (does not publish)
VOLUMEYesDeclare anonymous mount point
WORKDIRNo*Set working directory
USERNo*Switch to non-root user
LABELNo*Add metadata key-value pairs
HEALTHCHECKNoDefine container health check

CMD vs ENTRYPOINT:

  • CMD sets defaults — easily overridden: docker run image custom-cmd.
  • ENTRYPOINT fixes the executable — docker run args become parameters to it.
  • Together: ENTRYPOINT ["nginx"] + CMD ["-g","daemon off;"] is the idiomatic pattern.

Exec form vs Shell form:

  • Exec: ["executable","arg"] — no shell, signals reach the process, PID 1 is your app. Preferred.
  • Shell: command arg — wrapped in /bin/sh -c, PID 1 is the shell, SIGTERM not forwarded.

Build Cache:
Docker caches each layer. Cache is reused if the instruction text and all preceding layers are unchanged. For COPY/ADD, the file contents are also compared. Order instructions from least-changing to most-changing for maximum cache efficiency.

Multi-stage Builds:

FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

The final image contains only nginx + dist — no node_modules, build tools, or source code.

Practice Linked Questions


easy

Q1. What is the purpose of the FROM instruction in a Dockerfile?


Select one answer before revealing.

easy

Q2. What does the RUN instruction do in a Dockerfile?


Select one answer before revealing.

medium

Q3. What is the difference between the COPY and ADD instructions in a Dockerfile?


Select one answer before revealing.

medium

Q4. What is the difference between CMD and ENTRYPOINT in a Dockerfile?


Select one answer before revealing.

hard

Q5. A Dockerfile has these instructions in order: FROM, RUN apt-get install, COPY package.json, RUN npm install, COPY . . — You change only the application source code. Which layers will Docker rebuild from cache?


Select one answer before revealing.

medium

Q6. What is the purpose of the ENV instruction in a Dockerfile?


Select one answer before revealing.

medium

Q7. What is the key difference between ARG and ENV in a Dockerfile?


Select one answer before revealing.

medium

Q8. What is the VOLUME instruction used for in a Dockerfile?


Select one answer before revealing.

medium

Q9. Why should containers avoid running as the root user, and which Dockerfile instruction sets the user?


Select one answer before revealing.

hard

Q10. What is a multi-stage build in Docker and what problem does it solve?


Select one answer before revealing.

hard

Q11. What is the difference between the exec form and shell form of CMD and ENTRYPOINT?


Select one answer before revealing.

medium

Q12. What is the purpose of a .dockerignore file?


Select one answer before revealing.

hard

Q13. What does the HEALTHCHECK instruction do in a Dockerfile?


Select one answer before revealing.

hard

Q14. You have `ENTRYPOINT ["python", "app.py"]` and `CMD ["--port", "8080"]` in a Dockerfile. What happens when you run `docker run myimage --port 9090`?


Select one answer before revealing.

medium

Q15. Which Dockerfile instructions create a new image layer? (Select all that apply — more than one answer may be correct.)


Select one answer before revealing.

medium

Q16. Which of the following are valid forms of the CMD instruction? (Select all that apply — more than one answer may be correct.)


Select one answer before revealing.

hard

Q17. Which practices reduce Docker image size? (Select all that apply — more than one answer may be correct.)


Select one answer before revealing.

easy

Q18. What is the LABEL instruction used for in a Dockerfile?


Select one answer before revealing.

hard

Q19. What is the ONBUILD instruction used for?


Select one answer before revealing.