
How to Run Docker Like a Pro
Learn the simplest way to set up and run Docker containers on Mac and Windows, even if you're just starting out.
Getting Started with Docker
Docker has become an essential tool for developers, but it can seem intimidating at first. In this comprehensive guide, I'll walk you through everything you need to know to start using Docker like a professional on both Mac and Windows.
What is Docker?
Docker is a containerization platform that allows you to package applications and their dependencies into lightweight, portable containers. Think of it as a way to create consistent environments that work the same way across different machines.
Installation and Setup
The first step is getting Docker installed on your system. Docker Desktop provides a user-friendly interface for both Windows and Mac users, making installation straightforward.
Step-by-Step Guide
Install Docker Desktop on Mac
# Method 1: Download from Docker Hub
# Go to https://hub.docker.com/editions/community/docker-ce-desktop-mac
# Download Docker Desktop for Mac and install the .dmg file
# Method 2: Using Homebrew (recommended for developers)
brew install --cask docker
# Start Docker Desktop from Applications folder
# Or use Spotlight: Cmd + Space, type "Docker"
# Verify installation
docker --version
docker-compose --version
Docker Desktop for Mac includes Docker Engine, Docker CLI, Docker Compose, and Kubernetes. The Homebrew method is preferred by developers as it's easier to manage updates. Make sure to start Docker Desktop after installation.
Install Docker Desktop on Windows
# Method 1: Download from Docker Hub
# Go to https://hub.docker.com/editions/community/docker-ce-desktop-windows
# Download Docker Desktop for Windows and run the installer
# Method 2: Using Chocolatey
choco install docker-desktop
# Method 3: Using Winget (Windows Package Manager)
winget install Docker.DockerDesktop
# After installation, restart your computer
# Start Docker Desktop from Start Menu
# Verify installation in PowerShell or Command Prompt
docker --version
docker-compose --version
Docker Desktop for Windows requires Windows 10/11 with WSL 2. The installer will enable WSL 2 if needed. After installation, you can use Docker commands in PowerShell, Command Prompt, or WSL terminals.
Your First Docker Container
# Test Docker installation with hello-world
docker run hello-world
# Run an interactive Ubuntu container
docker run -it ubuntu:latest bash
# Inside the container, you can run Ubuntu commands
# apt update && apt install curl
# curl --version
# exit
# On Windows, you can also run Windows containers
# docker run -it mcr.microsoft.com/windows/nanoserver:ltsc2022 cmd
The hello-world container is a simple test to verify Docker is working correctly. The Ubuntu container demonstrates how you can run a complete Linux environment. Windows users can run both Linux and Windows containers (switch in Docker Desktop settings).
Running Web Applications
# Run Nginx web server in detached mode
docker run -d -p 8080:80 --name my-nginx nginx:latest
# Check if container is running
docker ps
# View container logs
docker logs my-nginx
# Open in browser (Mac)
open http://localhost:8080
# Open in browser (Windows)
start http://localhost:8080
# Stop the container
docker stop my-nginx
This runs an Nginx web server in the background (-d flag) and maps port 8080 on your host to port 80 in the container. The open/start commands will launch your default browser to view the running web server.
Managing Docker Images
# Search for images on Docker Hub
docker search node
# Pull a specific Node.js version
docker pull node:18-alpine
# List all downloaded images
docker images
# Remove an image (must stop containers first)
docker rmi node:18-alpine
# On Mac/Windows, you can also use Docker Desktop GUI
# Go to Images tab to manage images visually
Docker images are templates for containers. Alpine versions are smaller and more secure. Docker Desktop provides a graphical interface to manage images, which is helpful for beginners. Always use specific version tags in production.
Container Lifecycle Management
# List all containers (running and stopped)
docker ps -a
# Start a stopped container
docker start container_name
# Restart a running container
docker restart container_name
# Execute commands in running container
docker exec -it container_name bash
# Copy files between host and container (Mac/Windows)
docker cp /Users/username/file.txt container_name:/app/file.txt
docker cp "C:\Users\username\file.txt" container_name:/app/file.txt
# View container details in Docker Desktop GUI
These commands help you manage container lifecycles. Note the different path formats for Mac (/Users/) and Windows (C:\Users\). Docker Desktop provides a GUI alternative for managing containers, viewing logs, and accessing terminals.
Docker Compose for Multi-Container Apps
# Create docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
database:
image: postgres:13
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
# Note: On Windows, make sure to use forward slashes in volume paths
# or use named volumes for better cross-platform compatibility
Docker Compose allows you to define multi-container applications in a single file. This works identically on Mac and Windows. Use named volumes for better cross-platform compatibility, as path formats differ between operating systems.
Docker Compose Commands
# Start all services defined in docker-compose.yml
docker-compose up -d
# Alternative: using newer docker compose (without hyphen)
docker compose up -d
# View logs from all services
docker-compose logs
# Stop all services
docker-compose down
# Rebuild and start services
docker-compose up --build
# Scale a service to multiple instances
docker-compose up --scale web=3
Docker Compose commands work the same on Mac and Windows. Newer Docker versions include 'docker compose' (without hyphen) as a subcommand. Both versions work, but 'docker compose' is the newer standard.
Cleanup and Maintenance
# Remove all stopped containers
docker container prune
# Remove unused images
docker image prune
# Remove unused volumes
docker volume prune
# Remove everything unused (be careful!)
docker system prune -a
# Check Docker disk usage
docker system df
# On Mac: Check Docker Desktop resource usage in Preferences
# On Windows: Check Docker Desktop resource usage in Settings
# Adjust memory and CPU limits as needed
Regular cleanup is essential as Docker can consume significant disk space on both Mac and Windows. Docker Desktop allows you to configure resource limits (CPU, memory, disk) in the settings to prevent system slowdowns.
Platform-Specific Tips
# Mac-specific tips
# Use Docker Desktop's built-in Kubernetes if needed
# Enable "Use gRPC FUSE for file sharing" for better performance
# Consider using Rosetta 2 emulation on Apple Silicon Macs
# Windows-specific tips
# Ensure WSL 2 is enabled for better performance
# Use WSL 2 backend instead of Hyper-V when possible
# Consider using Windows Terminal for better Docker CLI experience
# Check platform architecture
docker version --format '{{.Server.Arch}}'
# Run multi-architecture builds
docker buildx create --use
docker buildx build --platform linux/amd64,linux/arm64 -t myapp .
Platform-specific optimizations can significantly improve Docker performance. Mac users with Apple Silicon should be aware of architecture differences. Windows users benefit from WSL 2 integration for better Linux container performance.
Pro Tips & Best Practices
Use docker system prune
regularly to clean up unused resources
Always use specific image tags instead of latest
in production
Use .dockerignore
files to exclude unnecessary files
Multi-stage builds can significantly reduce image sizes