Skip to content

Instantly share code, notes, and snippets.

@rbcmgs
Last active May 1, 2024 18:07
Show Gist options
  • Select an option

  • Save rbcmgs/f91047d04bd08e3372082e9097c5374a to your computer and use it in GitHub Desktop.

Select an option

Save rbcmgs/f91047d04bd08e3372082e9097c5374a to your computer and use it in GitHub Desktop.
Bash script to automate the installation of Docker CE on Ubuntu systems. The script ensures that all necessary prerequisites are met and sets up Docker to run as a service, automatically starting at boot. It also adds the current user to the Docker group to allow running Docker commands without `sudo`.
#!/bin/bash
# This script is intended for Ubuntu distributions to automate the Docker installation.
#
# Additional Setup Instructions:
# 1. Save the script as `install-docker-ubuntu.sh`.
# 2. Make the script executable with the command: `chmod +x install-docker-ubuntu.sh`.
# 3. Run the script using `sudo ./install-docker-ubuntu.sh`.
# 4. After the installation, log out and log back into your system to ensure that group changes take effect. This is required to run Docker commands without `sudo`.
# Alert user about root privileges requirement
if [ "$(id -u)" -ne 0 ]; then
echo "Please run this script with sudo or as root."
exit 1
fi
# Update the apt package index to ensure access to the latest package listings
sudo apt update
# Install packages to allow apt to use a repository over HTTPS
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
# Import Docker's official public key to verify packages
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker's stable repository to fetch and install its latest version
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Update the package index again after adding Docker's repository
sudo apt update
# Install Docker Community Edition
sudo apt install -y docker-ce
# Start Docker service immediately after the installation
sudo systemctl start docker
# Set Docker service to start automatically upon system boot
sudo systemctl enable docker
# Add the current user to the Docker group to manage Docker without superuser privileges
sudo usermod -aG docker $USER
# Machine-readable output to verify Docker install status and version
echo "Docker installed successfully."
echo "Docker service status: $(sudo systemctl is-active docker)"
echo "Docker version: $(sudo docker --version)"
# Inform the user that a re-login is necessary to apply user group changes
echo "Please log out and log back in to apply the group changes."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment