DEV Community

kingyou
kingyou

Posted on

Complete Guide: Installing Docker and Docker Compose Step by Step

Docker has revolutionized how we build, ship, and run applications. Whether you're a developer, DevOps engineer, or system administrator, learning Docker is essential. This comprehensive guide will walk you through installing Docker and Docker Compose on various operating systems.

What is Docker?

Docker is a platform that enables developers to package applications into containers—standardized executable components that combine application source code with all the operating system libraries and dependencies required to run that code in any environment.

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes.


Prerequisites

Before installing Docker, ensure your system meets these requirements:

  • Operating System: Ubuntu 20.04+, Debian 11+, CentOS 7+, macOS 10.15+, or Windows 10/11 Pro
  • System Architecture: 64-bit processor
  • Virtualization: Must be enabled in BIOS (for Windows/macOS)
  • Minimum RAM: 4GB recommended
  • Sudo/Administrator privileges: Required for installation

Part 1: Installing Docker on Ubuntu/Debian (One-Click Script)

Instead of manual installation, we'll use Docker's official convenience script for a faster, easier installation process.

Step 1: Download and Run the Official Installation Script

Docker provides an official installation script that automatically handles all dependencies and configurations:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
Enter fullscreen mode Exit fullscreen mode

This script will:

  • Detect your OS version
  • Add Docker's official repository
  • Install all required dependencies
  • Install Docker Engine, CLI, and containerd
  • Start the Docker service

Step 2: Verify Docker Installation

Check that Docker is installed and running:

sudo docker --version
sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

Step 3: Add Your User to the Docker Group (Optional)

To run Docker commands without sudo:

sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

Important: Log out and log back in for this to take effect, or run:

newgrp docker
Enter fullscreen mode Exit fullscreen mode

Step 4: Test Docker Installation

Run the hello-world container to verify everything works:

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

You should see a welcome message confirming Docker is working correctly.

Step 5: Clean Up Installation Script (Optional)

Remove the installation script:

rm get-docker.sh
Enter fullscreen mode Exit fullscreen mode

Note: The convenience script is recommended for development and testing environments. For production systems, consider using your package manager for more control over versions and updates.


Part 2: Installing Docker on CentOS/RHEL (One-Click Script)

Similar to Ubuntu/Debian, we'll use Docker's official convenience script:

Step 1: Download and Run the Installation Script

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
Enter fullscreen mode Exit fullscreen mode

Step 2: Start and Enable Docker

sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Step 3: Add User to Docker Group

sudo usermod -aG docker $USER
newgrp docker
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Installation

sudo docker --version
docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Step 5: Clean Up

rm get-docker.sh
Enter fullscreen mode Exit fullscreen mode

Part 3: Installing Docker on macOS

Step 1: Download Docker Desktop

  1. Visit the Docker Desktop for Mac download page
  2. Download the appropriate version for your Mac (Intel or Apple Silicon)

Step 2: Install Docker Desktop

  1. Open the downloaded .dmg file
  2. Drag the Docker icon to your Applications folder
  3. Double-click Docker.app in Applications to start Docker

Step 3: Complete Setup

  1. Accept the terms and conditions
  2. Enter your system password when prompted (Docker needs privileged access)
  3. Wait for Docker to start (the whale icon in the menu bar will stop animating)

Step 4: Verify Installation

Open Terminal and run:

docker --version
docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Part 4: Installing Docker on Windows

Step 1: System Requirements

Ensure you have:

  • Windows 10/11 64-bit: Pro, Enterprise, or Education
  • Hardware virtualization enabled in BIOS
  • WSL 2 feature enabled

Step 2: Enable WSL 2

Open PowerShell as Administrator and run:

wsl --install
Enter fullscreen mode Exit fullscreen mode

Restart your computer when prompted.

Step 3: Download Docker Desktop

  1. Visit Docker Desktop for Windows
  2. Download the installer

Step 4: Install Docker Desktop

  1. Run the installer
  2. Follow the installation wizard
  3. Ensure "Use WSL 2 instead of Hyper-V" is selected
  4. Restart your computer when installation completes

Step 5: Start Docker Desktop

  1. Launch Docker Desktop from the Start menu
  2. Accept the service agreement
  3. Wait for Docker to start

Step 6: Verify Installation

Open PowerShell or Command Prompt:

docker --version
docker run hello-world
Enter fullscreen mode Exit fullscreen mode

Part 5: Installing Docker Compose

Docker Compose comes pre-installed with Docker Desktop on macOS and Windows. For Linux users, follow these steps:

Method 1: Install Docker Compose Plugin (Recommended)

The easiest way on modern Docker installations:

sudo apt update
sudo apt install docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

Verify installation:

docker compose version
Enter fullscreen mode Exit fullscreen mode

Method 2: Install Standalone Docker Compose

For systems that need the standalone version:

Step 1: Download Docker Compose Binary

sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Step 2: Apply Executable Permissions

sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Symbolic Link (Optional)

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Installation

docker-compose --version
Enter fullscreen mode Exit fullscreen mode

Testing Your Docker Setup

Let's create a simple Docker Compose application to test everything:

Step 1: Create Project Directory

mkdir ~/docker-test && cd ~/docker-test
Enter fullscreen mode Exit fullscreen mode

Step 2: Create docker-compose.yml

Create a file named docker-compose.yml:

version: '3.8'

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

Step 3: Create HTML Directory and File

mkdir html
echo "<h1>Docker is working!</h1>" > html/index.html
Enter fullscreen mode Exit fullscreen mode

Step 4: Start the Application

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

Step 5: Test in Browser

Open your browser and navigate to http://localhost:8080

You should see "Docker is working!"

Step 6: Stop the Application

docker compose down
Enter fullscreen mode Exit fullscreen mode

Useful Docker Commands

Here are essential Docker commands you'll use frequently:

Container Management

docker ps                    # List running containers
docker ps -a                 # List all containers
docker stop <container>      # Stop a container
docker start <container>     # Start a container
docker restart <container>   # Restart a container
docker rm <container>        # Remove a container
docker logs <container>      # View container logs
Enter fullscreen mode Exit fullscreen mode

Image Management

docker images                # List images
docker pull <image>          # Download an image
docker rmi <image>           # Remove an image
docker build -t <name> .     # Build image from Dockerfile
Enter fullscreen mode Exit fullscreen mode

System Commands

docker system df             # Show Docker disk usage
docker system prune          # Remove unused data
docker volume ls             # List volumes
docker network ls            # List networks
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

Issue 1: Permission Denied

Problem: Getting "permission denied" errors

Solution: Add your user to the docker group:

sudo usermod -aG docker $USER
newgrp docker
Enter fullscreen mode Exit fullscreen mode

Issue 2: Docker Daemon Not Running

Problem: "Cannot connect to Docker daemon"

Solution:

sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Issue 3: Port Already in Use

Problem: "Port is already allocated"

Solution: Change the port in your docker-compose.yml or stop the conflicting service.

Issue 4: Disk Space Issues

Problem: Running out of disk space

Solution: Clean up unused Docker resources:

docker system prune -a --volumes
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Keep Docker Updated: Regularly update Docker to get security patches and new features

  2. Use Official Images: When possible, use official Docker images from Docker Hub

  3. Don't Run as Root: Use the docker group instead of running docker commands with sudo

  4. Clean Up Regularly: Remove unused containers, images, and volumes to save disk space

  5. Use .dockerignore: Create .dockerignore files to exclude unnecessary files from your Docker builds

  6. Monitor Resources: Keep an eye on container resource usage with docker stats

  7. Use Docker Compose for Multi-Container Apps: It's much easier than managing multiple containers manually


Next Steps

Now that you have Docker and Docker Compose installed, here are some next steps:

  • ✅ Learn about Dockerfiles and how to create custom images
  • ✅ Explore Docker Hub for ready-to-use images
  • ✅ Practice with docker-compose for multi-container applications
  • ✅ Learn about Docker volumes for persistent data
  • ✅ Study Docker networking concepts
  • ✅ Explore container orchestration with Kubernetes

Conclusion

Congratulations! 🎉 You've successfully installed Docker and Docker Compose on your system using the simplified one-click installation method. Docker is a powerful tool that will streamline your development workflow and make deploying applications much easier.

Remember:

  • Docker packages applications into portable containers
  • Docker Compose helps manage multi-container applications
  • The one-click script simplifies installation for development environments
  • Practice makes perfect—start containerizing your projects!

Have questions or run into issues? Drop them in the comments below!


Happy Dockerizing! 🐳

Top comments (0)