Skip to content

Commit fd353ff

Browse files
gloursndeloof
authored andcommitted
add support of privileged attribut in service.build section
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 0307c16 commit fd353ff

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

pkg/compose/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/moby/buildkit/session/auth/authprovider"
3434
"github.com/moby/buildkit/session/secrets/secretsprovider"
3535
"github.com/moby/buildkit/session/sshforward/sshprovider"
36+
"github.com/moby/buildkit/util/entitlements"
3637
specs "github.com/opencontainers/image-spec/specs-go/v1"
3738

3839
"github.com/docker/compose/v2/pkg/api"
@@ -71,7 +72,6 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
7172
if err != nil {
7273
return err
7374
}
74-
7575
for _, image := range service.Build.CacheFrom {
7676
buildOptions.CacheFrom = append(buildOptions.CacheFrom, bclient.CacheOptionsEntry{
7777
Type: "registry",
@@ -258,6 +258,10 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
258258
if len(service.Build.Tags) > 0 {
259259
tags = append(tags, service.Build.Tags...)
260260
}
261+
var allow []entitlements.Entitlement
262+
if service.Build.Privileged {
263+
allow = append(allow, entitlements.EntitlementSecurityInsecure)
264+
}
261265

262266
imageLabels := getImageBuildLabels(project, service)
263267

@@ -279,6 +283,7 @@ func (s *composeService) toBuildOptions(project *types.Project, service types.Se
279283
NetworkMode: service.Build.Network,
280284
ExtraHosts: service.Build.ExtraHosts.AsList(),
281285
Session: sessionConfig,
286+
Allow: allow,
282287
}, nil
283288
}
284289

pkg/compose/build_classic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
buildx "github.com/docker/buildx/build"
3131
"github.com/docker/cli/cli"
3232
"github.com/docker/cli/cli/command/image/build"
33+
"github.com/docker/compose/v2/pkg/utils"
3334
dockertypes "github.com/docker/docker/api/types"
3435
"github.com/docker/docker/builder/remotecontext/urlutil"
3536
"github.com/docker/docker/pkg/archive"
@@ -38,6 +39,7 @@ import (
3839
"github.com/docker/docker/pkg/progress"
3940
"github.com/docker/docker/pkg/streamformatter"
4041
"github.com/hashicorp/go-multierror"
42+
"github.com/moby/buildkit/util/entitlements"
4143
"github.com/pkg/errors"
4244

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

96101
if options.Labels == nil {
97102
options.Labels = make(map[string]string)

pkg/e2e/build_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,27 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
338338

339339
}
340340

341+
func TestBuildPrivileged(t *testing.T) {
342+
c := NewParallelCLI(t)
343+
344+
// declare builder
345+
result := c.RunDockerCmd(t, "buildx", "create", "--name", "build-privileged", "--use", "--bootstrap", "--buildkitd-flags",
346+
`'--allow-insecure-entitlement=security.insecure'`)
347+
assert.NilError(t, result.Error)
348+
349+
t.Cleanup(func() {
350+
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/privileged", "down")
351+
_ = c.RunDockerCmd(t, "buildx", "rm", "-f", "build-privileged")
352+
})
353+
354+
t.Run("use build privileged mode to run insecure build command", func(t *testing.T) {
355+
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/privileged", "build")
356+
assert.NilError(t, res.Error, res.Stderr())
357+
res.Assert(t, icmd.Expected{Out: "CapEff:\t0000003fffffffff"})
358+
359+
})
360+
}
361+
341362
func TestBuildPlatformsStandardErrors(t *testing.T) {
342363
c := NewParallelCLI(t)
343364

@@ -380,4 +401,17 @@ func TestBuildPlatformsStandardErrors(t *testing.T) {
380401
Err: `DOCKER_DEFAULT_PLATFORM "windows/amd64" value should be part of the service.build.platforms: ["linux/amd64" "linux/arm64"]`,
381402
})
382403
})
404+
405+
t.Run("no privileged support with Classic Builder", func(t *testing.T) {
406+
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/privileged", "build")
407+
408+
res := icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
409+
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
410+
})
411+
res.Assert(t, icmd.Expected{
412+
ExitCode: 1,
413+
Err: "this builder doesn't support privileged mode, set DOCKER_BUILDKIT=1 to use builder supporting privileged mode",
414+
})
415+
})
416+
383417
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# syntax = docker/dockerfile:experimental
2+
3+
4+
# Copyright 2020 Docker Compose CLI authors
5+
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
FROM alpine
19+
RUN --security=insecure cat /proc/self/status | grep CapEff
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
privileged-service:
3+
build:
4+
context: .
5+
privileged: true

0 commit comments

Comments
 (0)