Skip to content

Commit

Permalink
add support of privileged attribut in service.build section
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
  • Loading branch information
glours committed Dec 21, 2022
1 parent 0307c16 commit 5f6660c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/compose/build.go
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/moby/buildkit/session/secrets/secretsprovider"
"github.com/moby/buildkit/session/sshforward/sshprovider"
"github.com/moby/buildkit/util/entitlements"
specs "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -71,7 +72,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
if err != nil {
return err
}

for _, image := range service.Build.CacheFrom {
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
Type: "registry",
Expand Down Expand Up @@ -258,6 +258,10 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
if len(service.Build.Tags) > 0 {
tags = append(tags, service.Build.Tags...)
}
var allow []entitlements.Entitlement
if service.Build.Privileged {
allow = append(allow, entitlements.EntitlementSecurityInsecure)
}

imageLabels := getImageBuildLabels(project, service)

Expand All @@ -279,6 +283,7 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
NetworkMode: service.Build.Network,
ExtraHosts: service.Build.ExtraHosts.AsList(),
Session: sessionConfig,
Allow: allow,
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/compose/build_classic.go
Expand Up @@ -30,6 +30,7 @@ import (
buildx "github.com/docker/buildx/build"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/compose/v2/pkg/utils"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/builder/remotecontext/urlutil"
"github.com/docker/docker/pkg/archive"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/docker/docker/pkg/progress"
"github.com/docker/docker/pkg/streamformatter"
"github.com/hashicorp/go-multierror"
"github.com/moby/buildkit/util/entitlements"
"github.com/pkg/errors"

"github.com/docker/compose/v2/pkg/api"
Expand Down Expand Up @@ -92,6 +94,9 @@ func (s *composeService) doBuildClassicSimpleImage(ctx context.Context, options
if len(options.Platforms) > 1 {
return "", errors.Errorf("this builder doesn't support multi-arch build, set DOCKER_BUILDKIT=1 to use multi-arch builder")
}
if utils.Contains(options.Allow, entitlements.EntitlementSecurityInsecure) {
return "", errors.Errorf("this builder doesn't support privileged mode, set DOCKER_BUILDKIT=1 to use builder supporting privileged mode")
}

if options.Labels == nil {
options.Labels = make(map[string]string)
Expand Down
33 changes: 33 additions & 0 deletions pkg/e2e/build_test.go
Expand Up @@ -338,6 +338,26 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {

}

func TestBuildPrivileged(t *testing.T) {
c := NewParallelCLI(t)

// declare builder
result := c.RunDockerCmd(t, "buildx", "create", "--name", "build-privileged", "--use", "--bootstrap", "--buildkitd-flags",
"'--allow-insecure-entitlement security.insecure'")
assert.NilError(t, result.Error)

t.Cleanup(func() {
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/privileged", "down")
_ = c.RunDockerCmd(t, "buildx", "rm", "-f", "build-privileged")
})

t.Run("use build privileged mode to run insecure build command", func(t *testing.T) {
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/privileged", "build")
assert.NilError(t, res.Error, res.Stderr())

})
}

func TestBuildPlatformsStandardErrors(t *testing.T) {
c := NewParallelCLI(t)

Expand Down Expand Up @@ -380,4 +400,17 @@ func TestBuildPlatformsStandardErrors(t *testing.T) {
Err: `DOCKER_DEFAULT_PLATFORM "windows/amd64" value should be part of the service.build.platforms: ["linux/amd64" "linux/arm64"]`,
})
})

t.Run("no privileged support with Classic Builder", func(t *testing.T) {
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/privileged", "build")

res := icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
})
res.Assert(t, icmd.Expected{
ExitCode: 1,
Err: "this builder doesn't support privileged mode, set DOCKER_BUILDKIT=1 to use builder supporting privileged mode",
})
})

}
16 changes: 16 additions & 0 deletions pkg/e2e/fixtures/build-test/privileged/Dockerfile
@@ -0,0 +1,16 @@
# Copyright 2020 Docker Compose CLI authors

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM alpine
RUN --security=insecure cat /proc/self/status | grep CapEff
5 changes: 5 additions & 0 deletions pkg/e2e/fixtures/build-test/privileged/compose.yaml
@@ -0,0 +1,5 @@
services:
privileged-service:
build:
context: .
privileged: true

0 comments on commit 5f6660c

Please sign in to comment.