Skip to content

Docker Reference

Docker is a platform for developing, shipping, and running applications in containers. This section provides comprehensive command references for working with Docker.

Quick Navigation

SectionDescription
ImagesPull, list, remove, and manage Docker images
ContainersCreate, run, manage, and debug containers
ComposeMulti-container applications with Docker Compose

Common Workflows

Quick Container Setup

bash
# Pull an image
docker pull ubuntu:latest

# Create and run a container
docker run -it --name myapp ubuntu:latest /bin/bash

# Verify running containers
docker container list

Development Workflow

bash
# Start development containers
docker-compose up -d

# View logs
docker-compose logs -f

# Execute commands in running container
docker-compose exec app bash

# Stop containers
docker-compose down

Cleanup

bash
# Remove stopped containers
docker container prune

# Remove unused images
docker image prune -a

# Clean everything (use with caution)
docker system prune -a --volumes

Installation

Linux

bash
# Install Docker using convenience script
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add user to docker group (to run without sudo)
sudo usermod -aG docker $USER

# Start Docker service
sudo systemctl start docker
sudo systemctl enable docker

macOS

Download and install Docker Desktop for Mac.

Windows

Download and install Docker Desktop for Windows.

Essential Commands

System Information

bash
# Docker version
docker --version

# System information
docker info

# Disk usage
docker system df

Quick Reference

TaskCommand
List imagesdocker image list
List containersdocker container list --all
View logsdocker logs <container>
Execute commanddocker exec -it <container> <command>
Clean systemdocker system prune

Best Practices

Security

  1. Don't run containers as root: Use USER directive in Dockerfile
  2. Scan images for vulnerabilities: Use docker scan <image>
  3. Use official images: Pull from verified publishers when possible
  4. Keep images updated: Regularly update base images

Performance

  1. Use .dockerignore: Exclude unnecessary files from build context
  2. Multi-stage builds: Reduce final image size
  3. Layer caching: Order Dockerfile commands for optimal caching
  4. Minimize layers: Combine RUN commands where appropriate

Organization

  1. Tag images properly: Use semantic versioning
  2. Clean up regularly: Remove unused images and containers
  3. Use docker-compose: Manage multi-container applications
  4. Document configurations: Add comments to Dockerfiles and compose files

Troubleshooting

Container Won't Start

bash
# Check logs for errors
docker logs <container>

# Inspect container configuration
docker inspect <container>

# Try running interactively
docker run -it <image> /bin/bash

Permission Denied Errors

bash
# Linux: Add user to docker group
sudo usermod -aG docker $USER

# Then log out and log back in

Out of Disk Space

bash
# Check disk usage
docker system df

# Clean up
docker system prune -a --volumes

See Also

External Resources

Released under the MIT License.