Docker
Definition
Docker is an open-source containerization platform that allows developers to package applications and their dependencies into standardized containers, ensuring consistent execution across any environment.
Core Concepts
- Image: A read-only template containing everything needed to run an application
- Container: A running instance of an image, an independent and isolated runtime environment
- Dockerfile: A script defining how to build an image
- Registry: A service for storing and distributing images (e.g., Docker Hub)
Dockerfile Example
# Multi-stage build example
FROM python:3.11.7-slim as builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.11.7-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]
Common Commands
docker build -t myapp . # Build image
docker run -d -p 80:80 myapp # Run container
docker ps # List running containers
docker logs <container> # View container logs
docker exec -it <container> sh # Enter container shell