Windows Subsystem for Linux 2 (WSL2) brought some fresh air to the Windows 10 operating system. Before WSL1, we had to have a separate virtual machine or dual boot to develop applications that were necessary to run on Linux. In some cases, the development process was just easier under Linux. Ten years ago I was developing applications with Ruby on Rails and I remember it was tricky to install the tools and run the code on Windows. That’s why I was using Ubuntu Linux for years and loved the experience.
In the last couple of years, I am working mostly with Microsoft .NET and using many open source applications, tools, and databases to get the work done. That means installing all sorts of database technologies, monitoring tools, messaging infrastructure, frameworks, tools, etc. And those apps are hungry for resources and install a lot of libraries and tools that can clog up the system in the long run. At least that’s my experience with the Microsoft ecosystem.
Recently I was re-installing my desktop PC to Windows 10 (2004) and wondered how I could improve that. With a fresh system, there is no better time to do it. Many tools I use run on Linux and are nowadays containerized and can be run with Docker. For some time now you could run Linux Docker containers inside Windows 10, but the latest Windows update supports WSL2, which is faster and more optimized.
Running docker containers is great, but the real magic lies in a Docker compose scripts. With those, you can easily spin up multiple containers at once, so for example, if you need a database and a client of some sort, you can compose them and run them together. In the end, I prepared basic docker-compose files for these tools:
- Microsoft SQL server
- MySQL
- PostgreSQL
- RabbitMQ
- Grafana
- Prometheus
- Nginx
- InfluxDB
- MongoDB
- Redis
For every application, there are three parts: docker-compose file, variables file, and a runner script. First, let me show you a Docker compose file for SQL Server 2019. Every docker image can have several environment variables you can find about those for every image in DockerHub. In the case of SQL Server 2019, we need to specify the SA password, ProductID and we need to accept EULA automatically. Next, if you want the state to persist you need to mount a volume (which is something you want to do for a database). We also need to publish a port and specify a docker image. Save the below docker-compose file to docker-compose.yml and continue.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
version: '3.8' services: run: container_name: ${containerName} environment: - ACCEPT_EULA=Y - MSSQL_PID=${sqlPID} - SA_PASSWORD=${password} volumes: - '${volumePath}:/var/opt/mssql/data' ports: - '${hostPort}:1433' image: ${image} |
As you can see, there are some properties that have variable placeholders (${…}), which will be specified in the file:
1 2 3 4 5 6 |
containerName=sqlserver volumePath=c:/Apps/DockerVolumes/sqlserver hostPort=1433 image=mcr.microsoft.com/mssql/server:latest sqlPID=EXPRESS password=MyPass123. |
Save the variables to the sqlserver.env file to the same folder as docker-compose.yml.
The final piece is a runner script. As a command argument, you need to specify a docker-compose and variable file:
1 |
docker-compose -f "c:\docker\docker-compose.yml" --env-file "c:\docker\sqlserver.env" up |
If everything is setup correctly you should see SQL server starting:
1 2 3 4 5 6 7 8 |
❯ sqlserver Starting sqlserver ... done Attaching to sqlserver sqlserver | SQL Server 2019 will run as non-root by default. sqlserver | This container is running as user mssql. sqlserver | Your master database file is owned by mssql. sqlserver | To learn more visit https://go.microsoft.com/fwlink/?linkid=2099216. ... |
And that’s it! Now you can run SQL Server 2019 on demand and save local resources :).
What’s next?
To extend this further, you could create docker-compose Powershell scripts, add those to the Windows 10 PATH ENV variable and run them from the Windows Terminal with Powershell Core.
The solution above keeps resources on Windows 10 under control. No more unnecessary services and applications that run all the time. This saves CPU, memory, and storage.
I’ve put those scripts on the Github. I’ll improve them in the future, so be sure to check the GIT repository from time to time :).
Let me know what you think in the comments below or contact me through LinkedIn!
Leave a Reply
You must belogged in to post a comment.