Dockerfile & Build System
Difficulty: medium
Overview
A Dockerfile is a text script of instructions that Docker executes sequentially to build an image.
Core Instructions:
| Instruction | Creates Layer? | Purpose |
|---|---|---|
| FROM | Yes | Base image — must be first instruction |
| RUN | Yes | Execute shell commands during build |
| COPY | Yes | Copy files from build context |
| ADD | Yes | Like COPY + URL download + tar extraction |
| CMD | No | Default command (overridable at docker run) |
| ENTRYPOINT | No | Fixed executable; CMD becomes its args |
| ENV | No* | Persistent environment variable |
| ARG | No | Build-time variable; not in final image |
| EXPOSE | No | Document listening port (does not publish) |
| VOLUME | Yes | Declare anonymous mount point |
| WORKDIR | No* | Set working directory |
| USER | No* | Switch to non-root user |
| LABEL | No* | Add metadata key-value pairs |
| HEALTHCHECK | No | Define container health check |
CMD vs ENTRYPOINT:
CMDsets defaults — easily overridden:docker run image custom-cmd.ENTRYPOINTfixes 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
Q1. What is the purpose of the FROM instruction in a Dockerfile?
Select one answer before revealing.
Q2. What does the RUN instruction do in a Dockerfile?
Select one answer before revealing.
Q3. What is the difference between the COPY and ADD instructions in a Dockerfile?
Select one answer before revealing.
Q4. What is the difference between CMD and ENTRYPOINT in a Dockerfile?
Select one answer before revealing.
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.
Q6. What is the purpose of the ENV instruction in a Dockerfile?
Select one answer before revealing.
Q7. What is the key difference between ARG and ENV in a Dockerfile?
Select one answer before revealing.
Q8. What is the VOLUME instruction used for in a Dockerfile?
Select one answer before revealing.
Q9. Why should containers avoid running as the root user, and which Dockerfile instruction sets the user?
Select one answer before revealing.
Q10. What is a multi-stage build in Docker and what problem does it solve?
Select one answer before revealing.
Q11. What is the difference between the exec form and shell form of CMD and ENTRYPOINT?
Select one answer before revealing.
Q12. What is the purpose of a .dockerignore file?
Select one answer before revealing.
Q13. What does the HEALTHCHECK instruction do in a Dockerfile?
Select one answer before revealing.
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.
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.
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.
Q17. Which practices reduce Docker image size? (Select all that apply — more than one answer may be correct.)
Select one answer before revealing.
Q18. What is the LABEL instruction used for in a Dockerfile?
Select one answer before revealing.
Q19. What is the ONBUILD instruction used for?
Select one answer before revealing.