Repository navigation
Conversation
withEnvironment merged the new variables into this.environment but appended them to createOpts.Env, so setting a key twice sent both entries to Docker. The last one won at runtime, but docker inspect showed the stale value, the reuse hash differed from a container configured with the final value once, and subclasses that need to change a variable had to edit createOpts.Env by hand. Build createOpts.Env from the merged environment so it holds one entry per key. Document that repeated withEnvironment calls add to the variables and that setting a key again replaces its value. Fixes testcontainers#1519
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1519
Summary
withEnvironmentmerged the new variables intothis.environmentbut appended them tocreateOpts.Env. Setting a key twice sent both entries to Docker:The process saw
A=2, so this was mostly invisible, butdocker inspectshowed the staleA=1, the reuse hash differed from a container configured with{ A: "2" }once, and a subclass that needs to change a variable had to filtercreateOpts.Envby hand (theREDIS_ARGSworkaround inRedisContainer.start()).createOpts.Envis now built from the mergedthis.environment, so it holds one entry per key and the last value wins, matching what the container already saw at runtime. The Redis workaround keeps working unchanged and is already being removed with the Redis Stack code in #1597, so this PR does not touch it.The containers doc gets one line saying that repeated
withEnvironmentcalls add to the variables and that setting a key again replaces its value.Verification
npm run lint:ci,npm run knip,npm run check-compilesmkdocs build --strictnpx vitest run packages/testcontainers/src/generic-container/generic-container.test.tsNew case in
generic-container.test.ts,should replace an environment variable that is set twice: setscustomKeytwice andotherKeyonce, starts the container, and checks throughinspect()thatConfig.Envhas exactly onecustomKey=secondentry and still hasotherKey=otherValue, and through the test image's/envendpoint that the process seessecond.generic-container.ts): 1 failed,Config.Envcontained bothcustomKey=firstandcustomKey=second.Not breaking
No public API change. A key set once produces the same
Enventry as before. A key set more than once now produces one entry with its final value, which is the value the container process already received.