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 #!/bin/bash
# Update and upgrade the system # Function to print a header with a description
sudo apt update print_header() {
sudo apt upgrade -y echo "------------------------------------------------------------"
echo $1
}
# Add a new user to run Docker # Check if pv is installed, and install it if necessary
echo "Adding a new user to run Docker (username 'docker' is recommended)..." check_and_install_pv() {
read -p "Enter username for the new Docker user: " USERNAME if ! command -v pv &> /dev/null; then
sudo useradd -m -s /bin/bash $USERNAME print_header "Installing Pipe Viewer (pv)"
sudo usermod -aG docker $USERNAME sudo apt-get update -y
echo "New user $USERNAME added to the docker group." sudo apt-get install -y pv
else
print_header "Pipe Viewer (pv) is already installed"
fi
}
# Install required dependencies # Function to run a command with progress bar if `pv` is available
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common 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 # Install required packages for Docker
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - 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 # Add Dockers official GPG key with progress bar
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" 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 # Set up the stable repository for Docker
sudo apt update 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 # Update the package list again with progress bar
sudo apt install -y docker-ce run_with_progress "sudo apt-get update"
# Install Docker Compose # Install Docker CE (Community Edition)
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 print_header "Installing Docker CE..."
sudo chmod +x /usr/local/bin/docker-compose run_with_progress "sudo apt-get install -y docker-ce docker-ce-cli containerd.io"
# Install Portainer # Verify that Docker is installed correctly
sudo docker volume create portainer_data print_header "Starting and enabling Docker service..."
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 sudo systemctl start docker
sudo systemctl enable docker
# Check if Portainer container is running # Download Docker Compose with progress bar
if sudo docker ps | grep -q portainer; then check_and_install_pv
echo "Portainer container is running successfully." print_header "Downloading and installing Docker Compose..."
else 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)
echo "Failed to start Portainer container." run_with_progress "pv -p -t -e -r /tmp/docker-compose.tar.gz | tar -xzf - -C /usr/local/bin"
fi
# 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"