Last active
June 25, 2024 08:44
-
-
Save noor-jafri/44f74fe9ecb043aa3d3f5f6119276289 to your computer and use it in GitHub Desktop.
Docker - DockerCompose - Let's Encrypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to display a message with a timestamp | |
function echo_with_timestamp() { | |
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | |
} | |
echo_with_timestamp "Starting pre-installation script for Docker and (optionally) Certbot..." | |
# Update and upgrade the package lists | |
echo_with_timestamp "Updating package lists..." | |
sudo apt update | |
echo_with_timestamp "Upgrading installed packages..." | |
sudo apt upgrade -y | |
# Install prerequisites | |
echo_with_timestamp "Installing prerequisites..." | |
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common | |
# Add Docker's official GPG key | |
echo_with_timestamp "Adding Docker's GPG key..." | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg | |
# Set up the Docker stable repository | |
echo_with_timestamp "Setting up Docker repository..." | |
echo "deb [arch=$(dpkg --print-architecture) 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 | |
# Update the package database with Docker packages from the newly added repo | |
echo_with_timestamp "Updating package lists again..." | |
sudo apt update | |
# Ensure that Docker will be installed from the Docker repository | |
echo_with_timestamp "Checking Docker package..." | |
apt-cache policy docker-ce | |
# Install Docker | |
echo_with_timestamp "Installing Docker..." | |
yes | sudo apt install -y docker-ce | |
# Add current user to the Docker group | |
sudo groupadd docker | |
echo_with_timestamp "Adding user to Docker group..." | |
sudo usermod -aG docker $USER | |
# Verify Docker installation | |
echo_with_timestamp "Verifying Docker installation..." | |
sudo systemctl status docker | |
# Install Docker Compose | |
echo_with_timestamp "Installing Docker Compose..." | |
mkdir -p ~/.docker/cli-plugins/ | |
curl -SL https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose | |
chmod +x ~/.docker/cli-plugins/docker-compose | |
# Ask if Certbot needs to be installed | |
read -p "Do you need to install Certbot and obtain an SSL certificate? (yes/no): " install_certbot | |
if [ "$install_certbot" == "yes" ]; then | |
# Install Certbot | |
echo_with_timestamp "Installing Certbot..." | |
sudo snap install --classic certbot | |
# Create symlink for Certbot | |
echo_with_timestamp "Creating symlink for Certbot..." | |
sudo ln -s /snap/bin/certbot /usr/bin/certbot | |
# Prompt for user email and domain | |
read -p "Please enter your email address for Certbot: " email | |
read -p "Please enter your domain for SSL certificate: " domain | |
# Obtain SSL certificate using Certbot | |
echo_with_timestamp "Obtaining SSL certificate..." | |
sudo certbot certonly --standalone --non-interactive --agree-tos -m "$email" -d "$domain" | |
else | |
echo_with_timestamp "Skipping Certbot installation." | |
fi | |
echo_with_timestamp "Pre-installation script completed successfully!" | |
echo_with_timestamp "Please log out and log back in for the Docker group changes to take effect." | |
# Ask if the user wants to restart the system | |
read -p "Do you want to restart the system now? (yes/no): " restart_now | |
if [ "$restart_now" == "yes" ]; then | |
echo_with_timestamp "Restarting the system..." | |
sudo reboot | |
else | |
echo_with_timestamp "System restart skipped. Please restart the system manually if needed." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment