Introduction
Docker has become the industry standard for containerization. In this post, I’ll cover the basics of Docker and how to get started with containers.
What is Docker?
Docker is a containerization platform that packages your application and all its dependencies into a standardized unit called a container. This ensures your application runs the same way regardless of where the container is deployed.
Key Benefits
- Consistency: Run anywhere, anytime with the same behavior
- Isolation: Containers are isolated from each other
- Efficiency: Lightweight compared to virtual machines
- Scalability: Easy to scale applications horizontally
Getting Started
Installation
First, install Docker from the official website.
Your First Container
Let’s run a simple container:
docker run hello-world
This command:
- Pulls the
hello-worldimage from Docker Hub - Creates a new container from that image
- Runs the container
- Exits
Understanding Images and Containers
- Image: A template or blueprint (like a class in OOP)
- Container: A running instance of an image (like an object)
Creating Your Own Image
Dockerfile
A Dockerfile is a text file that contains instructions to build an image.
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Key instructions:
FROM: Base image to build uponWORKDIR: Working directory inside the containerCOPY: Copy files from host to containerRUN: Execute commands during buildCMD: Default command to run
Building the Image
docker build -t my-app:1.0 .
Running Your Container
docker run -p 8000:8000 my-app:1.0
The -p flag maps ports between host and container.
Docker Compose
For multi-container applications, use Docker Compose:
version: '3.8'
services:
app:
build: .
ports:
- "8000:8000"
environment:
- DEBUG=true
database:
image: postgres:15
environment:
- POSTGRES_DB=myapp
- POSTGRES_PASSWORD=secret
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Start all services:
docker-compose up -d
Best Practices
- Keep images small: Use slim or alpine base images
- Use .dockerignore: Exclude unnecessary files
- Security: Don’t run as root, use specific versions
- Health checks: Include health check instructions
- Logging: Send logs to stdout/stderr
Common Commands
# List running containers
docker ps
# List all containers
docker ps -a
# View logs
docker logs <container-id>
# Execute command in container
docker exec -it <container-id> bash
# Remove container
docker rm <container-id>
# Remove image
docker rmi <image-id>
Next Steps
- Explore Docker Hub for pre-built images
- Learn about Docker networking and storage
- Practice with docker-compose for multi-container setups
- Explore container orchestration with Kubernetes
Conclusion
Docker has revolutionized how we develop and deploy applications. Start with these basics and gradually explore more advanced features as you become comfortable with containers.
Happy containerizing!