[GH-ISSUE #711] Usage with testContainers-go #120

Closed
opened 2026-03-03 12:08:27 +03:00 by kerem · 4 comments
Owner

Originally created by @majelbstoat on GitHub (Mar 10, 2022).
Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/711

I'm trying to programmatically start fake-gcs-server using testcontainers-go. Example:

       // Start a container listening on port 3000 internally.
        storagePort := 3000
	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: testcontainers.ContainerRequest{
			Image:          "fsouza/fake-gcs-server:latest",
			Entrypoint:     []string{"/bin/fake-gcs-server", "-data", "/data", "-scheme", "http"},
			BindMounts:     map[string]string{"/data": e.hostDir},
			ExposedPorts:   []string{TCPPort(storagePort)},
			WaitingFor:     wait.ForLog("server started at"),
			Networks:       []string{Network},
			NetworkAliases: map[string][]string{Network: {StorageHostname}},
		},
		Started: true,
	})
	if err != nil {
		return err
	}

        // map this internal port to a randomly chosen external port.
	port, err := container.MappedPort(ctx, storagePort)
	if err != nil {
		return err
	}

        // set the emulator host and create a client.
        os.Setenv("STORAGE_EMULATOR_HOST", fmt.Sprintf("localhost:%s", port.Port())
        client, err  := storage.NewClient()
}

As with other folks, I can curl --insecure http://localhost:XXXX/storage/v1/b/sample-bucket/o and see my files in there, where XXXX is the mapped external port, but I can't retrieve them using the storage client.

From other issues, I believe this is because I'm not setting a public host. My problem is, I don't know what the full public host will be until after the container starts, because that's when I get the mapped port.

As others have note, -public-host 0.0.0.0 and -public-host localhost don't work, so I have this circular dependency which I can't figure out how to break.

Originally created by @majelbstoat on GitHub (Mar 10, 2022). Original GitHub issue: https://github.com/fsouza/fake-gcs-server/issues/711 I'm trying to programmatically start fake-gcs-server using testcontainers-go. Example: ```go // Start a container listening on port 3000 internally. storagePort := 3000 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: testcontainers.ContainerRequest{ Image: "fsouza/fake-gcs-server:latest", Entrypoint: []string{"/bin/fake-gcs-server", "-data", "/data", "-scheme", "http"}, BindMounts: map[string]string{"/data": e.hostDir}, ExposedPorts: []string{TCPPort(storagePort)}, WaitingFor: wait.ForLog("server started at"), Networks: []string{Network}, NetworkAliases: map[string][]string{Network: {StorageHostname}}, }, Started: true, }) if err != nil { return err } // map this internal port to a randomly chosen external port. port, err := container.MappedPort(ctx, storagePort) if err != nil { return err } // set the emulator host and create a client. os.Setenv("STORAGE_EMULATOR_HOST", fmt.Sprintf("localhost:%s", port.Port()) client, err := storage.NewClient() } ``` As with other folks, I can `curl --insecure http://localhost:XXXX/storage/v1/b/sample-bucket/o` and see my files in there, where `XXXX` is the mapped external port, but I can't retrieve them using the storage client. From other issues, I believe this is because I'm not setting a public host. My problem is, I don't know what the full public host will be until after the container starts, because that's when I get the mapped port. As others have note, `-public-host 0.0.0.0` and `-public-host localhost` don't work, so I have this circular dependency which I can't figure out how to break.
kerem closed this issue 2026-03-03 12:08:28 +03:00
Author
Owner

@fsouza commented on GitHub (Mar 10, 2022):

@sergseven recently added an internal endpoint for updating some configuration parameters, aimed specifically at setting the external URL after starting the container with test containers, you can see a Java example in his PR: https://github.com/fsouza/fake-gcs-server/pull/659#issue-1114081250

Let me know if that's enough information is enough for you.

We should probably document that endpoint in the README at some point.

<!-- gh-comment-id:1063525686 --> @fsouza commented on GitHub (Mar 10, 2022): @sergseven recently added an internal endpoint for updating some configuration parameters, aimed specifically at setting the external URL after starting the container with test containers, you can see a Java example in his PR: https://github.com/fsouza/fake-gcs-server/pull/659#issue-1114081250 Let me know if that's enough information is enough for you. We should probably document that endpoint in the README at some point.
Author
Owner

@majelbstoat commented on GitHub (Mar 10, 2022):

So, that looks like it might be helpful, thanks! But, at the moment, it looks like it can only be used to change the ExternalUrl.

https://github.com/fsouza/fake-gcs-server/blob/main/fakestorage/config.go

I can submit a patch for changing the public host, if that's acceptable?

<!-- gh-comment-id:1063540790 --> @majelbstoat commented on GitHub (Mar 10, 2022): So, that looks like it might be helpful, thanks! But, at the moment, it looks like it can only be used to change the ExternalUrl. https://github.com/fsouza/fake-gcs-server/blob/main/fakestorage/config.go I can submit a patch for changing the public host, if that's acceptable?
Author
Owner

@fsouza commented on GitHub (Mar 10, 2022):

@majelbstoat ohh sorry I misread your message. Yeah, that's acceptable, feel free to send the PR adding support for overriding the public host!

<!-- gh-comment-id:1063557312 --> @fsouza commented on GitHub (Mar 10, 2022): @majelbstoat ohh sorry I misread your message. Yeah, that's acceptable, feel free to send the PR adding support for overriding the public host!
Author
Owner

@majelbstoat commented on GitHub (Mar 10, 2022):

Cool, PR here: https://github.com/fsouza/fake-gcs-server/pull/712 :)

<!-- gh-comment-id:1063559506 --> @majelbstoat commented on GitHub (Mar 10, 2022): Cool, PR here: https://github.com/fsouza/fake-gcs-server/pull/712 :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/fake-gcs-server#120
No description provided.