| name | docker-containerization |
| description | Docker container management and image creation. Use when building Docker images, running containers, managing container networks, or creating containerized development environments. |
Docker Containerization
Build and manage Docker containers for development and deployment.
Image Management
# Build image from Dockerfile
docker build -t myapp:latest .
# Build with build arguments
docker build --build-arg VERSION=1.0 -t myapp:1.0 .
# List images
docker images
# Remove image
docker rmi myapp:latest
Container Operations
# Run container interactively
docker run -it ubuntu:22.04 /bin/bash
# Run detached with port mapping
docker run -d -p 8080:80 --name webserver nginx
# Run with volume mount
docker run -v /host/path:/container/path myapp
# Run with environment variables
docker run -e DATABASE_URL=postgres://... myapp
Dockerfile Best Practices
FROM ubuntu:22.04
# Install dependencies in single layer
RUN apt-get update && apt-get install -y \
build-essential \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy only necessary files
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY src/ /app/src/
WORKDIR /app
CMD ["python", "src/main.py"]
Multi-Architecture Builds
# Create builder for multi-arch
docker buildx create --use --name multiarch
# Build for ARM and x86
docker buildx build --platform linux/amd64,linux/arm64 \
-t myapp:latest --push .
Container Networking
# Create network
docker network create mynet
# Run containers on network
docker run -d --network mynet --name db postgres
docker run -d --network mynet --name app myapp
Docker Compose
version: '3.8'
services:
web:
build: .
ports:
- "8080:80"
volumes:
- ./data:/app/data
environment:
- DEBUG=true