Configure TestContainers to Use Remote Docker hosts

Overview

With the recent Docker’s license change, companies no longer can use Docker desktop without a subscription. Since software companies have been using free software for far too long, they cannot get used to this.

Damn those greedy Docker desktop makers, how can our company survive!!!

CEO of some billion dollars software companies who refused to pay for Docker desktop license

If you are a dev in such companies and got addicted to TestContainers, there’s still hope. No, I’m not telling you to install a Windows virtual machine and install Docker desktop on that. Doing so still violates Docker desktop new license.

Let’s find out how you can TestContainers without using Docker desktop if you are on Windows or Mac!

First step: Create an Ubuntu virtual machine and install dockerd on that

The new license applied only to Docker desktop. You are free to install and use docker on linux distros. I will not bore you to death with a lengthy tutorial on how to install docker on Ubuntu or fedora. There are plenty tutorials available. The official tutorial is a decent one and that’s all you need.

Next step: Expose docker for remote connection

By default, docker doesn’t open port to remote connections. Let’s fix that.

First, create a file named daemon.json under /etc/docker (edit if that file is already available)

Put the following code in (if you see similar code available, skip this step)

 {"hosts": 
    ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
 }

Save the file. You need to have root privileges to create/edit this file.

Next, create this folder

sudo mkdir -p /etc/systemd/system/docker.service.d/

Then create this file: /etc/systemd/system/docker.service.d/override.conf

and put the following content in:

 [Service]
 ExecStart=
 ExecStart=/usr/bin/dockerd

Now, execute the following commands with root privileges

 sudo systemctl daemon-reload
 sudo systemctl restart docker

Your docker service should be restarted without problems.

What do you have now? A remote linux distro running docker environment and has port 2375 exposed for remote connection.

That’s all you need to tell TestContainers to connect to.

Configure TestContainers to connect to Remote Docker enviroment

TestContainers has detailed configuration tutorials here.

Go to that page, find out where is the configuration file on your system.

On Windows, that would be C:\Users\USER_NAME\

Create and edit a file at that location named `.testcontainers.properties` (with the dot) and put the following content:

docker.client.strategy=org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy
docker.host=tcp\://192.168.56.102\:2375

Replace the IP (192.168.56.102) with your own address.

Now you are done!

You can run TestContainers now.

Running TestContainers

When running the tests (I’m using Spring Boot) I am able to see the container running:

Testcontainers pulling and running containers on remote host
Testcontainers pulling and running containers on remote host

On the remote host, you can see that the image is actually available there:

Docker image on remote host
Docker image on remote host

Amazing, isn’t it?

Conclusion

As you can see, with the new limitation of Docker desktop, you may need to do some extra work to continue working with Docker if you are using Windows or Mac computer at your company. Until further update to the license (or your company budget policy), this is a nice workaround!

Leave a Comment