Docker is an opensource platform that is used to develop, manage and run containerized applications. Docker can be used in both Windows and Linux. However, running docker in windows is resource intensive (RAM and CPU utilization) as it uses Hyper-V for virtualization. On the other hand, installing docker on WSL is not resource intensive
This write-up will walk through the steps necessary for installing docker and docker compose on WSL2. This write-up assumes reader is familiar with basic Linux Commands and is doing a clean installation
- Start with enabling WSL on Windows. Don’t know how to enable WSL? Click Here to get assistance
- Start Linux and log-in as root user using “sudo su”
- Install pre-required packages
sudo apt update
sudo apt install --no-install-recommends apt-transport-https ca-certificates curl gnupg2
- Configure package repository
source /etc/os-release
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
sudo apt update
- Install Docker
sudo apt install docker-ce docker-ce-cli containerd.io
- Add user to group
sudo usermod -aG docker $USER
- Configure dockerd
DOCKER_DIR=/mnt/wsl/shared-docker
mkdir -pm o=,ug=rwx "$DOCKER_DIR"
sudo chgrp docker "$DOCKER_DIR"
sudo mkdir /etc/docker
sudo vi /etc/docker/daemon.json
{
"hosts": ["unix:///mnt/wsl/shared-docker/docker.sock"]
}
- Run Docker
sudo dockerd
If the command ends with “API listen on /mnt/wsl/shared-docker/docker.sock”, things are working. However, if docker fails to start with an error something like “iptables failed”
Run the following commands for Ubuntu 22.0
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
Run docker again.
Perform an additional test by opening a new terminal and running the following command
docker -H unix:///mnt/wsl/shared-docker/docker.sock run --rm hello-world
Run Docker automatically on WSL start
To always run dockerd automatically on WSL start, add the following to .bashrc
.bashrc file can be found in users home directory. Use following command to edit the file
sudo vi ~/.bashrc
DOCKER_DISTRO="Ubuntu-22.04"
DOCKER_DIR=/mnt/wsl/shared-docker
DOCKER_SOCK="$DOCKER_DIR/docker.sock"
export DOCKER_HOST="unix://$DOCKER_SOCK"
if [ ! -S "$DOCKER_SOCK" ]; then
mkdir -pm o=,ug=rwx "$DOCKER_DIR"
sudo chgrp docker "$DOCKER_DIR"
/mnt/c/Windows/System32/wsl.exe -d $DOCKER_DISTRO sh -c "nohup sudo -b dockerd < /dev/null > $DOCKER_DIR/dockerd.log 2>&1"
fi
Install docker compose
- Run below command to install latest version of docker compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- Give executable permissions
sudo chmod +x /usr/local/bin/docker-compose
- Test Installation
docker-compose --version