Top 10 Common DevOps/SRE Interview Questions and Answers on Dockerfiles

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 serves as a hint for users running the container.

  • To make the ports accessible, use the -p flag with docker run.

Example:

EXPOSE 8080

4. What is the Difference Between ARG and ENV?

  • ARG: Defines a variable that users can pass at build time using docker build --build-arg. It is not available at runtime.

Example:

ARG VERSION=1.0
RUN echo $VERSION
ENV NODE_ENV=production
  • ENV: Sets environment variables that are available both during build time and at runtime.

Example:

ENV NODE_ENV=production

5. How Do You Optimize Docker Images for Size?

  • Use multi-stage builds to separate build dependencies from runtime dependencies.

Example:

# Build stage
FROM node:14 AS build
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .

# Production stage
FROM node:14-alpine
WORKDIR /app
COPY --from=build /app .
CMD ["node", "app.js"]
  • Combine commands in RUN statements to reduce the number of layers.

Example:

RUN apt-get update && apt-get install -y curl git
  • Use smaller base images, like Alpine, to minimize the image size.

6. How Can You Persist Data Across Container Restarts?

  • Use Docker volumes or bind mounts to persist data outside of the container’s filesystem. This ensures data remains available even when the container is restarted or recreated.

Example:

docker run -v mydata:/data myapp

7. What is the Purpose of the LABEL Instruction?

  • LABEL: Adds metadata to the image, such as maintainer information, version, or description. This helps with the documentation and management of images.

Example:

LABEL maintainer="[email protected]" version="1.0" description="A sample app"

8. Why Might You Use HEALTHCHECK in a Dockerfile?

  • HEALTHCHECK: Defines a command to test whether the container is functioning correctly. If the health check fails, Docker can automatically restart the container or take other corrective actions.

Example:

HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost/ || exit 1

9. How Can You Handle Secrets in Dockerfiles?

  • Avoid hardcoding secrets in Dockerfiles. Instead, use Docker secrets, environment variables, or external tools like HashiCorp Vault to manage sensitive information securely.

Example:

ENV API_KEY=${API_KEY}

10. What is the Difference Between CMD and ENTRYPOINT?

  • CMD: Provides defaults for an executing container. It can be overridden by passing arguments to docker run.

Example:

CMD ["nginx", "-g", "daemon off;"]

ENTRYPOINT: Configures a container to run as an executable. Commands and arguments provided with docker run are appended to the ENTRYPOINT instruction.

Example:

ENTRYPOINT ["nginx"]
CMD ["-g", "daemon off;"]

Thank you for reading!🙏 If you enjoyed this article and want to stay updated with more content like this, follow me on my social media channels:

Feel free to connect, and let’s continue the conversation!😊

🚀 Struggling with Kubernetes Concepts? We’ve Got You Covered!

This course simplifies everything with: ✅ Real-world examples to connect theory with practice. 🛠️ Hands-on labs to build confidence through action. 📚 Clear explanations that make even complex topics easy to understand.

👉 You won’t find a better way to master Kubernetes! Enroll Now https://cloudops0.gumroad.com/l/k8s

and take the first step toward becoming a Kubernetes pro! 🌟

DockerKubernetesContainersMicroservicesCicd

Last updated