Skip to content

Commit

Permalink
fix: container binds syntax (#2899)
Browse files Browse the repository at this point in the history
The container binds syntax is `host-src:container-dest[:options]` or
`volume-name:container-dest[:options]` where `options` is a comma
separated list of: `nocopy` `[ro|rw]`, `[z|Z]` or
`[[r]shared|[r]slave|[r]private]`. See `HostConfig.Binds` of the Create
a container request[1]

[1] https://docs.docker.com/reference/api/engine/version/v1.47/#tag/Container/operation/ContainerCreate
  • Loading branch information
zregvart authored Dec 4, 2024
1 parent c28f0f8 commit 72be139
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (c *ContainerRequest) validateMounts() error {
if len(hostConfig.Binds) > 0 {
for _, bind := range hostConfig.Binds {
parts := strings.Split(bind, ":")
if len(parts) != 2 {
if len(parts) != 2 && len(parts) != 3 {
return fmt.Errorf("%w: %s", ErrInvalidBindMount, bind)
}
targetPath := parts[1]
Expand Down
26 changes: 24 additions & 2 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,33 @@ func Test_ContainerValidation(t *testing.T) {
},
{
Name: "Invalid bind mount",
ExpectedError: "invalid bind mount: /data:/data:/data",
ExpectedError: "invalid bind mount: /data:/data:a:b",
ContainerRequest: testcontainers.ContainerRequest{
Image: "redis:latest",
HostConfigModifier: func(hc *container.HostConfig) {
hc.Binds = []string{"/data:/data:/data"}
hc.Binds = []string{"/data:/data:a:b"}
},
},
},
{
Name: "bind-options/provided",
ContainerRequest: testcontainers.ContainerRequest{
Image: "redis:latest",
HostConfigModifier: func(hc *container.HostConfig) {
hc.Binds = []string{
"/a:/a:nocopy",
"/b:/b:ro",
"/c:/c:rw",
"/d:/d:z",
"/e:/e:Z",
"/f:/f:shared",
"/g:/g:rshared",
"/h:/h:slave",
"/i:/i:rslave",
"/j:/j:private",
"/k:/k:rprivate",
"/l:/l:ro,z,shared",
}
},
},
},
Expand Down

0 comments on commit 72be139

Please sign in to comment.