Search⌘ K
AI Features

Programmatically Creating Containers

Explore how to programmatically define and customize Docker containers for integration testing with TestContainers-scala in Scala. Learn to create your own container classes, manage credentials, and use these containers in test suites for flexible and maintainable tests.

In this lesson, we’re going to take a look at a different approach to using TestContainers-scala: programmatically defining containers. This option gives us maximum flexibility, letting us customize every aspect of the Docker containers we’ll be using in our tests. This is our preferred approach when the built-in modules don’t suit our needs. It’s also the way to go when you want to use your own Docker images (for example, additional internal applications needed by your code).

Defining a container

When you decide to create your own container definition, the first step is to model it. Since we’re now writing our own class, it can contain any helper methods we need to make our tests more declarative.

Scala
class LocalStackContainer private (underlying: GenericContainer) extends GenericContainer(underlying):
lazy val endpoint = s"http://localhost:${mappedPort(LocalStackContainer.LocalStackPort)}"
val accessKeyId = "not_used"
val secretAccessKey = "not_used"

This class defines our own wrapper of a LocalStack container by extending the GenericContainer class provided by ...