Overview ๐
Docker โ Overview & Quick Commands¶
Docker packages applications into portable containers. Below are the most-used commands and a few practical examples and best practices.
Quick Commands (common)¶
| Command | Description |
|---|---|
docker build -t <image_name>:<tag> . | Build an image from a Dockerfile in the current directory. |
docker run -p <host>:<container> <image> | Run a container and map ports. |
docker ps -a | List all containers (running and stopped). |
docker logs <container> | Show container logs. |
docker exec -it <container> /bin/bash | Open an interactive shell inside a running container. |
docker images | List local images. |
docker rmi <image> | Remove an image. |
docker system prune | Clean up unused resources. |
docker pull <image> / docker push <image> | Pull from / push to a registry. |
Dockerfile best practices¶
- Start from a small, maintained base image (e.g.,
python:3.11-slim). - Use multi-stage builds to keep final images small.
- Pin dependency versions in
requirements.txtor package manifests. - Minimize the number of layers by combining related RUN commands.
- Avoid running as
rootin production containers; create a non-root user. - Add healthchecks for production services.
Example (multi-stage) Dockerfile snippet:
FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:stable-alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
HEALTHCHECK --interval=30s CMD curl -f http://localhost/ || exit 1
Docker Compose quick example¶
version: '3.8'
services:
app:
build: .
ports:
- "8080:80"
environment:
- NODE_ENV=production
redis:
image: redis:6-alpine
volumes:
- redis-data:/data
volumes:
redis-data:
CI/CD tips for images¶
- Build images in CI, tag with commit SHA and semantic tag, and push to a registry (ACR/ECR/GCR/Docker Hub).
- Scan images for vulnerabilities (Trivy, Clair) as part of pipeline.
- Use immutable tags for deployments (avoid
latestin production).
If you want, I can add a runnable examples/docker-quickstart/ folder with a small sample app, Dockerfile, and a GitHub Actions pipeline next.