Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/testcontainers/testcontainers-go/internal/config"
"github.com/testcontainers/testcontainers-go/internal/core"
"github.com/testcontainers/testcontainers-go/log"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down Expand Up @@ -94,6 +95,9 @@ func TestContainerWithHostNetworkOptions(t *testing.T) {
}

func TestContainerWithHostNetworkOptions_UseExposePortsFromImageConfigs(t *testing.T) {
t.Setenv("TESTCONTAINERS_AUTO_EXPOSE_PORTS", "true")
config.Reset()

ctx := context.Background()
gcr := GenericContainerRequest{
ContainerRequest: ContainerRequest{
Expand Down Expand Up @@ -1911,6 +1915,9 @@ func assertExtractedFiles(t *testing.T, ctx context.Context, container Container
}

func TestDockerProviderFindContainerByName(t *testing.T) {
t.Setenv("TESTCONTAINERS_AUTO_EXPOSE_PORTS", "true")
config.Reset()

ctx := context.Background()
provider, err := NewDockerProvider(WithLogger(log.TestLogger(t)))
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion docs/features/common_functional_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ctr, err = mymodule.Run(ctx, "docker.io/myservice:1.2.3", testcontainers.WithEnv

- Since <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.20.0"><span class="tc-version">:material-tag: v0.20.0</span></a>

If you need to set a different wait strategy for the container, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy.
If you need to set a different wait strategy for the container, replacing the existing one, you can use `testcontainers.WithWaitStrategy` with a valid wait strategy.

!!!info
The default deadline for the wait strategy is 60 seconds.
Expand Down
16 changes: 16 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"

"github.com/magiconair/properties"

"github.com/testcontainers/testcontainers-go/log"
)

const ReaperDefaultImage = "testcontainers/ryuk:0.13.0"
Expand Down Expand Up @@ -85,6 +87,11 @@ type Config struct {
//
// Environment variable: TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE
TestcontainersHost string `properties:"tc.host,default="`

// AutoExposePorts is a flag to enable or disable the automatic exposure of ports when no ports are explicitly exposed.
//
// Environment variable: TESTCONTAINERS_AUTO_EXPOSE_PORTS
AutoExposePorts bool `properties:"tc.auto.expose.ports,default=true"`
}

// }
Expand Down Expand Up @@ -141,6 +148,15 @@ func read() Config {
config.RyukConnectionTimeout = timeout
}

autoExposePortsEnv := readTestcontainersEnv("TESTCONTAINERS_AUTO_EXPOSE_PORTS")
if parseBool(autoExposePortsEnv) {
config.AutoExposePorts = autoExposePortsEnv == "true"
}

if config.AutoExposePorts {
log.Printf("⚠️ Testcontainers is configured to automatically expose ports from the Image definition, but this is deprecated and will be removed in a future version. Please set `tc.auto.expose.ports=false` in the testcontainers.properties file.")
}

return config
}

Expand Down
Loading
Loading