How to run tests with TestContainers without Docker Desktop (Windows 10/11)

Guide was divided into two parts:

  1. Installing Ubuntu using WSL
  2. Installing Docker Engine and setting up environment variables

Step-by-step guide

 Installing Ubuntu using WSL

  1. Open CMD as administrator.
  2. Type the following command to enable WSL feature: 
    wsl --install
  3. Type the following command to enable WSL feature: 
    wsl --install -d Ubuntu-22.04

 Installing Docker Engine and setting up environment variables:

      1. Find in the search panel wsl application and run:


          

      2. Execute following commands in batch:

     sudo apt-get update && \
     sudo apt-get install ca-certificates curl gnupg && \
     sudo install -m 0755 -d /etc/apt/keyrings && \
     curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \
     sudo chmod a+r /etc/apt/keyrings/docker.gpg && \
     echo "deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null && \
     sudo apt-get update

      3. Install docker:

     sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

      4. Expose port:

     sudo sh -c 'echo "{\"hosts\": [\"tcp://0.0.0.0:2375\", \"unix:///var/run/docker.sock\"]}" > /etc/docker/daemon.json'

      5. Open CMD and restart WSL: 

     wsl --shutdown

      6. Open Linux terminal again and check that port is exposed:

     sudo apt-get update
     sudo apt-get install net-tools
     netstat -nl | grep 2375

      7. Add env variables for Windows:

NameValue
DOCKER_HOSTtcp://{wsl_ip}:2375
DOCKER_TLS_VERIFY0
DOCKER_CERT_PATH\\wsl$\home\{USER_NAME}\.docker

       Variables in brackets should be replaced with:

       {USER_NAME} - Your linux user

       {wsl_ip} - Execute following command and replace with inet IP: 

          ifconfig eth0

       

      8. Repeat step (5) after every restart of the Windows to re-expose the port 2375.

Troublehooting

  1. To check if docker service is running use:
    service docker status
  2. You can stop/start/restart docker service using the following commads:
    service docker stop
    service docker start
    service docker restart

If docker service fails to start with daemon.json config (step 4) but is running without it then run following commands and restart the service:

cd /etc/systemd/system
mkdir docker.service.d
cd docker.service.d
echo -e "[Service]\nExecStart=\nExecStart=/usr/bin/dockerd" > override.conf
systemctl daemon-reload

Related articles