Member-only story
Top 10 Common DevOps/SRE Interview Questions and Answers on Dockerfiles
3 min readSep 6, 2024
1. What is the Difference Between RUN
and CMD
?
RUN
: Executes commands during the image build process, creating a new layer. Typically used for installing software packages.
Example:
RUN apt-get update && apt-get install -y curl
CMD
: Specifies the default command to run when the container starts. It executes at runtime, not during the build process.
Example:
CMD ["node", "app.js"]
2. How to Use Multi-Stage Builds in Dockerfiles?
- Multi-stage builds allow you to use multiple
FROM
statements in your Dockerfile to create temporary stages that help keep the final image smaller.
Example:
# Build stage
FROM golang:1.17 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Production stage
FROM alpine:latest
COPY --from=builder /app/myapp /myapp
CMD ["/myapp"]
3. What is the Purpose of the EXPOSE
Instruction?
EXPOSE
: Documents the ports on which the container listens at runtime. It does not publish the ports but…