Skip to content

Instantly share code, notes, and snippets.

@aleung
Last active September 21, 2022 03:24
Show Gist options
  • Save aleung/35d68cb9e150c20de4494f07cad7f029 to your computer and use it in GitHub Desktop.
Save aleung/35d68cb9e150c20de4494f07cad7f029 to your computer and use it in GitHub Desktop.
Install Docker CE on WSL2
#!/bin/bash
set -eu
echo Install Docker on WSL2 Ubuntu
echo =============================
echo It will prompt to input password for sudo.
echo
# Setup repo
source /etc/os-release
if [[ ${ID} != "ubuntu" ]]; then
echo Current system is not Ubuntu
exit 1
fi
curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo apt-key add -
echo "deb [arch=amd64] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/docker.list
# == Install docker
sudo apt update
sudo apt -y upgrade
sudo apt -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# == Setup permission
sudo usermod -aG docker $USER
sudo groupmod -g 36257 docker
sudo chmod u+s /usr/bin/dockerd
if ! sudo grep -q dockerd /etc/sudoers; then
cat << 'EOF' | sudo EDITOR='tee -a' visudo
# Run dockerd without password
ALL ALL=NOPASSWD: /usr/bin/dockerd
EOF
fi
# == Share docker socket between WSL distributions
# == (Remove this section if no need to share docker socket)
DOCKER_DIR=/mnt/wsl/shared-docker
mkdir -pm o=,ug=rwx "$DOCKER_DIR"
sudo chgrp docker "$DOCKER_DIR"
sudo mkdir -p /etc/docker/
cat << EOF | sudo tee /etc/docker/daemon.json > /dev/null
{
"hosts": ["unix:///mnt/wsl/shared-docker/docker.sock"]
}
EOF
# == Setup environment in bashrc
# == (Remove this section if no need to share docker socket)
if ! grep -q DOCKER_HOST ~/.bashrc; then
cat << 'EOF' | tee -a ~/.bashrc > /dev/null
DOCKER_DIR=/mnt/wsl/shared-docker
DOCKER_SOCK="$DOCKER_DIR/docker.sock"
export DOCKER_HOST="unix://$DOCKER_SOCK"
EOF
fi
# == Done
cat << 'EOF'
Install finished.
To start docker service, run:
sudo service docker start
Then try docker with:
docker run hello-world
To debug docker daemon running issue, check log file: /var/log/docker.log
To run docker-compose:
docker compose help
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment