Skip to content

Commit

Permalink
Adds --read-only and --mount flags to container run command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran authored and ryanmoran committed Jul 25, 2022
1 parent 6f0983f commit 75b5894
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
20 changes: 20 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ type DockerContainerRun struct {
publishPorts []string
tty bool
volumes []string
readOnly bool
mounts []string
}

func (r DockerContainerRun) WithEnv(env map[string]string) DockerContainerRun {
Expand Down Expand Up @@ -186,6 +188,16 @@ func (r DockerContainerRun) WithNetwork(network string) DockerContainerRun {
return r
}

func (r DockerContainerRun) WithReadOnly() DockerContainerRun {
r.readOnly = true
return r
}

func (r DockerContainerRun) WithMounts(mounts ...string) DockerContainerRun {
r.mounts = append(r.mounts, mounts...)
return r
}

func (r DockerContainerRun) Execute(imageID string) (Container, error) {
args := []string{"container", "run", "--detach"}

Expand Down Expand Up @@ -232,6 +244,14 @@ func (r DockerContainerRun) Execute(imageID string) (Container, error) {
args = append(args, "--volume", volume)
}

if r.readOnly {
args = append(args, "--read-only")
}

for _, mount := range r.mounts {
args = append(args, "--mount", mount)
}

args = append(args, imageID)

if r.direct {
Expand Down
35 changes: 31 additions & 4 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
return nil
}
})

it("sets all of the published ports in the run command", func() {
container, err := docker.Container.Run.
WithPublish("3000").
Expand Down Expand Up @@ -445,6 +446,7 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
}))
})
})

context("when given optional tty setting", func() {
it("sets the tty flag on the run command", func() {
container, err := docker.Container.Run.
Expand Down Expand Up @@ -555,10 +557,10 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
})
})

context("when given optionial volume setting", func() {
it("sets the volume flag on the run command", func() {
context("when given optional read-only setting", func() {
it("sets the read-only flag on the run command", func() {
container, err := docker.Container.Run.
WithVolume("/tmp/host-source:/tmp/dir-on-container:rw").
WithReadOnly().
Execute("some-image-id")

Expect(err).NotTo(HaveOccurred())
Expand All @@ -570,7 +572,32 @@ func testDocker(t *testing.T, context spec.G, it spec.S) {
Expect(executeArgs[0]).To(Equal([]string{
"container", "run",
"--detach",
"--volume", "/tmp/host-source:/tmp/dir-on-container:rw",
"--read-only",
"some-image-id",
}))
})
})

context("when given optional mount setting", func() {
it("sets the mount flag on the run command", func() {
container, err := docker.Container.Run.
WithMounts(
"type=tmpfs,destination=/tmp",
"type=bind,source=/my-local,destination=/local",
).
Execute("some-image-id")

Expect(err).NotTo(HaveOccurred())
Expect(container).To(Equal(occam.Container{
ID: "some-container-id",
}))

Expect(executeArgs).To(HaveLen(2))
Expect(executeArgs[0]).To(Equal([]string{
"container", "run",
"--detach",
"--mount", "type=tmpfs,destination=/tmp",
"--mount", "type=bind,source=/my-local,destination=/local",
"some-image-id",
}))
})
Expand Down

0 comments on commit 75b5894

Please sign in to comment.