Docker and Containerization: Basic Concepts and Usage
Docker packages applications into containers for consistent and portable deployment. This guide covers Docker basics, Dockerfile writing, and container management.
Last updated:What is Docker?
Docker is a platform that packages applications and their dependencies into containers. Containers are lightweight, isolated runtime environments.
VM vs Container: Virtual Machines run a full operating system (GBs). Containers share the host OS kernel and take up MBs.
Consistency: Solves the 'works on my machine' problem. Development, test, and production environments use the same container.
Portability: Containers run on any Docker-supported platform (Linux, Windows, Mac, cloud).
Docker Core Concepts
Image: Template for a container. Stored as read-only layers. Built from a Dockerfile.
Container: Running instance of an image. Each container runs as an isolated process.
Dockerfile: Text file that defines how to build an image. Contains instructions like FROM, RUN, COPY, CMD.
Registry: Repository that stores and distributes images. Docker Hub is the most popular public registry.
Volume: Storage used to persist container data. Data is preserved even if the container is deleted.
Writing a Dockerfile
FROM node:18-alpine: Use Node.js 18 (Alpine Linux) as the base image. Alpine is a small-sized base image.
WORKDIR /app: Set the working directory to /app. All subsequent commands run in this directory.
COPY package*.json ./: Copy package.json and package-lock.json. Install dependencies with RUN npm install.
COPY . .: Copy all source code. Create production build with RUN npm run build.
EXPOSE 3000: Specify that the container listens on port 3000. Define the command to run when container starts with CMD ["npm", "start"].
Basic Docker Commands
docker build -t myapp:1.0 .: Build image from Dockerfile. Tag image with name:version using -t.
docker run -p 3000:3000 myapp:1.0: Run container. Map host port to container port with -p.
docker ps: List running containers. docker ps -a: List all containers (including stopped ones).
docker stop <container_id>: Stop container. docker rm <container_id>: Remove container.
docker images: List local images. docker rmi <image_id>: Remove image.
Docker Compose
Docker Compose is a tool for defining and managing multi-container applications. Configured with docker-compose.yml file.
Services: Each service is a container. Define services like web (frontend), api (backend), db (database).
Volumes: Define volumes for data persistence. networks: Create networks for inter-service communication.
docker compose up: Start all services. docker compose down: Stop and remove all services.
docker compose up -d: Run in detached mode (background). docker compose logs -f: Follow logs.
Best Practices
Small images: Use Alpine or distroless base images. Remove build dependencies from final image with multi-stage build.
Layer caching: COPY dependencies (rarely changing) first, code (frequently changing) later. This optimizes build cache.
.dockerignore: Ignore unnecessary files (node_modules, .git, logs). This reduces build context.
Security: Use non-root user instead of root. Create user with RUN adduser --disabled-password appuser.
Health check: Use HEALTHCHECK instruction to perform container health check. Docker automatically restarts unhealthy containers.