Skip to content

Install Docker and Docker Compose on Debian Server Using a Script

Prerequisites

  • A Debian server
  • A user with sudo privileges

Installation Script

Automated Setup

The following script automates the installation of Docker Engine, the Docker Compose plugin, and sets up your user group permissions.

View the bash script
#!/bin/bash

# Exit on any error
set -e

# Update package list
sudo apt-get update

# Install required packages
sudo apt-get install -y \
    ca-certificates \
    curl \
    gnupg

# Add Docker’s official GPG key for Debian
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg

# Set up the official Debian Docker repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update the package list again
sudo apt-get update

# Install Docker Engine and the Docker Compose plugin
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

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

# Add current user to the Docker group (requires logout and login or `newgrp docker` to take effect)
sudo usermod -aG docker $USER

# Verify Docker installation
sudo docker run hello-world

# Verify Docker Compose (modern plugin style)
docker compose version

echo "========================================================================="
echo "Docker and Docker Compose installation is complete."
echo "Please log out and log back in to run docker commands without sudo."
echo "========================================================================="

Script Walkthrough

  • Exit on any error: set -e ensures that the script will exit immediately if any command fails.
  • Update package list: sudo apt-get update updates the list of available packages.
  • Install required packages: Installs ca-certificates curl gnupg.
  • Add Docker’s official GPG key: Ensures the authenticity of the packages.
  • Set up the official Debian Docker repository: Adds the official Docker repo to package sources.
  • Install Docker Engine: Installs docker-ce and docker-compose-plugin.
  • Start and enable Docker service: Starts and enables it at boot.
  • Add user to group: Adds user to the docker group to prevent needing sudo for every command.

How to run the script

  1. Copy the script to the server:
    scp /path/to/script.sh user@server:/path/to/script.sh
    
  2. Make it executable:
    chmod +x /path/to/script.sh
    
  3. Run the script:
    /path/to/script.sh
    

Troubleshooting: Permission Denied on docker.sock

If you receive the following error:

debian@vps:~$ docker ps
permission denied while trying to connect to the docker API at unix:///var/run/docker.sock

Why does this happen?

This happens because your user group changes (usermod -aG docker $USER) have not been applied to your active SSH session yet.

Resolution Methods

You can apply the changes using any of the following methods:

Method 1: Apply to current session immediately

newgrp docker

Method 2: Log out and log back in Simply close your SSH connection and reconnect.

Method 3: Force restart the Docker daemon If you still get permission errors, ensure the Docker socket has correct permissions:

sudo systemctl restart docker


Next Steps

Once Docker is installed, you can proceed to host-multiple-docker-projects-with-shared-nginx-proxy to host multiple applications efficiently.