Skip to content

Instantly share code, notes, and snippets.

@GodSpoon
Last active July 16, 2025 21:44
Show Gist options
  • Select an option

  • Save GodSpoon/5d51756de0ac9ef4453b351e02cfb364 to your computer and use it in GitHub Desktop.

Select an option

Save GodSpoon/5d51756de0ac9ef4453b351e02cfb364 to your computer and use it in GitHub Desktop.
Automated Docker and Portainer BE Setup for Ubuntu 22.04 (Non-Root)

Ubuntu 22.04 Docker & Portainer Business Setup (Non-Root User)

This script automates the installation of Docker Engine and Portainer Business Edition on Ubuntu 22.04. It also configures Docker so that a non-root user can run Docker commands without sudo.

Features

  • Installs Docker Engine and required dependencies
  • Adds the current user to the docker group for non-root Docker access
  • Fixes Docker config directory permissions
  • Installs and runs Portainer Business Edition with persistent data volume
  • Configures Docker daemon for production logging and storage
  • (Optional) Configures UFW firewall rules for Portainer ports

Quickstart: Download and Run in One Command

Run this in your Ubuntu 22.04 terminal:

curl -fsSL https://gist.githubusercontent.com/GodSpoon/5d51756de0ac9ef4453b351e02cfb364/raw/6ef283e84f7102c31484af212319b82e072de473/ubuntu22-docker-portainer-nonroot.sh | bash

Post-Installation

  • Log out and log back in (or run newgrp docker) to apply Docker group membership

  • Test Docker without sudo:

    docker run hello-world
  • Access Portainer UI at:

    • http://<your-server-ip>:9000 (HTTP)
    • https://<your-server-ip>:9443 (HTTPS)
#!/bin/bash
# Ubuntu 22.04 Azure Docker and Portainer Business Setup Script
# Installs Docker Engine and Portainer Business Edition
# Enables non-root Docker command access for the current user
set -e
echo "Starting setup for Docker and Portainer Business Edition..."
# Update system packages
sudo apt-get update
sudo apt-get upgrade -y
# Install necessary dependencies
sudo apt-get install -y \
ca-certificates \
curl \
software-properties-common \
apt-transport-https \
gnupg \
lsb-release
# Remove potential conflicting packages
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
sudo apt-get remove -y $pkg 2>/dev/null || true
done
# Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add Docker's official repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine and plugins
sudo apt-get update
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
# Enable non-root Docker usage for the current user
sudo usermod -aG docker $USER
# Ensure user owns Docker config (fixes permission errors if .docker was created with sudo)
sudo mkdir -p /home/"$USER"/.docker
sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
sudo chmod g+rwx "$HOME/.docker" -R
# Configure Docker daemon for production logging and storage
sudo mkdir -p /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
},
"storage-driver": "overlay2"
}
EOF
# Restart Docker to apply configuration
sudo systemctl restart docker
# Create Portainer data volume
sudo docker volume create portainer_data
# Deploy Portainer Business Edition (latest, with commonly used ports)
sudo docker run -d \
-p 8000:8000 \
-p 9000:9000 \
-p 9443:9443 \
--name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data \
portainer/portainer-ee:latest
# Configure firewall (if using ufw locally for demos; Azure NSG rules must be set separately)
if command -v ufw &> /dev/null; then
sudo ufw allow 9000/tcp
sudo ufw allow 9443/tcp
sudo ufw allow 8000/tcp
fi
echo "=============================================="
echo "Installation completed!"
echo "IMPORTANT: You must log out and log back in, or run 'newgrp docker', to use Docker without sudo."
echo "Portainer Business Edition is running at:"
echo " - HTTP: http://$(curl -s ifconfig.me):9000"
echo " - HTTPS: https://$(curl -s ifconfig.me):9443"
echo "=============================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment