Install Docker and Portainer on Debian

This tutorial provides an automated script to install Docker and Portainer on a Debian-based system. The script handles all dependencies, repository setup, and container configuration.

Prerequisites

  • A Debian-based system (tested on Debian 11/12)
  • sudo privileges

Step 1: Create the Installation Script

Run this command to create the script file:

sudo nano install_docker_portainer.sh

Paste this script into the editor:

#!/bin/bash

# Update package lists
sudo apt update

# Install prerequisites
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release

# Add Docker's GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update again for Docker packages
sudo apt update

# Install Docker components
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Install the latest version of Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -oP '(?<=tag_name\": \")[0-9a-zA-Z.-]+')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# Make Docker Compose executable
sudo chmod +x /usr/local/bin/docker-compose

# Create the docker group and add the current user
sudo groupadd docker
sudo usermod -aG docker $USER

# Set up Portainer
sudo docker pull portainer/portainer-ce:latest
sudo docker volume create portainer_data
sudo docker run -d -p 9000:9000 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest

# Final message
echo "Installation complete."
echo "Access Portainer: http://$(hostname -I | awk '{print $1}'):9000"
echo "Log out and back in to apply Docker permissions."

Save the file:
Press Ctrl+OEnterCtrl+X.


Step 2: Make the Script Executable

This command allows the script to be run.

sudo chmod +x install_docker_portainer.sh

Step 3: Run the Script

This will execute the installation process.

sudo ./install_docker_portainer.sh

Step 4: Finalize Setup

  1. Log out and back in to apply the new Docker group permissions.
  2. Access Portainer in your browser at: http://<your-server-ip>:9000

You will be prompted to create an admin user on your first visit.


Verification

You can verify that both services are running correctly.

# Check Docker status
sudo systemctl status docker

# Verify Portainer container is running
sudo docker ps | grep portainer

A Note on Security

Adding a user to the docker group provides privileges equivalent to the root user. Be aware of this when running commands.