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
| Feature | Docker | 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 imagesdocker psdocker ps -adocker 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└── DockerfileExample: app.py
print("Hello from Dockerized Python App!")Dockerfile
FROM python:3.12WORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txtCOPY . .CMD ["python", "app.py"]Build Image
docker build -t my-python-app .Run Container
docker run my-python-appDockerizing a Django Application
Since you're working heavily with Django, this is important.
Example Django Dockerfile
FROM python:3.12ENV PYTHONDONTWRITEBYTECODE=1ENV PYTHONUNBUFFERED=1WORKDIR /codeCOPY requirements.txt .RUN pip install -r requirements.txtCOPY . .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
Post a Comment