blog
  • Blogs
    • Medium Articles
      • Linux
        • 40 Powerful Linux Networking Commands You Must Know.
        • These (Linux) VI Editor Shortcuts You Must Know
        • Bash/Linux Interview Questions for DevOps Engineers
        • Page 1
      • Git
        • 40 Powerful Git Commands Every Developer Should Know
        • 10 Git Best Practices That Every Developer Must Know
      • DevOps/SRE Interview Questions and Answers
        • Top DevOps/SRE Interview Questions and Answers on AWS VPC
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Terraform Best Practices
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Kubernetes Best Practices
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Dockerfiles
        • Top 10 Common DevOps/SRE Interview Questions and Answers on Grafana
      • Installation
        • Docker Installation on Ubuntu 20/22
        • Install WireGuard VPN on Docker Compose
        • Install Redis on Docker Compose
        • Gravitee Docker Compose
      • Kubernetes Series 2025
        • Understanding Kubernetes: Part 1 -Control Plane
        • Understanding Kubernetes: Part 2 -Worker Node
        • Understanding Kubernetes: Part 3 -Pod
        • Understanding Kubernetes: Part 4-ReplicaSets
        • Understanding Kubernetes: Part 5 -Deployment
        • Understanding Kubernetes: Part 6 -DaemonSets
        • Understanding Kubernetes: Part 7 -StatefulSet
        • Understanding Kubernetes: Part 8 -ConfigMap
        • Understanding Kubernetes: Part 9 -Kubernetes Secret
        • Understanding Kubernetes: Part 10 -StorageClass
        • Understanding Kubernetes: Part 11 -Persistent Volume (PV)
        • Understanding Kubernetes: Part 12 -Persistent Volume Claim (PVC)
        • Understanding Kubernetes: Part 13 -Services
        • Understanding Kubernetes: Part 14 -ClusterIP Service
        • Understanding Kubernetes: Part 15 -NodePort Service
        • Understanding Kubernetes: Part 16 -Load Balancer Service
        • Understanding Kubernetes: Part 17 -Ingress
        • Understanding Kubernetes: Part 18 -Ingress Controller
        • Understanding Kubernetes: Part 19 -Headless Service
        • Understanding Kubernetes: Part 20-Network Policy
        • Understanding Kubernetes: Part 21 -CNI
        • Understanding Kubernetes: Part 22 Kubernetes Resource Requests & Limits
        • Understanding Kubernetes: Part 23 Node Selector
        • Understanding Kubernetes: Part 24 Taints and Tolerations
        • Understanding Kubernetes: Part 25 Affinity and Anti-Affinity
        • Understanding Kubernetes: Part 26 Preemption and Priority
        • Understanding Kubernetes: Part 27 Role and RoleBinding
        • Understanding Kubernetes: Part 28 ClusterRole and ClusterRoleBinding
        • Understanding Kubernetes: Part 29 Service Account
        • Understanding Kubernetes: Part 30 Horizontal Pod Autoscaler (HPA)
        • Understanding Kubernetes: Part 31 Vertical Pod Autoscaler (VPA)
        • Understanding Kubernetes: Part 33 Startup Probe
        • Understanding Kubernetes: Part 34 Liveness Probe
        • Understanding Kubernetes: Part 35 Readiness Probe
        • Understanding Kubernetes: Part 36 Container Network Interface (CNI)
        • Understanding Kubernetes: Part 37 Container Runtime Interface (CRI)
        • Understanding Kubernetes: Part 38 Container Storage Interface (CSI)
      • Cloudflare
        • Cloudflare Tunnel for Secure HTTP Routing
      • Nginx
        • Nginx use cases that every engineer must know
Powered by GitBook
On this page
  • 1. What is the Difference Between RUN and CMD?
  • 2. How to Use Multi-Stage Builds in Dockerfiles?
  • 3. What is the Purpose of the EXPOSE Instruction?
  • 4. What is the Difference Between ARG and ENV?
  • 5. How Do You Optimize Docker Images for Size?
  • 6. How Can You Persist Data Across Container Restarts?
  • 7. What is the Purpose of the LABEL Instruction?
  • 8. Why Might You Use HEALTHCHECK in a Dockerfile?
  • 9. How Can You Handle Secrets in Dockerfiles?
  • 10. What is the Difference Between CMD and ENTRYPOINT?
  • 🚀 Struggling with Kubernetes Concepts? We’ve Got You Covered!
  1. Blogs
  2. Medium Articles
  3. DevOps/SRE Interview Questions and Answers

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

PreviousTop 10 Common DevOps/SRE Interview Questions and Answers on Kubernetes Best PracticesNextTop 10 Common DevOps/SRE Interview Questions and Answers on Grafana

Last updated 5 months ago

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="you@example.com" 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.

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

YouTube:

LinkedIn:

Instagram:

👉 You won’t find a better way to master Kubernetes!

Techwithpatil
Tech with Patil
techwithpatil
Enroll Now
https://cloudops0.gumroad.com/l/k8s
Docker
Kubernetes
Containers
Microservices
Cicd