Setting up a docker in Windows Subsystem for Linux (2) really gave me a pain. Therefore once I was able to successfully set it up. I decided to keep it as a note for the future reference. However if things are needed for setting up WSL then notes written on this link can be followed.
The basics of docker are not discussed here and its not also the objective. However, if anyone is willing have some overview please check this 7-minutes read in the official docker page.
The following step by step instruction can be followed for installing docker inside the Ubuntu 18.04 setup on WSL 2 on windows 10+ machine. Note that this wont install the desktop docker version.
Step-1: Clean the existing docker setups and prepare with dependencies
# clean previous setups
sudo apt remove docker docker-engine docker.io containerd runc
# prepare dependencies
Step-2: Package configuration
# source OS-variables
source /etc/os-release
# add apt-key
# note that {ID} will be source from env. variables and needs no change
curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo apt-key add -
# add repo.
echo "deb [arch=amd64] https://download.docker.com/linux/${ID} ${VERSION_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/docker.list
# update repo.
sudo apt update
Step-3: Install docker and setup docker group
# install docker
sudo apt install docker-ce docker-ce-cli containerd.io
# setup docker group
sudo usermod -aG docker $USER
Step-4: Launch
sudo dockerd
One issue with this setup is that, we have to manually run the dockerd daemon for initializing the docker api which might be a problem if you are willing to run docker based program from the startup.
To solve this issue we can actually add the following script in the .bashrc file that will activate dockerd as soon as WSL is activated.
# open .bashrc
nano ~/.bashrc
# copy the following script at the end of file
is_running=`pgrep dockerd`
if [[ -n "${is_running}" ]]; then
echo "docker running";
else
echo "Starting docker";
echo "<password>" | sudo -S nohup dockerd >/dev/null 2>&1 &
fi
# replace <password> with the real password and save the .bashrc file
Basically this should solve the problem of docker installation and running in Windows (WSL)
Further Readings (Source):
