Docker Fundamentals
Master Docker containerization from basics to advanced concepts. Learn to build, run, and manage containers for your applications.
What is Docker?
Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization technology. Containers allow developers to package an application with all its dependencies into a standardized unit for software development.
Unlike virtual machines, containers share the host system's kernel, making them lightweight and fast. This means you can run many more containers on a given hardware combination than virtual machines.
Docker Architecture
Docker uses a client-server architecture with the following key components:
- Docker Daemon - The background service running on the host that manages building, running, and distributing Docker containers
- Docker Client - The command-line tool that allows users to interact with the Docker daemon
- Docker Registry - A storage and distribution system for Docker images (Docker Hub is the default public registry)
- Docker Images - Read-only templates used to create containers
- Docker Containers - Running instances of Docker images
Key Docker Concepts
Images vs Containers
A Docker image is like a blueprint or template - it contains everything needed to run an application (code, runtime, libraries, environment variables, config files). A container is a running instance of an image. You can run multiple containers from the same image.
Dockerfile
A Dockerfile is a text document containing instructions to build a Docker image. Each instruction creates a layer in the image, making it efficient to rebuild images when only specific parts change.
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes.
Benefits of Docker
- đ Consistency - Same environment from development to production
- ⥠Speed - Containers start in seconds, not minutes
- đĻ Portability - Run anywhere Docker is installed
- đ§ Isolation - Each container is isolated from others
- đ° Efficiency - Better resource utilization than VMs
- đ Scalability - Easy horizontal scaling
Docker Best Practices
- Use official base images when possible
- Keep images small by using multi-stage builds
- Don't run processes as root inside containers
- Use .dockerignore to exclude unnecessary files
- Tag images with meaningful version numbers
- Scan images for security vulnerabilities
# Pull an image from Docker Hub
docker pull nginx:latest
# List all images
docker images
# Run a container
docker run -d -p 8080:80 --name my-nginx nginx:latest
# List running containers
docker ps
# Stop a container
docker stop my-nginx
# Remove a container
docker rm my-nginx
# Remove an image
docker rmi nginx:latest
# Use an official Python runtime as base
FROM python:3.11-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set work directory
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy project
COPY . .
# Expose port
EXPOSE 8000
# Run the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Test Your Knowledge
Answer these questions to check your understanding