Rewrite of Docker isntall script

This commit is contained in:
Oliver Gwyther 2025-01-16 00:27:16 +00:00
parent b95d0cffd6
commit e2c81a7359

View File

@ -1,42 +1,88 @@
#!/bin/bash
# Update and upgrade the system
sudo apt update
sudo apt upgrade -y
# Function to print a header with a description
print_header() {
echo "------------------------------------------------------------"
echo $1
}
# Add a new user to run Docker
echo "Adding a new user to run Docker (username 'docker' is recommended)..."
read -p "Enter username for the new Docker user: " USERNAME
sudo useradd -m -s /bin/bash $USERNAME
sudo usermod -aG docker $USERNAME
echo "New user $USERNAME added to the docker group."
# Check if pv is installed, and install it if necessary
check_and_install_pv() {
if ! command -v pv &> /dev/null; then
print_header "Installing Pipe Viewer (pv)"
sudo apt-get update -y
sudo apt-get install -y pv
else
print_header "Pipe Viewer (pv) is already installed"
fi
}
# Install required dependencies
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# Function to run a command with progress bar if `pv` is available
run_with_progress() {
local cmd="$1"
if command -v pv &> /dev/null; then
echo "$cmd | pv -l -p -t -e -r"
else
echo "Running: $cmd"
eval "$cmd"
fi
}
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
# Install required packages for Docker
print_header "Installing required packages..."
sudo apt-get update -y
sudo apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
# Add Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
# Add Dockers official GPG key with progress bar
check_and_install_pv
print_header "Adding Docker's official GPG key..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /usr/share/keyrings/docker-archive-keyring.gpg
# Update package index again after adding Docker repository
sudo apt update
# Set up the stable repository for Docker
print_header "Setting up Docker repository..."
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker
sudo apt install -y docker-ce
# Update the package list again with progress bar
run_with_progress "sudo apt-get update"
# Install 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": "\K(.*)(?=")')/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Install Docker CE (Community Edition)
print_header "Installing Docker CE..."
run_with_progress "sudo apt-get install -y docker-ce docker-ce-cli containerd.io"
# Install Portainer
sudo docker volume create portainer_data
sudo docker run -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data --restart always --name=portainer portainer/portainer-ce
# Verify that Docker is installed correctly
print_header "Starting and enabling Docker service..."
sudo systemctl start docker
sudo systemctl enable docker
# Check if Portainer container is running
if sudo docker ps | grep -q portainer; then
echo "Portainer container is running successfully."
else
echo "Failed to start Portainer container."
fi
# Download Docker Compose with progress bar
check_and_install_pv
print_header "Downloading and installing Docker Compose..."
sudo curl -L --output /tmp/docker-compose.tar.gz https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)
run_with_progress "pv -p -t -e -r /tmp/docker-compose.tar.gz | tar -xzf - -C /usr/local/bin"
# Verify that Docker Compose is installed correctly
print_header "Checking installation of Docker Compose..."
sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
# Install Portainer and start it
print_header "Installing and starting Portainer..."
run_with_progress "curl -fL https://portainer.io/portainer.tar.gz | sudo tar xz -C /"
sudo mv /portainer/portainer /usr/local/bin/portainer
rm -rf /portainer
sudo chmod +x /usr/local/bin/portainer
# Run Portainer in detached mode
print_header "Starting Portainer..."
run_with_progress "sudo /usr/local/bin/portainer --log-level=info --tls"
# Print a message indicating that Portainer is running and accessible on the default URL
echo ""
echo "Portainer has been installed and started. You can access it at:"
echo "https://<your-server-ip>:9000"