Basic containerization workflow with practical examples for local development.
Summary
Docker has revolutionized how we develop, deploy, and manage applications. While Docker offers dozens of commands, mastering these 5 essential commands will give you a solid foundation for containerized development and enable you to handle 90% of common Docker workflows.
The 5 Essential Commands
1. docker build
- Creating Images
The foundation of Docker workflows. This command creates container images from a Dockerfile.
Basic syntax:
docker build -t my-app:latest .
Practical example:
# Build an image for a Node.js application
docker build -t my-node-app:v1.0 .
# Build with specific Dockerfile
docker build -f Dockerfile.dev -t my-app:dev .
# Build with build arguments
docker build --build-arg NODE_ENV=production -t my-app:prod .
When to use: Every time you need to package your application into a container image, whether for local development, testing, or deployment.
2. docker run
- Running Containers
Starts a new container from an image. This is where your application comes to life.
Basic syntax:
docker run [OPTIONS] IMAGE [COMMAND]
Practical examples:
# Run a simple web server
docker run -p 3000:3000 my-node-app:latest
# Run with environment variables
docker run -e NODE_ENV=development -p 3000:3000 my-node-app:latest
# Run with volume mounts for development
docker run -v $PWD:/app -p 3000:3000 my-node-app:dev
# Run in detached mode (background)
docker run -d --name my-running-app -p 3000:3000 my-node-app:latest
# Run interactively with shell access
docker run -it --rm my-node-app:latest /bin/bash
Common flags:
-p 3000:3000
- Port mapping (host:container)-v $(pwd):/app
- Volume mount for live code changes-d
- Detached mode (runs in background)--name
- Give container a custom name--rm
- Automatically remove container when it stops-it
- Interactive mode with terminal
3. docker ps
- Listing Containers
Shows running containers and their status. Essential for monitoring and debugging.
Basic syntax:
docker ps [OPTIONS]
Practical examples:
# Show only running containers
docker ps
# Show all containers (including stopped)
docker ps -a
# Show container sizes
docker ps -s
# Format output for specific information
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
Sample output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 my-node-app:latest "npm start" 5 minutes ago Up 5 minutes 0.0.0.0:3000->3000/tcp my-running-app
4. docker stop
/ docker start
- Managing Container Lifecycle
Control when containers run or stop.
Basic syntax:
docker stop CONTAINER_NAME_OR_ID
docker start CONTAINER_NAME_OR_ID
Practical examples:
# Stop a running container gracefully
docker stop my-running-app
# Stop multiple containers
docker stop container1 container2 container3
# Start a stopped container
docker start my-running-app
# Restart a container
docker restart my-running-app
# Stop all running containers
docker stop $(docker ps -q)
Pro tip: Use container names instead of IDs for easier management.
5. docker-compose up
/ docker-compose down
- Multi-Container Applications
Manages multi-container applications defined in docker-compose.yml
. Perfect for local development environments.
Basic syntax:
docker-compose up [OPTIONS]
docker-compose down [OPTIONS]
Sample docker-compose.yml:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=development
volumes:
- .:/app
- /app/node_modules
depends_on:
- database
database:
image: postgres:13
environment:
- POSTGRES_DB=myapp
- POSTGRES_USER=developer
- POSTGRES_PASSWORD=password
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
postgres_data:
Practical examples:
# Start all services in foreground
docker-compose up
# Start all services in background
docker-compose up -d
# Build images and start services
docker-compose up --build
# Start specific service
docker-compose up web
# Stop all services and remove containers
docker-compose down
# Stop services and remove volumes
docker-compose down -v
# View logs from all services
docker-compose logs
# View logs from specific service
docker-compose logs web
Complete Local Development Workflow
Here's how these commands work together in a typical development workflow:
# 1. Build your application image
docker build -t my-app:dev .
# 2. Start your development environment
docker-compose up -d
# 3. Check that everything is running
docker ps
# 4. Make code changes, rebuild if needed
docker build -t my-app:dev .
# 5. Restart services to pick up changes
docker-compose restart web
# 6. Stop everything when done
docker-compose down
Quick Reference
Command | Purpose | Common Usage |
---|---|---|
docker build |
Create images | docker build -t myapp . |
docker run |
Start containers | docker run -p 3000:3000 myapp |
docker ps |
List containers | docker ps -a |
docker stop |
Stop containers | docker stop myapp |
docker-compose up |
Start multi-container apps | docker-compose up -d |
These 5 commands form the core of most Docker workflows. Master them, and you'll be well-equipped to containerize your applications and manage local development environments effectively.