Skip to content

Add environment variable substitution to docker compose parsing - #3628

Open
mpdn wants to merge 2 commits into
testcontainers:mainfrom
mpdn:compose-env-substitution
Open

Add environment variable substitution to docker compose parsing#3628
mpdn wants to merge 2 commits into
testcontainers:mainfrom
mpdn:compose-env-substitution

Conversation

@mpdn

@mpdn mpdn commented Dec 23, 2020

Copy link
Copy Markdown

Adds environment variable substitution (as described here) to ParsedDockerComposeFile.

This also means compose file parsing is deferred until the container is started.

@rnorth rnorth changed the title Add environemnt variable substitution to docker compose parsing Add environment variable substitution to docker compose parsing Jan 10, 2021

@rnorth rnorth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, I think it makes sense but I've suggested a couple of things.

Just to note, we wanted to keep our parsing of docker-compose/Dockerfiles as barebones as possible, as we really don't want to have a lot of implementation in parallel with the Docker userland tools. From my perspective this PR is OK but about as far as we should go.

this.composeFileContent = testContent;
ParsedDockerComposeFile(Map<String, Object> testContent, Map<String, String> env) {
//noinspection unchecked
this.composeFileContent = (Map<String, Object>) substituteEnv(testContent, env);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use Apache Commons StringSubstitutor instead?

At a glance your implementation of substituteEnv looks decent, but it would be good to avoid having our own implementation of placeholder substitution from a maintenance/verification perspective.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using StringSubtitutor could be possible, but it seems to have a number of differences:

  1. It behaves like - (replacing if the variable is set, even if empty). You can't really get the same behavior as :- as described in the docker docs.
  2. Matching multiple different operators seem possible, albeit slightly weird.

Assertions
.assertThatThrownBy(() -> {
new ParsedDockerComposeFile(file);
new ParsedDockerComposeFile(file, emptyMap());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be worth having a constructor that omits the Map parameter, at least for testing purposes?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Having it as a parameter forces the user of the class to consider the env. vars (if any) to substitute with. A developer might not otherwise consider that parsing a compose file depends on environment variables.

But I can add a constructor variant defaulting to no env. vars if you disagree with this.

@stale

stale Bot commented Jun 18, 2021

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this.

@stale stale Bot added the stale label Jun 18, 2021
@rnorth rnorth removed the stale label Jun 24, 2021
@eddumelendez
eddumelendez requested a review from a team November 19, 2024 22:04
@eddumelendez
eddumelendez requested a review from a team as a code owner August 19, 2025 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants