testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

[Feature]: Support custom auth configs in container options

Open SpencerC opened this issue 1 year ago • 4 comments

Problem

In sandboxed/hermetic environments such as Bazel, $HOME is not available. This causes builds to fail following #2727. In our case, we're just using public registries so appearently being unable to load an auth config wasn't a problem before.

Solution

Add DockerConfig and DockerAuthConfig options to ContainerRequest or FromDockerfile.

Benefit

This would simplify the upgrade/management process for users running TestContainers in sandboxed environments. It also seems more consistent with the design of the rest of the package (e.g. configuration in code vs. environment variables).

Alternatives

Testers can always set DOCKER_CONFIG or DOCKER_AUTH_CONFIG in their bash profile, but then this needs to be done on every system running the tests.

Another approach is to map a sandbox path and set DOCKER_CONFIG at runtime:

dac, err := runfiles.Rlocation("_main/docker/.config")
if err != nil {
	log.Fatalf("could not get absolute path to docker auth config: %v", err)
}
if err := os.Setenv("DOCKER_CONFIG", dac); err != nil {
	log.Fatalf("could not set DOCKER_CONFIG: %v", err)
}

But this is a bit hacky.

Would you like to help contributing this feature?

Yes

SpencerC avatar Aug 31 '24 14:08 SpencerC

I can do a PR for this if there's interest.

SpencerC avatar Aug 31 '24 14:08 SpencerC

Testers can always set DOCKER_CONFIG or DOCKER_AUTH_CONFIG in their bash profile, but then this needs to be done on every system running the tests.

I'd like to walk back this statement a bit. Doing this works on my system (macOS), but has broken my colleagues' ability to run our tests. One is also running macOS, another is running linux.

SpencerC avatar Sep 02 '24 14:09 SpencerC

They are encountering this error: https://forums.docker.com/t/error-failed-to-solve-error-getting-credentials-err-exit-status-1-out/136124/10

SpencerC avatar Sep 02 '24 14:09 SpencerC

I too am hitting this on MacOS, linux is fine.

For awhile if I set env_inherit.. it would work, but that has stopped..

go_test(
    name = "testing_test",
    srcs = ["postgres_test.go"],
    embed = [":testing"],
    embedsrcs = [
        "migrations/20241006221227_test.down.sql",
        "migrations/20241006221227_test.up.sql",
    ],
    deps = [
        "@com_github_golang_migrate_migrate_v4//database/postgres",
        "@com_github_stretchr_testify//require",
    ],
    env_inherit = ["HOME"]
)

Any.. advice?

I don't fully follow you're runfiles approach.

Geethree avatar Oct 26 '24 21:10 Geethree