Skip to content

Commit 15549c6

Browse files
committed
feat(options): add WithProvider
The generic container allows to set a provider for a container but this is not exposed via the options. This commit adds this capability so that it is possible to set the provider to Podman on MacOS when the auto detection fails
1 parent 3835fa5 commit 15549c6

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

docs/features/common_functional_options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,13 @@ ctr, err := mymodule.Run(ctx, "docker.io/myservice:1.2.3",
437437

438438
If you need to prevent the container from being started after creation, you can use the `testcontainers.WithNoStart` option.
439439

440+
##### WithProvider
441+
442+
- Since <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.39.0"><span class="tc-version">:material-tag: v0.39.0</span></a>
443+
444+
If you need specify which provider to use to run the container, you can use the `testcontainers.WithProvider` option.
445+
Currently only `docker` or `podman` are supported.
446+
440447
#### Experimental Options
441448

442449
##### WithReuseByName

docs/features/common_functional_options_list.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ The following options are exposed by the `testcontainers` package.
6060
- [`CustomizeRequest`](/features/creating_container/#customizerequest) 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>
6161
- [`WithName`](/features/creating_container/#withname) Since <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.38.0"><span class="tc-version">:material-tag: v0.38.0</span></a>
6262
- [`WithNoStart`](/features/creating_container/#withnostart) Since <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.38.0"><span class="tc-version">:material-tag: v0.38.0</span></a>
63+
- [`WithProvider`](/features/creating_container/#withprovider) Since <a href="https://github.com/testcontainers/testcontainers-go/releases/tag/v0.39.0"><span class="tc-version">:material-tag: v0.39.0</span></a>
64+
6365

6466
### Experimental Options
6567

docs/system_requirements/using_podman.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ The reaper container needs to connect to the docker daemon to reap containers, s
4444
> systemctl --user start podman.socket
4545
```
4646

47+
## MacOS
48+
49+
Currently, in MacOS the autodetection of podman does not work as intended, which leads to Ryku failing at boot-up.
50+
In order to use testcontainer then either
51+
1. Disable Ryku (not recommended): see [here](../features/garbage_collector.md#ryuk)
52+
2. If you want to use Ryku then you need
53+
1. Run podman in rootful mode by running the following commands
54+
```bash
55+
podman machine stop
56+
podman machine set --rootful
57+
podman machine start
58+
```
59+
2. Add
60+
```
61+
ryuk.container.privileged=true
62+
```
63+
to `~/.testcontainers.properties`
64+
3. Use the `WithProvider` option when running your containers
65+
```go
66+
package some_test
67+
68+
import (
69+
"testing"
70+
71+
tc "github.com/testcontainers/testcontainers-go"
72+
)
73+
74+
func TestSomething(t *testing.T) {
75+
ctx := t.Context()
76+
ctr, err := tc.Run(ctx,
77+
"docker.io/myservice:1.2.3",
78+
tc.WithProvider(tc.ProviderPodman),
79+
)
80+
81+
// ...
82+
}
83+
```
84+
4785
## Fedora
4886

4987
`DOCKER_HOST` environment variable must be set

options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,12 @@ func WithFiles(files ...ContainerFile) CustomizeRequestOption {
537537
return nil
538538
}
539539
}
540+
541+
// WithProvider sets the provider type for a container
542+
func WithProvider(provider ProviderType) CustomizeRequestOption {
543+
return func(req *GenericContainerRequest) error {
544+
req.ProviderType = provider
545+
546+
return nil
547+
}
548+
}

options_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,3 +906,30 @@ func TestWithWaitStrategy(t *testing.T) {
906906
})
907907
})
908908
}
909+
910+
func TestWithProvider(t *testing.T) {
911+
t.Parallel()
912+
req := testcontainers.GenericContainerRequest{
913+
ContainerRequest: testcontainers.ContainerRequest{
914+
Image: "alpine",
915+
},
916+
}
917+
918+
t.Run("default", func(t *testing.T) {
919+
opt := testcontainers.WithProvider(testcontainers.ProviderDefault)
920+
require.NoError(t, opt.Customize(&req))
921+
require.Equal(t, testcontainers.ProviderDefault, req.ProviderType)
922+
})
923+
924+
t.Run("docker", func(t *testing.T) {
925+
opt := testcontainers.WithProvider(testcontainers.ProviderDocker)
926+
require.NoError(t, opt.Customize(&req))
927+
require.Equal(t, testcontainers.ProviderDocker, req.ProviderType)
928+
})
929+
930+
t.Run("podman", func(t *testing.T) {
931+
opt := testcontainers.WithProvider(testcontainers.ProviderPodman)
932+
require.NoError(t, opt.Customize(&req))
933+
require.Equal(t, testcontainers.ProviderPodman, req.ProviderType)
934+
})
935+
}

0 commit comments

Comments
 (0)