Using Testcontainers on Rancher Desktop

Rancher Desktop can be used with Testcontainers to execute ephemeral tests and containers that work inside Docker. This guide demonstrates the use of Testcontainers with a sample repository.

Prerequisites

Testcontainers can only be used with the moby (dockerd) runtime as it requires a Docker-API compatible container runtime. Kubernetes must be disabled for machines on Apple Silicon. The setting can be disabled via the Preferences > Kubernetes dialog, or by using the rdctl command below:

  1. rdctl set --kubernetes-enabled=false

Please also ensure that Apache Maven is installed on your machine in order to make use of the mvn verify command.

  • Linux
  • macOS
  • Windows

You can download a sample test repository in the testcontainers-java-repro located here: https://github.com/testcontainers/testcontainers-java-repro

After the repository is downloaded, please navigate to the testcontainers-java-repro folder and run the command mvn verify.

  1. mvn verify

After the command has been run, you should see a BUILD SUCCESS with test statistics for failures, number of tests ran, skipped tests, time elapsed, and errors.

You can download a sample test repository in the testcontainers-java-repro located here: https://github.com/testcontainers/testcontainers-java-repro

  • Apple Silicon
  • Intel

Currently, workarounds are needed for using Testcontainers on macOS M1 machines. Below are methods for using Testcontainers on either runtime, depending on administrative access.

QEMU

Details

Workaround Summary

This runtime can be used with administrative access enabled which can be set via the Preferences > Application > General dialog. This will ensure that routable IP’s are allocated.

Next, export the virtual machine port explicitly using the command below:

  1. export TESTCONTAINERS_HOST_OVERRIDE=$(rdctl shell ip a show rd0 | awk '/inet / {sub("/.*",""); print $2}')

VZ

Details

Workaround Summary

This runtime can be used with administrative access enabled which can be set via the Preferences > Application > General dialog. This will ensure that routable IP’s are allocated.

Next, export the virtual machine port explicitly using the command below:

  1. export TESTCONTAINERS_HOST_OVERRIDE=$(rdctl shell ip a show vznat | awk '/inet / {sub("/.*",""); print $2}')

For VZ virtual machines, you can also use Testcontainers without the need for administrative access by exporting the settings below:

  1. export DOCKER_HOST=unix://$HOME/.rd/docker.sock
  2. export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock
  3. export TESTCONTAINERS_HOST_OVERRIDE=$(rdctl shell ip a show vznat | awk '/inet / {sub("/.*",""); print $2}')

After the respective virtual machine settings have been applied, and the repository is downloaded, please navigate to the testcontainers-java-repro folder and run the command mvn verify.

  1. mvn verify

After the command has been run, you should see a BUILD SUCCESS with test statistics for failures, number of tests ran, skipped tests, time elapsed, and errors.

After the repository is downloaded, please navigate to the testcontainers-java-repro folder and run the command mvn verify.

  1. mvn verify

After the command has been run, you should see a BUILD SUCCESS with test statistics for failures, number of tests ran, skipped tests, time elapsed, and errors.

You can download a sample test repository in the testcontainers-java-repro located here: https://github.com/testcontainers/testcontainers-java-repro

After the repository is downloaded, please navigate to the testcontainers-java-repro folder and run the command mvn verify.

  1. mvn verify

After the command has been run, you should see a BUILD SUCCESS with test statistics for failures, number of tests ran, skipped tests, time elapsed, and errors.