Skip to content

Conversation

@wybczu
Copy link
Contributor

@wybczu wybczu commented Apr 25, 2025

This will enable users to use authed docker registries. An example for Google Cloud Artifact Registry:

func Example_artifact_registry_auth() {
	dockerImageName := "us-docker.pkg.dev/GOOGLE_CLOUD_PROJECT/REGISTRY/image:latest"

	ctx := context.Background()

	// using github.com/google/go-containerregistry/pkg/v1/google
	authenticator, err := google.NewEnvAuthenticator(ctx)
	if err != nil {
		panic(err)
	}

	authConfig, err := authenticator.Authorization()
	if err != nil {
		panic(err)
	}

	authConfigJSON, err := authConfig.MarshalJSON()
	if err != nil {
		panic(err)
	}

	opts := dktest.Options{
		PortRequired:     true,
		ReadyFunc:        func(ctx context.Context, c dktest.ContainerInfo) bool { return true },
		LogStderr:        true,
		LogStdout:        true,
		PullRegistryAuth: base64.StdEncoding.EncodeToString(authConfigJSON),
	}

	dktest.Run(&testing.T{}, dockerImageName, opts, func(t *testing.T, c dktest.ContainerInfo) {})

	// Output:
}

This will allow to pull images from authed docker registries. An example for
Google Cloud Artifact Registry:

```go
func Example_artifact_registry_auth() {
	dockerImageName := "us-docker.pkg.dev/GOOGLE_CLOUD_PROJECT/REGISTRY/image:latest"

	ctx := context.Background()

	// using github.com/google/go-containerregistry/pkg/v1/google
	authenticator, err := google.NewEnvAuthenticator(ctx)
	if err != nil {
		panic(err)
	}

	authConfig, err := authenticator.Authorization()
	if err != nil {
		panic(err)
	}

	authConfigJSON, err := authConfig.MarshalJSON()
	if err != nil {
		panic(err)
	}

	pullRegistryAuth := base64.StdEncoding.EncodeToString(authConfigJSON)

	opts := dktest.Options{
		PortRequired:     true,
		ReadyFunc:        func(ctx context.Context, c dktest.ContainerInfo) bool { return true },
		LogStderr:        true,
		LogStdout:        true,
		PullRegistryAuth: pullRegistryAuth,
	}

	dktest.Run(&testing.T{}, dockerImageName, opts, func(t *testing.T, c dktest.ContainerInfo) {})

	// Output:
}
```
Copy link
Owner

@dhui dhui left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @wybczu!

@dhui dhui merged commit 0e2cf78 into dhui:master Apr 25, 2025
7 checks passed
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