Mastering Docker for DevOps: From Dockerfile to Deployment

After learning Linux, Networking, Git & GitHub, and Shell Scripting, the next most important tool in a DevOps journey is Docker.
In real-world DevOps, almost every application runs inside containers. If you understand Docker properly, you understand how modern applications are deployed.
1. What is Docker? Why is it used?
What is Docker?
Docker is a tool that allows us to package an application with all its dependencies and run it anywhere*.*
This package is called a Container.
So instead of saying:
“This app works on my machine but not on server”
With Docker:
“This container will run everywhere”
Why Docker is used?
Problems before Docker:
Application works on developer machine but not on server
Different OS issues
Dependency conflicts
Difficult deployment
Scaling issues
Docker solves:
Same environment everywhere
Easy deployment
Lightweight
Fast startup
Easy scaling
Works perfectly with CI/CD
2. Virtualization vs Containerization
Virtualization
In virtualization, we create Virtual Machines.
Structure:
Hardware
Host OS
Hypervisor
VM 1 (Guest OS)
VM 2 (Guest OS)
VM 3 (Guest OS)
Each VM has its own OS, so it is heavy.
Examples:
VMware
VirtualBox
Containerization
In containerization, we don’t create full OS.
Structure:
Hardware
Host OS
Docker Engine
Container 1
Container 2
Container 3
Containers share the Host OS Kernel, so they are:
Lightweight
Fast
Efficient
Difference Summary
| Virtual Machine | Container |
|---|---|
| Heavy | Lightweight |
| Slow startup | Fast startup |
| Full OS | Shared OS |
| GB size | MB size |
| More resources | Less resources |
3. Docker Terminologies
Docker Engine
Core engine that runs containers.
Docker Client
Where we run commands:
docker build
docker run
docker pull
Docker Daemon (dockerd)
Background service that actually:
Builds images
Runs containers
Manages networks
Manages volumes
Docker CLI
Command line tool (docker command)
Docker Hub
Public registry where official images are stored.
Docker Desktop
GUI application for Windows/Mac to run Docker.
4. Containers Examples
Run Nginx container:
docker run -d -p 80:80 nginx
Run MySQL:
docker run -d \
-e MYSQL_ROOT_PASSWORD=root \
mysql
Run Ubuntu:
docker run -it ubuntu bash
5. Docker Images
Images are blueprints of containers.
Images can be:
Pulled from DockerHub
Created using Dockerfile
Pull official image:
docker pull nginx
docker pull mysql
docker pull ubuntu
6. Dockerfile – Step by Step
Dockerfile is used to build your own image.
Example Dockerfile (Flask App):
# Get the base image (OS)
FROM python:3.14-slim
# Set working directory inside container
WORKDIR /app
# Copy all files from current folder to container
COPY . .
# Install dependencies
RUN pip install -r requirements.txt
# Expose container port
EXPOSE 80
# Command to run the application
CMD ["python","run.py"]
Build Image
docker build -t flask-app .
Run Container
docker run -d -p 80:80 flask-app
7. Example Projects
Custom Nginx Example
Dockerfile:
FROM nginx
COPY index.html /usr/share/nginx/html/index.html
Build:
docker build -t my-nginx .
docker run -p 80:80 my-nginx
FastAPI Python Example
FROM python:3.14
WORKDIR /app
COPY . .
RUN pip install fastapi uvicorn
CMD ["uvicorn","main:app","--host","0.0.0.0","--port","80"]
Simple Java Example – Hello World
FROM openjdk:21
WORKDIR /app
COPY Hello.java .
RUN javac Hello.java
CMD ["java","Hello"]
8. Dockerfile for Different Applications
Python (Django)
FROM python:3.14
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python","manage.py","runserver","0.0.0.0:8000"]
Java (Spring Boot)
FROM openjdk:21
COPY target/app.jar app.jar
ENTRYPOINT ["sh","-c","java -jar /app.jar"]
Node.js
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["node","app.js"]
9. Multi-Stage Docker Builds (Reduce Image Size & Secure Image)
Problem:
My Docker image size is too big.
Solution:
Multi-stage Docker builds using Distroless images
Instead of keeping build tools in the final image, we use multiple stages. This reduces image size significantly.
Distroless Images
Distroless images contain only:
Application
Runtime
No shell
No package manager
More secure and smaller.
Example Dockerfile:
# Build Stage (builder -> for identification)
# Get base image only for the required task
FROM python:3.14-slim AS builder
WORKDIR /app
# Copy dependency file (only required files)
COPY requirements.txt .
# Install dependencies in separate folder
RUN pip install --no-cache-dir -r requirements.txt --target=/app/deps
# Copy application code
COPY . .
# Final Distroless Stage (very small & secure image)
FROM gcr.io/distroless/python3-debian12 AS deployer
WORKDIR /app
# Copy dependencies from builder stage
COPY --from=builder /app/deps /app/deps
COPY --from=builder /app .
# Environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH="/app/deps"
# Expose port
EXPOSE 80
# Run application
CMD ["run.py"]
Benefits:
Smaller image
More secure
No unnecessary tools
Faster deployment
10. Docker Volumes – Persist Data
Problem:
My container crashed and I lost all my data.
Solution:
Docker Volumes
Containers are temporary. Data inside container is lost if container is deleted.
Create Volume Example:
docker volume create mysql-data
Run MySQL with volume:
docker run -d -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql
Now data will persist even if the container is deleted.
11. Docker Compose – Multi-tier Application
Problem:
I want to run a multi-tier project that has a frontend, a backend, a database, and AI LLM integration.
Solution:
Docker Compose
Using this project for practice:
AI-Powered Bank App: AI-BankApp-DevOps by Shubham Londhe
docker-compose.yml Example:
services:
mysql:
image: mysql:8.0
container_name: bankapp-mysql
environment:
MYSQL_ROOT_PASSWORD: Test@123
MYSQL_DATABASE: bankappdb
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
networks:
- bankapp-net
ollama:
image: ollama/ollama
container_name: bankapp-ollama
healthcheck:
test: ["CMD", "ollama", "list"]
interval: 10s
timeout: 5s
retries: 5
ports:
- "11434:11434"
volumes:
- ollama-data:/root/.ollama
networks:
- bankapp-net
bankapp:
build: .
container_name: bankapp
ports:
- "8080:8080"
environment:
MYSQL_HOST: mysql
MYSQL_PORT: 3306
MYSQL_DATABASE: bankappdb
MYSQL_USER: root
MYSQL_PASSWORD: Test@123
OLLAMA_URL: http://ollama:11434
depends_on:
mysql:
condition: service_healthy
networks:
- bankapp-net
volumes:
mysql-data:
ollama-data:
networks:
bankapp-net:
driver: bridge
Run multi-container project:
docker-compose up -d
Docker Compose provides:
Networking
Volumes
Service communication
Health checks
Multi-container management
12. Docker Hub – Push Image
Login:
docker login
Tag image:
docker image tag ai-bankapp:latest username/ai-bankapp:latest
Push image:
docker push username/ai-bankapp:latest
Now anyone can run:
docker run username/ai-bankapp:latest
Final Summary – Docker Workflow
Real DevOps Docker workflow:
Write Application
↓
Create Dockerfile
↓
Build Docker Image
↓
Run Container
↓
Use Volumes for Data
↓
Use Docker Compose for Multi-tier App
↓
Use Multi-stage Builds for Small Images
End Note
If you are learning DevOps, Docker is one of the most important tools because:
Applications run in containers
Kubernetes runs containers
CI/CD pipelines build Docker images
Cloud deployments use containers
In short: No Docker → No Modern DevOps