Docker Basic
Core Concepts
- Image: Lightweight, standalone, executable package containing code, runtime, system tools, and OS.
- Container: Runnable instance of an image, representing a specific execution environment.
- Volume: Persistent data storage mechanism, ensuring data durability and persistence even if containers are stopped or removed.
- Network: Facilitates connectivity between Docker containers.
Basic Commands
docker build -t <image_name>
: Build a Docker image.docker images
: View Docker images.docker ps
: View running containers.docker ps -a
: View all containers.docker prune
: Remove all idle containers.docker rm <container_id>
: Delete a container.docker run -p <host_port>:<container_port> <image_name>
: Run a container.docker run -p <host_port>:<container_port> -v "$(pwd):/app" -v /app/node_modules <image_name>
: Run a container with live code updates.
Dockerfile Example
FROM node:20-alpine
WORKDIR /app
COPY . .
CMD node hello.js
Docker Compose
- Docker Compose simplifies multi-container Docker applications.
- Define services in a YAML file (
docker-compose.yaml
). - Use
docker compose up
to start services defined in thedocker-compose.yaml
. - Use
docker compose watch
for development, syncing changes and rebuilding containers as necessary.
Docker Compose YAML Example
services:
web:
build:
context: .
ports:
- 5173:5173
volumes:
- .:/app
- /app/node_modules
develop:
watch:
- path: ./frontend/package.json
action: rebuild
- path: ./frontend/package-lock.json
action: rebuild
- path: ./frontend
target: /app
action: sync
Docker Compose Watch Mode
- Enables live development and testing.
- Syncs changes, rebuilds, and restarts containers automatically.
docker compose watch