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

[Feature]: Support for Compose logs

Open benmoss opened this issue 3 months ago • 4 comments

Problem

One of my docker compose containers is failing to come up, and so I get an unhelpful error message:

dependency failed to start: container 62e034fc-fae9-4e80-af07-8c1d8902b1da-minio_init-1 is unhealthy

If I was using the Docker compose CLI, I would call docker compose logs to see why the container was failing.

Solution

Add something to the Compose module's API to get logs of all the containers

Benefit

Debugging, providing helpful test output in the case of failures

Alternatives

Not as far as I can think of

Would you like to help contributing this feature?

Yes

benmoss avatar Mar 12 '24 15:03 benmoss

As a workaround I came up with this:

t.Cleanup(func() {
	ctx := context.Background()
	for _, service := range compose.Services() {
		container, err := compose.ServiceContainer(ctx, service)
		if err != nil {
			t.Logf("failed to get container for service %q: %v", service, err)
			continue
		}
		logs, err := container.Logs(ctx)
		if err != nil {
			t.Logf("failed to get logs for service %q: %v", service, err)
			continue
		}
		buf, err := io.ReadAll(logs)
		if err != nil {
			t.Logf("failed to read logs for service %q: %v", service, err)
			continue
		}
		t.Logf("[%s]\n%s", service, string(buf))
	}
})

benmoss avatar Mar 12 '24 15:03 benmoss