Created
June 21, 2022 01:53
-
-
Save bytespec/609c00d0c92cfe7322eae43e622fc57a to your computer and use it in GitHub Desktop.
Bash script to install Docker in WSL2 without using Docker Desktop. Tested with Debian 11.3 and Ubuntu 20.04 LTS. Based on information from the Docker installation instructions and https://dev.to/felipecrs/simply-run-docker-on-wsl2-3o8
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 | |
set -e | |
if [ "$USER" = "root" ]; then | |
echo "This script shouldn't run as root (don't use sudo)." | |
exit 1 | |
fi | |
echo "Elevating..." | |
sudo echo " - ok!" | |
# Uninstall older versions | |
echo | |
echo "Uninstalling older versions..." | |
if [ $(dpkg-query -W -f='${Status}' docker | grep -c "ok installed") -ne 0 ]; then | |
echo " - removing docker..." | |
sudo apt-get remove docker -y >/dev/null | |
fi | |
if [ $(dpkg-query -W -f='${Status}' docker-engine | grep -c "ok installed") -ne 0 ]; then | |
echo " - removing docker-engine..." | |
sudo apt-get remove docker-engine -y >/dev/null | |
fi | |
if [ $(dpkg-query -W -f='${Status}' docker.io | grep -c "ok installed") -ne 0 ]; then | |
echo " - removing docker.io..." | |
sudo apt-get remove docker.io -y >/dev/null | |
fi | |
if [ $(dpkg-query -W -f='${Status}' containerd | grep -c "ok installed") -ne 0 ]; then | |
echo " - removing containerd..." | |
sudo apt-get remove containerd -y >/dev/null | |
fi | |
if [ $(dpkg-query -W -f='${Status}' runc | grep -c "ok installed") -ne 0 ]; then | |
echo " - removing runc..." | |
sudo apt-get remove runc -y >/dev/null | |
fi | |
if [ -f "/etc/apt/sources.list.d/docker.list" ]; then | |
echo " - removing apt source file: /etc/apt/sources.list.d/docker.list" | |
sudo rm /etc/apt/sources.list.d/docker.list | |
fi | |
# Set up the docker repo | |
echo | |
echo "Setting up the Docker repository..." | |
echo " - installing prerequisites..." | |
sudo apt-get update >/dev/null | |
echo " - installing ca-certificates..." | |
sudo apt-get install ca-certificates -y >/dev/null | |
echo " - installing curl..." | |
sudo apt-get install curl -y >/dev/null | |
echo " - installing gnupg..." | |
sudo apt-get install gnupg -y >/dev/null | |
echo " - installing lsb-release..." | |
sudo apt-get install lsb-release -y >/dev/null | |
echo " - loading Docker GPG key..." | |
sudo mkdir -p /etc/apt/keyrings | |
if [ -f "/etc/apt/keyrings/docker.gpg" ]; then | |
sudo rm /etc/apt/keyrings/docker.gpg | |
fi | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg | |
echo " - creating Docker apt source file: /etc/apt/sources.list.d/docker.list" | |
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null | |
# Install docker | |
echo | |
echo "Installing Docker..." | |
sudo apt-get update >/dev/null | |
echo " - installing docker-ce..." | |
sudo apt-get install docker-ce -y >/dev/null | |
echo " - installing docker-ce-cli..." | |
sudo apt-get install docker-ce-cli -y >/dev/null | |
echo " - installing containerd.io..." | |
sudo apt-get install containerd.io -y >/dev/null | |
echo " - installing docker-compose-plugin..." | |
sudo apt-get install docker-compose-plugin -y >/dev/null | |
if ! (ls -l /etc/alternatives/iptables | grep -qe "/iptables-legacy$"); then | |
echo " - switching to iptables-legacy..." | |
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy >/dev/null | |
fi | |
echo | |
echo "Finishing up..." | |
# Add user to docker group | |
if ! (groups | grep -q "docker"); then | |
echo " - adding user '$USER' to the docker group..." | |
sudo usermod -aG docker $USER | |
fi | |
# Auto-start docker service | |
if ! (grep -q "# Start Docker service" ~/.profile); then | |
echo " - adding docker auto-start entry to ~/.profile..." | |
(cat << EOF | |
# Start Docker service | |
if service docker status 2>&1 | grep -q "is not running"; then | |
echo "Starting Docker service..." | |
wsl.exe -d "\${WSL_DISTRO_NAME}" -u root -e /usr/sbin/service docker start >/dev/null 2>&1 | |
fi | |
EOF | |
) >> ~/.profile | |
fi | |
echo | |
echo "All set! Open a new session and type 'docker version' to confirm everything is working." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment