Skip to content

Instantly share code, notes, and snippets.

@madkoding
Last active December 10, 2023 20:37
Show Gist options
  • Save madkoding/98d657fd43d65cc7172e0ace55ededfb to your computer and use it in GitHub Desktop.
Save madkoding/98d657fd43d65cc7172e0ace55ededfb to your computer and use it in GitHub Desktop.
Install Docker and Docker Compose in WSL2
#!/bin/bash
# Check if running in WSL
if grep -qEi "(Microsoft|WSL)" /proc/version &> /dev/null ; then
echo "WSL detected, proceeding with Docker installation..."
else
echo "This script is intended to be run in WSL. Exiting."
exit 1
fi
# Update and install dependencies
echo "Updating packages and installing dependencies..."
sudo apt-get update -y
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common || { echo "Failed to install dependencies"; exit 1; }
# Check if Docker is installed and remove it
if dpkg -l | grep -qw docker; then
echo "Removing old Docker versions..."
sudo apt-get remove -y docker docker-engine docker.io containerd runc || { echo "Failed to remove existing Docker installations"; exit 1; }
fi
# Add Docker's official GPG key
echo "Adding Docker's GPG key..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - || { echo "Failed to add Docker's GPG key"; exit 1; }
# Verify key with fingerprint
sudo apt-key fingerprint 0EBFCD88
# Add Docker repository
echo "Adding Docker repository..."
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" || { echo "Failed to add Docker repository"; exit 1; }
# Install Docker CE
echo "Installing Docker CE..."
sudo apt-get update -y
sudo apt-get install -y docker-ce || { echo "Failed to install Docker CE"; exit 1; }
# Add current user to the Docker group (optional)
echo "Adding current user to the Docker group..."
sudo usermod -aG docker $(whoami) || { echo "Failed to add user to Docker group"; exit 1; }
# Install Python 3 and PIP
echo "Installing Python 3 and PIP..."
sudo apt-get install -y python3 python3-pip || { echo "Failed to install Python 3 and PIP"; exit 1; }
# Install Docker Compose
echo "Installing Docker Compose..."
pip3 install --user docker-compose || { echo "Failed to install Docker Compose"; exit 1; }
echo "Docker installation and setup completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment