Back to Blog
Originally published on Medium.Read Original Article
2024-02-18

Leveraging TestContainers with Podman

Leveraging TestContainers with Podman Introduction: TestContainers is a popular library that simplifies the setup and management of Docker c...

Share Editorial
Leveraging TestContainers with Podman

Leveraging TestContainers with Podman

Introduction:

TestContainers is a popular library that simplifies the setup and management of Docker containers for integration testing purposes. It seamlessly integrates with Docker, allowing developers to focus on writing test cases without wrestling with container intricacies.

However, with the growing adoption of Podman, a lightweight and user-friendly alternative to Docker, users might encounter compatibility challenges due to TestContainers’ current reliance on Docker.

This blog post aims to bridge this gap by providing a step-by-step guide on enabling TestContainers to work seamlessly with Podman on macOS. By following these instructions, you’ll be able to leverage the benefits of both TestContainers and Podman for efficient integration testing within your Spring Boot application.

Prerequisites:

  • Ensure you have Podman installed on your system. Refer to the official Podman documentation for installation instructions: https://podman.io/docs/installation
  • Possess a basic understanding of command-line operations and environment variables.

Steps:

Listing Podman Connections:

Open a terminal window and execute the following command to list available Podman connections:

podman system connection list

text
~ podman system connection list
Name                         URI                                                         Identity                                     Default
podman-machine-default       ssh://[email protected]:58331/run/user/501/podman/podman.sock  /Users/durgadas/.ssh/podman-machine-default  true
podman-machine-default-root  ssh://[email protected]:58331/run/podman/podman.sock           /Users/durgadas/.ssh/podman-machine-default  false

Identify the connection name that corresponds to your Podman setup. In most cases, it’s likely podman-machine-default .

Creating an SSH Tunnel:

Establish an SSH tunnel to forward the Podman socket from the remote machine to your local system, making it accessible to TestContainers:

text
ssh -fnNT -L/tmp/podman.sock:/run/user/501/podman/podman.sock -i ~/.ssh/podman-machine-default ssh://core@localhost:58331 -o StreamLocalBindUnlink=yes

Replace core with the username you used during Podman machine setup and adjust the path to your SSH key file if necessary.

Environment Variable Configuration:

Modify your shell configuration file (e.g.,  .zshrc or  .bashrc ) to set the following environment variables:

text
export DOCKER_HOST=unix://$(podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}')
export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock 
export TESTCONTAINERS_RYUK_DISABLED="true"

Verification and Testing:

Reload your shell configuration for the changes to take effect:

source ~/.zshrc # Or source ~/.bashrc

Run your TestContainers-based integration tests to confirm that they now function correctly with Podman.

Conclusion:

By following these steps, you’ve successfully integrated TestContainers with Podman , enabling you to leverage the combined benefits of both tools for streamlined integration testing within your Spring Boot or any other application.

Sponsored Advertisement