Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cap-add/cap-drop #555

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ type ContainerRequest struct {
AlwaysPullImage bool // Always pull image
ImagePlatform string // ImagePlatform describes the platform which the image runs on.
Binds []string
ShmSize int64 // Amount of memory shared with the host (in bytes)
ShmSize int64 // Amount of memory shared with the host (in bytes)
CapAdd []string // Add Linux capabilities
CapDrop []string // Drop Linux capabilities
}

type (
Expand Down
2 changes: 2 additions & 0 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
NetworkMode: req.NetworkMode,
Resources: req.Resources,
ShmSize: req.ShmSize,
CapAdd: req.CapAdd,
CapDrop: req.CapDrop,
}

endpointConfigs := map[string]*network.EndpointSettings{}
Expand Down
34 changes: 34 additions & 0 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-units"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -2214,6 +2215,39 @@ func TestContainerWithReaperNetwork(t *testing.T) {
assert.NotNil(t, cnt.NetworkSettings.Networks[networks[1]])
}

func TestContainerCapAdd(t *testing.T) {
if providerType == ProviderPodman {
t.Skip("Rootless Podman does not support setting cap-add/cap-drop")
}

ctx := context.Background()

expected := "IPC_LOCK"

nginx, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
CapAdd: []string{expected},
},
Started: true,
})
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, nginx)

dockerClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
require.NoError(t, err)
defer dockerClient.Close()

containerID := nginx.GetContainerID()
resp, err := dockerClient.ContainerInspect(ctx, containerID)
require.NoError(t, err)

assert.Equal(t, strslice.StrSlice{expected}, resp.HostConfig.CapAdd)
}

func TestContainerRunningCheckingStatusCode(t *testing.T) {
ctx := context.Background()
req := ContainerRequest{
Expand Down