π DevOps Roadmap 2025 β Step 4: Docker (Part 2) β Mastering Dockerfile & Docker Images

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
Instruction | Description | Example |
FROM | Sets the base image | FROM ubuntu:20.04 |
WORKDIR | Sets the working directory | WORKDIR /app |
COPY | Copies files into the image | COPY . /app |
ADD | Similar to COPY but supports URLs & tar files | ADD app.tar.gz / |
RUN | Executes commands during build | RUN apt-get install -y curl |
CMD | Default command for the container | CMD ["npm", "start"] |
ENTRYPOINT | Makes the container behave like an executable | ENTRYPOINT ["python3"] |
EXPOSE | Documents the port the container listens on | EXPOSE 8080 |
ENV | Sets environment variables | ENV NODE_ENV=production |
ARG | Defines variables for build-time | ARG VERSION=1.0 |
VOLUME | Creates a mount point for external storage | VOLUME /data |
USER | Sets the user to run the container | USER 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:
Use smaller base images β Instead of
ubuntu
, usealpine
.FROM python:3.9-alpine
Combine RUN commands β
RUN apt-get update && apt-get install -y \ curl \ git \ && rm -rf /var/lib/apt/lists/*
Use
.dockerignore
β Exclude files you donβt need inside the image.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"]
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 AppDockerfile
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
Subscribe to my newsletter
Read articles from Harshal Sonar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
