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:

  • CMD: Specifies the default command to run when the container starts. It executes at runtime, not during the build process.

Example:

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:

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:

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:

  • ENV: Sets environment variables that are available both during build time and at runtime.

Example:

5. How Do You Optimize Docker Images for Size?

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

Example:

  • Combine commands in RUN statements to reduce the number of layers.

Example:

  • 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:

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:

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:

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:

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:

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

Example:

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