<aside> 📚 📁 Category: Docker

</aside>

What is Docker?

Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers package software and all its dependencies so the application runs quickly and reliably across computing environments.

<aside> â„šī¸ Key Benefits: Consistency across environments, isolation, portability, efficient resource utilization, and rapid deployment.

</aside>

Docker Architecture

Essential Docker Commands

# Pull an image from Docker Hub
docker pull nginx:latest

# List all images
docker images

# Remove an image
docker rmi nginx:latest

# Build an image from Dockerfile
docker build -t my-app:1.0 .

# Tag an image
docker tag my-app:1.0 username/my-app:1.0

# Push image to registry
docker push username/my-app:1.0
# Run a container
docker run -d --name my-nginx -p 8080:80 nginx

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# Stop a container
docker stop my-nginx

# Start a stopped container
docker start my-nginx

# Restart a container
docker restart my-nginx

# Remove a container
docker rm my-nginx

# Remove all stopped containers
docker container prune
# View container logs
docker logs my-nginx

# Follow container logs
docker logs -f my-nginx

# Execute command in running container
docker exec -it my-nginx bash

# View container resource usage
docker stats

# Inspect container details
docker inspect my-nginx

Creating a Simple Dockerfile

# Use official Python runtime as base image
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Copy requirements file
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose port
EXPOSE 8000

# Define environment variable
ENV PYTHONUNBUFFERED=1

# Run application
CMD ["python", "app.py"]
# Build the image
docker build -t my-python-app:1.0 .

# Run the container
docker run -d -p 8000:8000 --name python-app my-python-app:1.0

Docker Volumes

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

# Create a volume
docker volume create my-data

# List volumes
docker volume ls

# Run container with volume
docker run -d \\
  --name postgres-db \\
  -v my-data:/var/lib/postgresql/data \\
  -e POSTGRES_PASSWORD=mysecret \\
  postgres:15

# Inspect volume
docker volume inspect my-data

# Remove volume
docker volume rm my-data

Docker Networks

Docker networks allow containers to communicate with each other.

# Create a network
docker network create my-network

# List networks
docker network ls

# Run containers on same network
docker run -d --name web --network my-network nginx
docker run -d --name api --network my-network my-api:1.0

# Containers can now communicate using container names
# web can reach api at: <http://api>:port

# Inspect network
docker network inspect my-network

# Remove network
docker network rm my-network

ASP.NET Core Example

# Multi-stage build for ASP.NET Core
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# Copy csproj and restore dependencies
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"

# Copy everything else and build
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

# Publish
FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

# Runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.dll"]