In previous articles (Part 1., Part 2.) I showed how to configure WSL itself and dev tools to work alongside. In this one, I’ll present how to install and configure Docker (with Docker Compose) using WSL Linux distribution.
Installation
The installation process of Docker is well described on the documentation page. There are a few installation methods, but I’ll focus on the one using an apt repository.
Execute steps from Set up the repository and then from Install Docker Engine paragraphs.
Post-install configuration
In this part I’ll describe how to configure Docker to use it as a non-privileged user and start the daemon automatically on the Linux OS startup.
Installation verification
When you try to execute the last step of the installation instruction in order to verify if Docker was installed successfully:
sudo docker run hello-world
You’ll encounter the following failure message:
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. See 'docker run --help'.
It’s because Docker daemon is not running. You can verify it, by executing:
service docker status
The output should be as follows:
* Docker is not running
You can start the Docker daemon by executing:
sudo service docker start
Having the Docker daemon started, it should be possible to run hello-world example.
Using Docker as a non-privileged user
By default, sudo must be used to use a docker command. It’s because docker user group has been created, but it contains no users.
Execute the following command to add the current user to the docker user group:
sudo usermod -aG docker $USER
Now exit the WSL Linux distro and let it restart – wait until status will be stopped by using:
wsl -l -v
command in PowerShell. If you don’t want to wait for Linux stopping automatically (it should be about 30 s.), you can stop it manually:
wsl --shutdown
Remember, that Docker is not configured yet to start automatically, so after logging in to your WSL Linux again, execute first:
sudo service docker start
And after that try to run hello-world example, but now – without using sudo:
docker run hello-world
If you encountered the following error:
WARNING: Error loading config file: /home/user/.docker/config.json - stat /home/user/.docker/config.json: permission denied
Refer to the documentation.
Starting Docker daemon automatically
So far, Docker has been installed and now it can be used without sudo, but it doesn’t start automatically with the system.
systemd
Many modern Linux distributions use systemd to manage services. Debian and Debian based Linux distributions – like Ubuntu are no exception for that. However, in case of WSL, there’s
a lightweight Linux (for example Ubuntu) distribution, that doesn’t use systemd (at least by default, as it’s possible to configure it to use systemd from a certain Windows 10 and Windows 11 update).
If the only reason to enable systemd in WSL Linux distribution is to start some services automatically, I would recommend not to do this that way. Check the interesting thread explaining this topic further.
WSL configuration
Simple WSL configuration can be used for automatic service execution at the OS startup. The only thing to do is to edit the /etc/wsl.conf file as described in this thread – the same that was edited in order not to generate resolvConf as described in the 1st article of this series.
Open the mentioned file with the chosen editor and admin privileges, for example, using nano:
sudo nano /etc/wsl.conf
Add the following lines and save the file:
[boot] command="service docker start"
In order to verify if new settings work properly, stop WSL Linux distro, and start it again. Check with the following command if Docker daemon is running:
service docker status
The output should be as follows:
* Docker is running
Configure logging driver
Last but not least, remember to configure Docker logging driver, for example as described here.
Running database example
In this section I’ll demonstrate how WSL can be used for running Docker containers. In this example MySQL database and that it is possible to easily work with such db from Windows host, the same way as it would have been installed natively on Windows.
Database configuration
In order to run database, docker compose tool will be used. Let’s begin with creating really simple compose file in the desired WSL Linux catalogue – for example the one as below (you can use Visual Studio Code for that 😉):
version: “3.8” services: mysql-db: image: mysql:8.0.32 restart: always environment: MYSQL_DATABASE: “mysql-test-db” MYSQL_USER: “test-user” MYSQL_PASSWORD: “test-password” MYSQL_ROOT_PASSWORD: “root-password” ports: - “3306:3306” volumes: - mysql-db-volume:/var/lib/mysql volumes: mysql-db-volume:
Save the file, and execute command to run the db:
docker compose -f mysql-compose.yml up
mysql-compose.yml is a docker-compose file name – change it regarding to your needs.
MySQL database should be up and running now, and be available to connect on port 3306.
Connect to the database from Windows
Having database up and running, it’s time to establish a connection from Windows host.
For that purpose, I’ll use application called DBeaver CE. It’s free universal tool, that can be used to connect and operate on different vendors databases, including the most popular, like MySQL, PostgreSQL, MariaDB, Microsoft SQL Server, etc. as well as to not relational dbs.
Configuration is straightforward – just enter the data configured in docker compose file, for this example it would be:
Server Host: localhost Port: 3306 Database: mysql-test-db Username: test-user Password: test-password
If you use Dbeaver, you may encounter a connection problem, like Public Key Retrieval is not allowed
. In connection settings, Driver properties tab, under User Properties add those 2 properties with values, as described in this thread:
useSSL: false allowPublicKeyRetrieval: true
That’s all. MySQL db is running in Docker container under the WSL Linux, but it’s possible to work with it from Windows host as it was installed there.
Conclusion
In this article I showed how to install and configure Docker to be used with WSL, as well as I presented on how it can be leveraged be example of using a database container.
This was the last part of three-series articles regarding the WSL configuration and usage. I believe, that I managed to show at least the most important aspects of WSL – how it can be configured and used in a daily development work.
There’s no doubt, this is an interesting tool that may be helpful – especially when working with Docker containers. Would I recommend it to everybody who use Windows? I think, I wouldn’t. You must remember that this tool is still under development – though quite stable and mature, there’re at least few problems to solve in terms of the configuration of some applications to work with it properly.
If I had to sum up – I would recommend it to all, who for some reason have any problems with working on Windows (for example previously used Linux for development, some tools don’t work properly, etc.), and also for those who create multi-platform applications.
Sources
- Docker docs – Install using the Apt repository
- Docker docs – Manage Docker as a non-root user
- AskUbuntu – System has not been booted with systemd as init system
- SuperUser – How to make WSL run services at startup
- Docker docs – Local File logging driver
- Stack Overflow – Connection Java – MySQL: Public Key Retrieval is not allowed
***
Other articles in the series can be found here:
Leave a comment