If you're anything like me, you use both Windows and Linux in the same environment (The Windows subsystem for Linux is awesome) and you also use Docker.
Unfortunately, Docker doesn't run natively on the Windows subsystem for Linux, however.. That won't stop us from using our beloved Bash terminal for working with Docker! And with a few simple tewaks, you'll be able to use Docker and Docker compose from the WSL Bash terminal, just like on a full fat Linux machine (Sort of..).
We achieve this by exposing the Docker daemon to localhost and listening for any docker
or docker-compose
commands, allowing us to use all of the cli tools available via the WSL terminal.
Install Docker on Windows
If you haven't already, head over to Docker hub and create an account to download Docker Desktop for Windows.
Launch the installer once it's downloaded and follow the installation instructions.
Once Docker Desktop has been successfully installed, test it by launching a new powershell terminal and run the following:
docker run hello-world
This command will pull the hello-world
test image and run it. You should see:
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Next up, you need to expose the Docker daemon.
Launch Docker from the icon dock in the bottom right corner of your desktop by right clicking on the Docker icon and selecting settings
A settings window will launch with the general
tab in view. At the bottom you'll see the expose daemon on tcp://localhost:2375 without TLS
Go ahead and tick the box and close the window.
Next, we need to install Docker and Docker compose on the Windows subsystem for Linux
Installing Docker on the Windows subsystem for Linux
Launch a new WSL Bash shell and a new tab in your browser and head to the official Docker CE documentation here
Tip - The Docker CE documentation has installation guides for Ubuntu, Debian, Fedora and CentOS
Follow the installation guide for your flavour of Linux untill you get to the following section:
sudo docker run hello-world
docker run
isn't going to work and you'll get a cannot connect to the docker daemon
error.
Tip - Installing Docker compose is only a few commands away. Head over to the documentation here to install it
For the Linux Docker to communicate with Windows Docker, you'll need to export the DOCKER_HOST
environment variable.
We can do this with one line in our .bashrc
. Go ahead and open up your .bashrc
with the following:
nano ~/.bashrc
Scroll down to the bottom of the file and the following:
export DOCKER_HOST=tcp://locahost:2375
Save and close nano with Ctrl + x
, followed by y
then Enter
and reload the file with:
source ~/.bashrc
Whilst we're here in the shell, go ahead and add yourself to the docker
group with the following command:
sudo usermod -aG docker $USER
Now you can go ahead and test it! In your WSL Bash shell, run the following:
docker run hello-world
You should see the same message as you saw when we ran the same command in Powershell.
Congratulations! You can now use all of the Docker and Docker compose commands from your Windows subsystem for Linux shell.