Skip to main content

Docker Basics

Essential Docker commands for container lifecycle management and basic operations.

Container Lifecycle

Running Containers

# Run a container
docker run -d --name myapp -p 8080:80 nginx

# Run with interactive shell
docker run -it ubuntu:latest /bin/bash

# Run with environment variables
docker run -e ENV_VAR=value myapp

# Run with volume mount
docker run -v /host/path:/container/path myapp

# Run with current directory mounted
docker run -v $(pwd):/app myapp

# Remove container automatically when it exits
docker run --rm myapp

Start/Stop/Restart

# Start container
docker start container_name

# Stop container
docker stop container_name

# Restart container
docker restart container_name

# Kill container (force stop)
docker kill container_name

# Pause/unpause container
docker pause container_name
docker unpause container_name

Container Information

Listing and Inspecting

# List running containers
docker ps

# List all containers
docker ps -a

# Inspect container
docker inspect container_name

# Show container processes
docker top container_name

# Show resource usage
docker stats container_name

# Show port mappings
docker port container_name

Logs and Debugging

# Show container logs
docker logs container_name

# Follow logs in real-time
docker logs -f container_name

# View logs with timestamps
docker logs -t container_name

# View last N lines of logs
docker logs --tail 50 container_name

# View logs since specific time
docker logs --since 2023-01-01T00:00:00 container_name

Accessing Containers

# Execute command in running container
docker exec -it container_name /bin/bash

# Run container with debugging
docker run -it --entrypoint /bin/bash myapp

# Copy files from container
docker cp container_name:/path/to/file /host/path

# Copy files to container
docker cp /host/path container_name:/path/to/file

Container Cleanup

Removing Containers

# Remove container
docker rm container_name

# Remove running container (force)
docker rm -f container_name

# Remove all stopped containers
docker container prune

File System Operations

# View container filesystem changes
docker diff container_name

# Export container as tar
docker export container_name > container.tar

System Commands

System Information

# Show Docker version
docker version

# Show system information
docker info

# Show disk usage
docker system df

# Show detailed disk usage
docker system df -v

# Show running processes
docker system events

System Cleanup

# Remove unused containers, networks, images
docker system prune

# Remove everything including volumes
docker system prune -a --volumes

# Remove unused containers
docker container prune

Quick Reference

Essential Commands

  • docker run - Create and start a new container
  • docker ps - List containers
  • docker logs - View container logs
  • docker exec - Execute commands in running container
  • docker stop - Stop a container
  • docker rm - Remove a container
  • docker system prune - Clean up unused resources

Common Patterns

  • Always use --rm for temporary containers
  • Use -d to run containers in background
  • Use -it for interactive sessions
  • Mount volumes with -v for persistent data
  • Set environment variables with -e