testcontainers-node: Reusing of docker compose containers is not possible
We have simple use case: We want to spin up some background containers (like a database or a file storage) to run our test against it. The test are cleaning the containers as they need. These containers should be reused on every test run so we save time in every TDD cycle.
We define these containers in a docker-compose.yml and use the docker-compose cli with docker-compose up.
docker-compose up reuses the container everytime with the option --no-recreate.
This approach is working fine.
If we try that with testcontainers we get everytime the error that the containers are already existing.
const composeFilePath = path.resolve(__dirname, "dir-containing-docker-compose-yml");
const composeFile = "docker-compose.yml";
const await new DockerComposeEnvironment(composeFilePath, composeFile).up();
According to the docs its also not possible to pass in options like --no-recreate.
Is testcontainers made to serve our usecase? Or should we stick to the raw docker-compose cli?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16
@kylemillar608 I’ve basically reverted the fix from the first part of this issue and what I’ve done is if
withNoRecreateis specified, the project name label is changed from a UUID to a constant, “testcontainers-node” in this case. This means the hash will remain the same across instances and therefore the containers won’t be recreated.I opted against removing the label altogether if the reaper is disabled because when we start a docker-compose environment, we rely on the label to differentiate between containers we’ve started, and containers which were previously running. Without the label to filter on, if the reaper is enabled we’d kill containers created outside of testcontainers.
I forgot to mention that we are using
TESTCONTAINERS_RYUK_DISABLED=true. So we want to really reuse the containers between runs.When I now use the v7.22.0 like that:
I will get a
So we are not able to run the DockerComposeEnvironment between multiple test runs. Imagine having a npm task like
Then using this
npm run startand after a few minutes anpm run startagain. I want to reuse all containers to not wait again for that startup.Released in v7.22.0
I’ve got it working so that you can do:
And it won’t conflict. I take it that will work?
@tiloio it seems this does not work because the environments have different ID labels:
but this will work, is it OK for you?