Introduction
Docker has revolutionized the way we build, ship, and run applications. Whether you're a backend developer, DevOps engineer, or just curious about containers, Docker is a tool worth learning.
In this guide, weβll cover:
What Docker is
How it works
Installing Docker
Key concepts: images, containers, Dockerfiles
Practical examples
Tips and best practices
π¦ What is Docker?
Docker is a platform that allows you to develop, ship, and run applications inside containers. Containers are lightweight, standalone, and executable packages that include everything needed to run a piece of softwareβcode, runtime, libraries, and dependencies.
βοΈ How Does Docker Work?
Docker runs containers using the Docker Engine, a lightweight runtime and tooling set.
Hereβs how it works:
Dockerfile β describes the environment
Docker Image β built from Dockerfile
Docker Container β running instance of the image
π οΈ Installing Docker
On Windows / macOS:
Download Docker Desktop: https://www.docker.com/products/docker-desktop
Follow the installation instructions.
Verify with:
docker --version
On Linux (Ubuntu):
sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
π Key Docker Concepts
π§± 1. Dockerfile
A script with instructions to build Docker images.
Example:
Use an official Python runtime
FROM python:3.10
Set working directory
WORKDIR /app
Copy source code
COPY . .
Install dependencies
RUN pip install -r requirements.txt
Command to run
CMD ["python", "app.py"]
πΈ 2. Docker Image
An immutable snapshot built from a Dockerfile.
Build:
docker build -t my-python-app .
π¦ 3. Docker Container
A running instance of a Docker image.
Run:
docker run -d -p 5000:5000 my-python-app
π Docker CLI Cheatsheet
List containers
docker ps -a
Stop container
docker stop
Remove container
docker rm
List images
docker images
Remove image
docker rmi
Start shell in a container
docker exec -it /bin/bash
π Practical Example: Python Flask App in Docker
File Structure:
.
βββ app.py
βββ requirements.txt
βββ Dockerfile
app.py
from flask import Flask
app = Flask(name)
@app.route('/')
def home():
return "Hello from Docker!"
if name == 'main':
app.run(host='0.0.0.0', port=5000)
requirements.txt
flask
Dockerfile
(See earlier example)
Build and Run:
docker build -t flask-app .
docker run -d -p 5000:5000 flask-app
Visit: http://localhost:5000
π§ Docker Best Practices
Use .dockerignore to exclude files from the image
Minimize layers in your Dockerfile
Use multi-stage builds for smaller images
Keep containers stateless
Use volumes for persistent data
π§° Bonus: Docker Compose
For multi-container applications:
docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
Run it:
docker-compose up --build
Top comments (0)