Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom container registry substitutor #2647

Merged
merged 2 commits into from
Jul 17, 2024

Conversation

stillya
Copy link
Contributor

@stillya stillya commented Jul 15, 2024

Custom Registry Substitutor

What does this PR do?

The new substitutor can be used by modules whose images are not placed on DockerHub by default. This way, you don't need to manually prefix your image with a custom container registry. Instead, you can configure your registry with a prefix at the configuration level using hub.image.name.prefix.

In a development environment, hub.image.name.prefix is commonly empty, so CustomHubSubstitutor will add the desired default prefix. However, in a CI environment, the default prefix will not be added, and the config prefix will be used (handled by prependHubRegistry).

Usage

func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*KeycloakContainer, error) {
	req := testcontainers.ContainerRequest{
		Image: img,
		Env: map[string]string{
			keycloakAdminUsernameEnv: defaultKeycloakAdminUsername,
			keycloakAdminPasswordEnv: defaultKeycloakAdminPassword,
		},
		ExposedPorts: []string{keycloakPort},
		Cmd:          []string{keycloakStartupCommand},
		ImageSubstitutors: []testcontainers.ImageSubstitutor{
			testcontainers.NewCustomHubSubstitutor("quay.io"),
		},
	}
}

Copy link

netlify bot commented Jul 15, 2024

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit c6af232
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/6696f50c7f4b1f0008653edd
😎 Deploy Preview https://deploy-preview-2647--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

The new substitutor can be used by modules whose images are not placed on DockerHub by default. This way, you don't need to manually prefix your image with a custom container registry. Instead, you can configure your registry with a prefix at the configuration level using hub.image.name.prefix.
@stillya stillya force-pushed the feat/custom-cr-substitutor branch from d82c3e6 to 344d43a Compare July 16, 2024 22:30
@stillya stillya marked this pull request as ready for review July 16, 2024 22:33
@stillya stillya requested a review from a team as a code owner July 16, 2024 22:33
@mdelapenya
Copy link
Member

@stillya thanks for submitting the PR. After discussing this on Slack, and seeing the code, I think the substitutor could make sense. I have one question regarding

In a development environment, hub.image.name.prefix is commonly empty, so CustomHubSubstitutor will add the desired default prefix. However, in a CI environment, the default prefix will not be added, and the config prefix will be used (handled by prependHubRegistry).

Do you have issues updating the properties file on the CI, configuring the well-known environment in which the project must work? I think it would be possible to do:

echo "hub.image.name.prefix=quay.io" >> ~/.testcontainers.properties 

as part of your Github action job, jenkins job, etc.

I'd like to understand the reasons why it's not possible to do that. Of course, if it's in code, the settings are applied to everybody, that's why I consider this changeset correct. Just want verify you have a workaround in the meantime.

@mdelapenya mdelapenya self-assigned this Jul 17, 2024
@mdelapenya mdelapenya added the feature New functionality or new behaviors on the existing one label Jul 17, 2024
@mdelapenya mdelapenya changed the title add custom container registry substitutor feat: add custom container registry substitutor Jul 17, 2024
@mdelapenya
Copy link
Member

Responding to myself (thanks @kiview): setting the properties will apply to all containers, but the substitutor will apply to the ones of interest.

@stillya
Copy link
Contributor Author

stillya commented Jul 17, 2024

Yep, that's the reason.

Copy link
Member

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@mdelapenya mdelapenya merged commit 99b8a93 into testcontainers:main Jul 17, 2024
108 checks passed
mdelapenya added a commit that referenced this pull request Aug 5, 2024
* main:
  feat: add grafana-lgtm module (#2660)
  Added valkey module (#2639)
  fix: container.Endpoint and wait.FortHTTP to use lowest internal port (#2641)
  chore: test cleanups (#2657)
  docs: fix compilation of examples (#2656)
  feat: add custom container registry substitutor (#2647)
  fix: couchbase containers intermittently hang on startup (#2650)
  chore(deps): bump Ryuk to 0.8.1 (#2648)
  fix: retry on label error (#2644)
  perf: optimise docker authentication config lookup (#2646)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or new behaviors on the existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants