πŸš€ DevOps Roadmap 2025 – Step 4: Docker (Part 2) – Mastering Dockerfile & Docker Images

Harshal SonarHarshal Sonar
5 min read

In Part 1 of our Docker series, we covered Docker basics, architecture, installation, and important commands.

Now in Part 2, we’ll deep dive into Dockerfile and Docker Images β€” the building blocks of containerized applications.

πŸ“Œ 1. What is a Dockerfile?

A Dockerfile is a plain text file that contains a set of instructions on how to build a Docker image.
It acts like a recipe β€” telling Docker exactly what to install, copy, and configure inside the container.

Example:

# This is a sample Dockerfile
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y python3
CMD ["python3", "--version"]

πŸ“Œ 2. Important Points about Dockerfile Naming

  • The correct default name is Dockerfile (with D capital, f small) and no file extension.
    βœ… Correct: Dockerfile
    ❌ Wrong: dockerfile.txt, Dockerfile.txt, dockerFile

  • This naming allows Docker to detect it by default when building images.

  • If you use another name, you must specify it while building:

      docker build -f MyDockerFile .
    

πŸ“Œ 3. How to Create a Dockerfile

1️⃣ Create a new directory for your project

mkdir myapp
cd myapp

2️⃣ Create a Dockerfile

touch Dockerfile

3️⃣ Add instructions to your Dockerfile

Instructions must be in uppercase for readability and convention:
Example for a Node.js app:

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Order matters – Docker builds images layer by layer. Changing a line causes all subsequent layers to rebuild.

  • Minimize RUN commands – Combine them to reduce layers.

  • Avoid unnecessary files – Use .dockerignore to skip unwanted files like logs, node_modules, or secrets.

πŸ“Œ 4. Dockerfile Instructions Explained

InstructionDescriptionExample
FROMSets the base imageFROM ubuntu:20.04
WORKDIRSets the working directoryWORKDIR /app
COPYCopies files into the imageCOPY . /app
ADDSimilar to COPY but supports URLs & tar filesADD app.tar.gz /
RUNExecutes commands during buildRUN apt-get install -y curl
CMDDefault command for the containerCMD ["npm", "start"]
ENTRYPOINTMakes the container behave like an executableENTRYPOINT ["python3"]
EXPOSEDocuments the port the container listens onEXPOSE 8080
ENVSets environment variablesENV NODE_ENV=production
ARGDefines variables for build-timeARG VERSION=1.0
VOLUMECreates a mount point for external storageVOLUME /data
USERSets the user to run the containerUSER appuser

πŸ“Œ 5. How to Build a Docker Image

docker build -t myapp:1.0 .
  • -t β†’ Tags the image (name:version)

  • . β†’ Path to Dockerfile (current directory)


πŸ“Œ 6. How to Run a Docker Image

docker run -it --name mycontainer myapp:1.0
  • -it β†’ Interactive terminal

  • --name β†’ Container name


πŸ“Œ 7. Docker Image Management Commands

docker images          # List all images
docker rmi image_id    # Remove image
docker pull ubuntu     # Download image from Docker Hub
docker push myrepo/app # Push image to Docker Hub
docker tag imgid repo/image:tag # Tag image

πŸ“Œ 8. Best Practices for Writing Dockerfiles

βœ… Use official base images
βœ… Minimize the number of layers
βœ… Use .dockerignore to avoid copying unnecessary files
βœ… Combine RUN commands to reduce image size
βœ… Use specific versions instead of latest
βœ… Keep images small for faster deployments

How to Reduce Docker Image Size

Smaller images mean faster builds, quicker deployments, and less storage usage.

Ways to reduce size:

  1. Use smaller base images – Instead of ubuntu, use alpine.

     FROM python:3.9-alpine
    
  2. Combine RUN commands –

     RUN apt-get update && apt-get install -y \
         curl \
         git \
      && rm -rf /var/lib/apt/lists/*
    
  3. Use .dockerignore – Exclude files you don’t need inside the image.

  4. Multi-stage builds – Build in one stage, copy only final output to a lightweight image.

     FROM golang:1.17 AS builder
     WORKDIR /app
     COPY . .
     RUN go build -o myapp
    
     FROM alpine
     COPY --from=builder /app/myapp /myapp
     CMD ["/myapp"]
    
  5. Avoid unnecessary packages – Install only what’s needed.


πŸ“Œ 9. Common Docker Interview Questions (Part 2)

Q1: What is the difference between CMD and ENTRYPOINT?
A: CMD sets default arguments for the container, which can be overridden. ENTRYPOINT defines the fixed executable.

Q2: How does Docker build an image?
A: Docker reads the Dockerfile line by line, creating a new layer for each instruction.

Q3: How can you reduce Docker image size?
A: Use smaller base images, combine RUN commands, use .dockerignore, multi-stage builds, and remove unnecessary packages.

Q4: Difference between COPY and ADD?
A: COPY only copies files/directories. ADD supports remote URLs and automatically extracts archives.

Q5: What is a multi-stage build?
A: A build process where you use multiple FROM statements to separate build and runtime stages, keeping the final image lightweight.


πŸ“Œ 10. Hands-on Example

Simple Python App
Dockerfile

FROM python:3.10
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]

Build & Run

docker build -t python-app .
docker run -it --rm python-app

πŸ’‘ Conclusion:
Mastering Dockerfiles and images is essential for creating consistent, portable, and optimized containers.
In the next part, we’ll explore Docker Networking & Volumes.

πŸ“’ If you missed Part 1: Docker Basics, read it here β†’ https://harshalsonar.hashnode.dev/devops-roadmap-2025-step-4-mastering-docker-part-1-beginner-to-intermediate-guide

0
Subscribe to my newsletter

Read articles from Harshal Sonar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshal Sonar
Harshal Sonar