Skip to content

Commit 5562dbc

Browse files
authored
feat: Allow the container working directory to be specified (#1899)
* feat: Allow the container working directory to be specified in ContainerRequest * move WorkingDir field position in ContainerRequest struct
1 parent ef7e6bf commit 5562dbc

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ type ContainerRequest struct {
114114
WaitingFor wait.Strategy
115115
Name string // for specifying container name
116116
Hostname string
117+
WorkingDir string // specify the working directory of the container
117118
ExtraHosts []string // Deprecated: Use HostConfigModifier instead
118119
Privileged bool // For starting privileged container
119120
Networks []string // for specifying network names

docker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
999999
Cmd: req.Cmd,
10001000
Hostname: req.Hostname,
10011001
User: req.User,
1002+
WorkingDir: req.WorkingDir,
10021003
}
10031004

10041005
hostConfig := &container.HostConfig{

docker_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,34 @@ func TestEntrypoint(t *testing.T) {
10591059
terminateContainerOnEnd(t, ctx, c)
10601060
}
10611061

1062+
func TestWorkingDir(t *testing.T) {
1063+
/*
1064+
print the current working directory to ensure that
1065+
we can specify working directory in the
1066+
ContainerRequest and it will be used for the container
1067+
*/
1068+
1069+
ctx := context.Background()
1070+
1071+
req := ContainerRequest{
1072+
Image: "docker.io/alpine",
1073+
WaitingFor: wait.ForAll(
1074+
wait.ForLog("/var/tmp/test"),
1075+
),
1076+
Entrypoint: []string{"pwd"},
1077+
WorkingDir: "/var/tmp/test",
1078+
}
1079+
1080+
c, err := GenericContainer(ctx, GenericContainerRequest{
1081+
ProviderType: providerType,
1082+
ContainerRequest: req,
1083+
Started: true,
1084+
})
1085+
1086+
require.NoError(t, err)
1087+
terminateContainerOnEnd(t, ctx, c)
1088+
}
1089+
10621090
func ExampleDockerProvider_CreateContainer() {
10631091
ctx := context.Background()
10641092
req := ContainerRequest{

0 commit comments

Comments
 (0)