2

I have an existing PostGres docker container running in the background. Is it possible to create a GenericContainer that will parse the existing PostGres container by taking in the container ID, instead of creating a completely new container?

Ajay
  • 21
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 13 '22 at 07:25

1 Answers1

1

No, one of the main ideas of Testcontainers is that it manages the lifecycle and configuration of the containers, for example shutting down containers after running the tests (even if they fail, or crash).

What you can do is to reuse a container managed by Testcontainers in your tests, sort of how described here.

In short, you can enable container reuse in your environment (the reused contaienrs aren't stopped or cleaned up automatically, so the environment needs to consent to run with the reusable containers). You can do this by adding: testcontainers.reuse.enable=true to the ~/.testcontainers.properties file.

Then on the container definition add .withReuse(true). The container is then labelled with a hash of the definition, and excluded from being stopped. Because of this the container will outlive the tests, and the next time you run the tests, Testcontainers will check the hash and find the running container and will use that instead of creating a new one.

This works for testcontainers-java, I'm not sure it is implemented in the other implementations the same way.

Oleg Šelajev
  • 3,530
  • 1
  • 17
  • 25
  • This is actually exactly what I want. I turned reuse on, but testcontainers fails to pickup the existing containers, and creates a second copy. Any idea how to fix this? – Ajay Aug 14 '22 at 02:51
  • Are these _existing containers_ created by Testcontainers, or by you? Else, if you share the code for defining the containers, we might be able to have some insights into where there might be issues. – Kevin Wittek Aug 15 '22 at 07:36