Skip to content

Instantly share code, notes, and snippets.

@kekru
Last active January 21, 2023 17:12
Show Gist options
  • Save kekru/0d14eb363260df78d012c901b94a19be to your computer and use it in GitHub Desktop.
Save kekru/0d14eb363260df78d012c901b94a19be to your computer and use it in GitHub Desktop.
Windows 10 Subsystem for Linux combined with Docker for Windows

Using Windows Subsystem for Linux combined with Docker for Windows

Docker CE for Windows

  • Install Docker CE for Windows
  • Go to Docker for Windows Settings -> General and enable Expose daemon on tcp://localhost:2375 without TLS.
    This will enable the Docker remote API for requests, coming from localhost, not from another computer in your network. A TLS secured version is not yet supported in Docker for Windows. See docker/for-win#453 for more information. I also tried a daemon.json file with options tlscacert, tlscert, tlskey and tlsverify, but Docker for Windows crashed on booting.

Install Windows Subsystem for Linux (WSL)

  • Enable Developer Mode as described in Enable your device for development
  • Install WSL by running the following in Powershell as Administrator: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux or read the Installation Guide
  • Reboot your computer
  • Open Windows search bar and type bash and you will find Bash on Ubuntu on Windows
  • Open it and you can start writing your bash commands

Download Docker Client

To be able to run Docker command, we need the Docker Client CLI in our WSL environment.
You find a script below, to download and setup Docker CLI and Docker Compose.
You can run it directly by executing the following:

sh -c "$(curl -sSL https://gist.githubusercontent.com/kekru/0d14eb363260df78d012c901b94a19be/raw/2-install-docker-client.sh)"

Content of the script:

  • It downloads the Docker binaries from https://download.docker.com/linux/static/edge/x86_64/
  • It extracts the docker executable (CLI client) from the tgz file and saves it under '/usr/local/bin/docker'.
  • It downloads bash completion
  • It adds the environment variable DOCKER_HOST=localhost:2375 to the ~/.bash_aliases file. Now Docker CLI knows that it should speak to the docker server on localhost:2375, which you enabled in Docker for Windows before.
  • It adds a rule to switch to /c when bash is started in /mnt/c
  • It adds a symbolic link, so that /c resolves to /mnt/c. By default you use /mnt/c in WSL to access your c drive. Now you can also use /c, which is the same behaviour, Docker for Windows uses, when you work with Docker volumes.

Bash from explorer context menu

To be able to start the WSL bash from the Windows explorer context menu, save 3-bash-context-menu.reg (see below) to a file and execute it by double click. Now you can open a WSL bash by right clicking in an explorer directory.

#!/bin/bash
set -e
# Docker Version
DOCKERVERSION=docker-17.11.0-ce
# Docker Compose Version
COMPOSEVERSION=1.17.1
# Download Docker CLI and Compose
DIR=~/install-docker-client-temp
mkdir -v --parents $DIR
curl https://download.docker.com/linux/static/edge/x86_64/$DOCKERVERSION.tgz | tar xvz --directory $DIR
mv -v $DIR/docker/docker /usr/local/bin/docker
chmod +x /usr/local/bin/docker
curl -L https://github.com/docker/compose/releases/download/$COMPOSEVERSION/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
rm -vR $DIR
# Install bash completion
sudo curl -L https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker -o /etc/bash_completion.d/docker
sudo curl -L https://raw.githubusercontent.com/docker/compose/$COMPOSEVERSION/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
# Set DOCKER_HOST variable in .bash_aliases
tee --append ~/.bash_aliases <<'EOF' > /dev/null
# set Docker remote
export DOCKER_HOST=localhost:2375
# switch to /c when starting bash in /mnt/c
DIR=$(pwd)
if [[ $PWD == "/mnt/c"* ]]; then
DIR=$(echo $DIR | cut -d '/' -f3-)
cd "/"$DIR
fi
EOF
# add symbolic link, so that /c resolves to /mnt/c
sudo ln -s /mnt/c /c
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\wslbash]
@="Open WSL Bash"
"Icon"="%USERPROFILE%\\AppData\\Local\\lxss\\bash.ico"
[HKEY_CLASSES_ROOT\Directory\Background\shell\wslbash\command]
@="C:\\Windows\\System32\\bash.exe"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment