Introduction to Docker

Introduction to Docker – Why It Matters

What is Docker?

Docker is an open-source platform that allows you to package applications and their dependencies into lightweight, portable containers.

🔹 Traditional Deployment Problem

  • Works on my machine 

  • Dependency conflicts

  • Environment mismatch

🔹 Docker Solution

  • Consistent environments

  • Easy setup

  • Isolated applications

What is a Container?

A container is:

  • Lightweight

  • Portable

  • Isolated runtime environment

  • Faster than virtual machines

Docker vs Virtual Machines

FeatureDocker                        Virtual Machine
Size            MBs                        GBs
Boot Time            Seconds                        Minutes
OS Required            Shares Host OS                        Full OS required

Installing Docker & Running First Container

Step 1: Install Docker

Download from official website:

(Available for Windows, macOS, Linux)

Step 2: Verify Installation

docker --version

Step 3: Run Your First Container

docker run hello-world

This command:

  • Pulls image from Docker Hub

  • Creates container

  • Runs it

  • Displays success message

Understanding Docker Images & Containers

What is a Docker Image?

An image is a blueprint for a container.

Example:

docker pull python:3.12

What is a Container?

A running instance of an image.

docker run -it python:3.12

Useful Commands

docker images
docker ps
docker ps -a
docker stop <container_id>
docker rm <container_id>

Writing Your First Dockerfile (Python App)

Let’s Dockerize a simple Python app.

Project Structure

myapp/
├── app.py
├── requirements.txt
└── Dockerfile

Example: app.py

print("Hello from Dockerized Python App!")

Dockerfile

FROM python:3.12

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Build Image

docker build -t my-python-app .

Run Container

docker run my-python-app

Dockerizing a Django Application

Since you're working heavily with Django, this is important.

Example Django Dockerfile

FROM python:3.12

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

WORKDIR /code

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["gunicorn", "project.wsgi:application", "--bind", "0.0.0.0:8000"]

Why Use Gunicorn?

Gunicorn is a production-ready WSGI server for Python apps.


Comments

Popular posts from this blog

Database Integration in FastAPI (SQLAlchemy CRUD)

Middleware & CORS in FastAPI

Python Data Handling