Docker for Backend Developers: A Practical Introduction
Learn how Docker works, why backend developers need it, and how to containerize your first Python or Go application in under 30 minutes.
Docker for Backend Developers: A Practical Introduction
If you’ve ever heard “it works on my machine” — Docker is the fix. Docker lets you package your application and all its dependencies into a container that runs identically everywhere: your laptop, your colleague’s machine, and production.
This guide is for backend developers who have heard of Docker but haven’t used it seriously yet.
What Is Docker and Why Does It Matter?
A container is a lightweight, isolated environment. Unlike a virtual machine, it doesn’t virtualise an entire OS — it shares the host kernel but keeps processes, files, and networking separate.
For backend developers this means:
- No more “it works on my machine” bugs caused by different library versions
- Reproducible builds that deploy consistently
- Easier local development with databases and services running as containers
Installing Docker
Download Docker Desktop for Mac or Windows. On Linux, install Docker Engine directly from your distro’s package manager.
Verify it works:
docker --version
docker run hello-world
Your First Dockerfile
A Dockerfile is a recipe for building a container image. Here’s one for a Python Flask app:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "-m", "gunicorn", "app:app", "--bind", "0.0.0.0:8000"]
Key Instructions
| Instruction | What it does |
|---|---|
FROM | Base image to build on |
WORKDIR | Sets the working directory inside the container |
COPY | Copies files from your machine into the image |
RUN | Executes a command during the build step |
EXPOSE | Documents which port the app listens on |
CMD | Default command to run when the container starts |
Build and Run Your Container
# Build the image, tag it "myapp"
docker build -t myapp .
# Run it, mapping host port 8000 to container port 8000
docker run -p 8000:8000 myapp
Visit http://localhost:8000 — your app is running inside a container.
Using Docker Compose for Local Development
Most backend apps need more than just the app process. You’ll want a database, maybe a cache. Docker Compose manages multi-container setups with a single file:
# docker-compose.yml
version: "3.9"
services:
app:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgresql://postgres:password@db:5432/mydb
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Start everything with:
docker compose up
Both the app and Postgres start together. Stop with Ctrl+C, or run detached with docker compose up -d.
Essential Docker Commands
docker ps # List running containers
docker ps -a # List all containers including stopped ones
docker logs <id> # View container logs
docker exec -it <id> sh # Open a shell inside a running container
docker stop <id> # Stop a container
docker rm <id> # Delete a container
docker images # List downloaded images
docker rmi <image> # Delete an image
Best Practices
Use specific base image tags — python:3.12-slim not python:latest. Latest changes unexpectedly.
Copy requirements before source code — Docker caches layers. If you copy requirements.txt first and run pip install, that layer is cached until requirements change, making rebuilds much faster.
Keep images small — Use slim or alpine variants. Avoid installing dev tools in production images.
Never store secrets in the image — Pass them as environment variables at runtime, not baked into the Dockerfile.
What to Learn Next
Once you’re comfortable with Docker basics, explore:
- Multi-stage builds (smaller production images)
- Docker volumes for persistent data
- Container orchestration with Kubernetes
Check out the official Docker documentation for deep dives on each topic.
Start containerising your projects now — future-you deploying to production will thank you.
Related Articles
Containerising a Backend Service: From Docker to Kubernetes
A practical walkthrough of containerising a Python backend service with Docker, deploying it to Kubernetes on ECS, and the production gaps that only show up once real traffic hits.
Environment Variables Explained: Keeping Secrets Out of Code
Learn what environment variables are and why every developer needs them. This guide covers how to use .env files, os.environ in Python, process.env in Node.js, and best practices.
Introduction to FastAPI: Modern Python APIs Made Simple
Get started with FastAPI Python to build fast, modern APIs with automatic documentation. This beginner guide covers installation, routes, request validation, and more.