Containerization is a fundamental concept in modern software development, enabling applications to run consistently across diverse environments. Docker is the leading platform for containerization, offering tools to create, deploy, and manage lightweight, portable containers.
Containers encapsulate an application and its dependencies, ensuring it runs the same way regardless of the environment. This eliminates the common problem of “it works on my machine.”
Docker is an open-source platform that automates the deployment of applications inside containers. It simplifies the creation and management of containers by offering a complete ecosystem, including Docker Engine, Docker Hub, and Docker CLI.
docker --version
.docker pull
: Download an image from a registry.docker build
: Create an image from a Dockerfile.docker run
: Start a container from an image.docker ps
: List running containers.docker stop
: Stop a running container.Create a Dockerfile
to define the image:
# Use an official Python runtime as the base image
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy application files into the container
COPY . /app
# Install dependencies
RUN pip install -r requirements.txt
# Define the command to run the application
CMD ["python", "app.py"]
Build the image with:
docker build -t my-python-app .
Start a container using the image:
docker run -d -p 5000:5000 my-python-app
This maps port 5000 on the host to port 5000 in the container.
List running containers:
docker ps
Stop a container:
docker stop [container_id]
Remove unused containers:
docker rm [container_id]
Docker Compose simplifies the management of multi-container applications by using a YAML file to define services.
Example docker-compose.yml
:
version: "3.8"
services:
web:
image: my-python-app
ports:
- "5000:5000"
redis:
image: redis:alpine
Using Docker Compose:
Start all services:
docker-compose up
Stop services:
docker-compose down
alpine
).my-app:1.0
) for traceability.docker system prune
Docker plays a pivotal role in DevOps by enabling consistent environments across development, testing, and production stages. Key use cases include:
In the next section, we’ll explore Kubernetes, the industry-standard platform for orchestrating and managing containers at scale. This complements Docker by automating deployment, scaling, and monitoring of containerized applications.