in case you haven't docker installed, please follow this instruction https://docs.docker.com/engine/install/ubuntu/
Create daemon.json file in /etc/docker, with content:
{"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}
Second way suggested by @maireadmccabe-fire
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vim /etc/systemd/system/docker.service.d/override.conf
# add the below to the override.conf file
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --host=tcp://0.0.0.0:2375 --host=unix:///var/run/docker.socksudo systemctl daemon-reload and then sudo systemctl restart docker
or
sudo service docker restart
or just restart WSL2 by calling wsl --shutdown in windows CMD/PowerShell and just open linux terminal once again
if you have run docker as sudo dockerd - stop it, and run sudo dockerd once again.
after restart docker daemon or restart wsl, run in linux terminal netstat -nl | grep 2375 (install net-tools if you haven't it). You should see that port are open.
add the following properties in Windows Env Variables:
| Name | Value |
|---|---|
| DOCKER_HOST | tcp://localhost:2375 |
| DOCKER_TLS_VERIFY | 0 |
| DOCKER_CERT_PATH | \\wsl$\home\$USER_NAME\.docker |
tcp://localhost:2375 should be replaced with tcp://$wsl_ip:2375, where $wsl_ip is IP of ifconfig eth0
✔️ There is a workaround suggest by @tacascer
@echo off
set port=:2375
set
(for /f "tokens=2" %%f in ('wsl ifconfig eth0 ^| findstr /c:"inet "') do (
set dockerhost=tcp://%%f%port%
))
@REM echo %dockerhost%
setx DOCKER_HOST %dockerhost%DOCKER_HOST variable must be same as exposed above
$USER_NAME -replace this placeholder with your linux user
.testcontainers.properties - were empty
To apply environment variables you need restart IDE (in my case is Intellij Idea) OR just specify in Run/Debug Configuration these env variables directly.
If you are looking for secure access to docker follow the guide suggested by @gim-
A complete, clean workflow for running .NET Testcontainers tests with MsSql using Docker on WSL (without Docker Desktop) and without relying on wrappers (docker.cmd) or environment-variable configurations. This setup has been validated and is the recommended approach.
# Step 1 — Install prerequisites inside WSL
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 8.0
export PATH=$PATH:$HOME/.dotnet
sudo apt update
sudo apt install docker.io -y
sudo service docker start
docker version
docker ps
# Step 2 — Create
.testcontainers.propertiescd ~
.testcontainers.properties:nano .testcontainers.properties
testcontainers.docker.client.transport=cli
testcontainers.docker.client.executable=docker
testcontainers.log.level=debug
Ctrl+O,Enter,Ctrl+X).cat ~/.testcontainers.properties
# Step 3 — Structure your .NET solution
MySQLApp/
├─ MySQLApp/ # Main project
│ └─ Program.cs
└─ MySQLApp.Tests/ # Test project
└─ SQLServerContainerTests.cs
MySQLApp.csproj).# Step 4 — Create a sample .NET app
Below is a minimal, working example of an xUnit test project that uses .NET Testcontainers to spin up a Microsoft SQL Server container and run a simple database test.
using System;
using Microsoft.Data.SqlClient;
using System.Threading.Tasks;
using Testcontainers.MsSql;
using Xunit;
public class SQLServerContainerTests : IAsyncLifetime
{
private readonly MsSqlContainer _sqlContainer;
private string _connectionString = default!;
}
# Step 5 — Run your tests from WSL
cd /mnt/c/dev/MySQLApp/MySQLApp.Tests
dotnet test
Expected:
unix:///var/run/docker.sock).