Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 5168e5b

Browse files
simonferquelzappy-shu
authored andcommitted
Ground work for non-root
Signed-off-by: Simon Ferquel <[email protected]>
1 parent 622f5d9 commit 5168e5b

File tree

12 files changed

+125
-41
lines changed

12 files changed

+125
-41
lines changed

Dockerfile.invocation-image

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ RUN make EXPERIMENTAL=${EXPERIMENTAL} bin/cnab-run
1515

1616
# local cnab invocation image
1717
FROM alpine:${ALPINE_VERSION} as invocation
18-
RUN apk add --no-cache ca-certificates
18+
RUN apk add --no-cache ca-certificates && adduser -S cnab
19+
USER cnab
1920
COPY --from=build /go/src/github.com/docker/app/bin/cnab-run /cnab/app/run
2021
WORKDIR /cnab/app
2122
CMD /cnab/app/run

Gopkg.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ required = ["github.com/wadey/gocovmerge"]
4444

4545
[[override]]
4646
name = "github.com/deislabs/duffle"
47-
branch = "master"
47+
source = "github.com/simonferquel/duffle"
48+
branch = "custom-container-config"
4849

4950
[[constraint]]
5051
name = "github.com/sirupsen/logrus"

e2e/.docker/.buildNodeID

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8162aa64cf9962b818007cfe7bb9ce9c4b14e7e5308c12847134c6574797e2c6

internal/commands/cnab.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"github.com/docker/cli/cli/context/docker"
45
"fmt"
56
"io/ioutil"
67
"os"
@@ -18,6 +19,8 @@ import (
1819
"github.com/docker/cli/cli/command"
1920
"github.com/docker/cli/cli/context/store"
2021
"github.com/docker/distribution/reference"
22+
"github.com/docker/docker/api/types/container"
23+
"github.com/docker/docker/api/types/mount"
2124
"github.com/pkg/errors"
2225
)
2326

@@ -76,13 +79,27 @@ func duffleHome() home.Home {
7679
}
7780

7881
// prepareDriver prepares a driver per the user's request.
79-
func prepareDriver(dockerCli command.Cli) (driver.Driver, error) {
82+
func prepareDriver(dockerCli command.Cli, bindLocalSocket bool) (driver.Driver, error) {
8083
driverImpl, err := driver.Lookup("docker")
8184
if err != nil {
8285
return driverImpl, err
8386
}
8487
if d, ok := driverImpl.(*driver.DockerDriver); ok {
8588
d.SetDockerCli(dockerCli)
89+
if bindLocalSocket {
90+
d.AddConfigurationOptions(func(config *container.Config, hostConfig *container.HostConfig) error {
91+
config.User = "0:0"
92+
mounts := []mount.Mount{
93+
{
94+
Type: mount.TypeBind,
95+
Source: "/var/run/docker.sock",
96+
Target: "/var/run/docker.sock",
97+
},
98+
}
99+
hostConfig.Mounts = mounts
100+
return nil
101+
})
102+
}
86103
}
87104

88105
// Load any driver-specific config out of the environment.
@@ -161,3 +178,30 @@ func resolveBundle(dockerCli command.Cli, name string, pullRef bool, insecureReg
161178
}
162179
return nil, fmt.Errorf("could not resolve bundle %q", name)
163180
}
181+
182+
func requiresBindMount(targetContextName string, targetOrchestrator string, dockerCli command.Cli) (bool, error){
183+
if targetOrchestrator == "kubernetes"{
184+
return false, nil
185+
}
186+
ctxMeta, err := dockerCli.ContextStore().GetContextMetadata(targetContextName)
187+
if err != nil{
188+
return false, err
189+
}
190+
dockerCtx, err := command.GetDockerContext(ctxMeta)
191+
if err != nil{
192+
return false, err
193+
}
194+
if dockerCtx.StackOrchestrator == command.OrchestratorKubernetes{
195+
return false, nil
196+
}
197+
dockerEndpoint, err := docker.EndpointFromContext(ctxMeta)
198+
if err != nil{
199+
return false, err
200+
}
201+
host := dockerEndpoint.Host
202+
switch host{
203+
case "", "unix:///var/run/docker.sock", "npipe:////./pipe/docker_engine":
204+
return true, nil
205+
}
206+
return false, nil
207+
}

internal/commands/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) erro
3939
if err != nil {
4040
return err
4141
}
42-
driverImpl, err := prepareDriver(dockerCli)
42+
driverImpl, err := prepareDriver(dockerCli, false)
4343
if err != nil {
4444
return err
4545
}

internal/commands/install.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ func runInstall(dockerCli command.Cli, appname string, opts installOptions) erro
7373
return errors.New("with-registry-auth is not supported at the moment")
7474
}
7575
targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext())
76-
76+
doBindMount, err := requiresBindMount(targetContext, opts.orchestrator, dockerCli)
77+
if err != nil {
78+
return err
79+
}
7780
bndl, err := resolveBundle(dockerCli, appname, opts.pull, opts.insecureRegistries)
7881
if err != nil {
7982
return err
@@ -95,7 +98,7 @@ func runInstall(dockerCli command.Cli, appname string, opts installOptions) erro
9598
return err
9699
}
97100

98-
driverImpl, err := prepareDriver(dockerCli)
101+
driverImpl, err := prepareDriver(dockerCli, doBindMount)
99102
if err != nil {
100103
return err
101104
}

internal/commands/status.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,15 @@ func runStatus(dockerCli command.Cli, claimName string, opts credentialOptions)
3838
return err
3939
}
4040
targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext())
41-
42-
driverImpl, err := prepareDriver(dockerCli)
41+
var specifiedOrchestrator string
42+
if rawOrchestrator, ok := c.Parameters["docker.orchestrator"]; ok {
43+
specifiedOrchestrator = rawOrchestrator.(string)
44+
}
45+
doBindMounts, err := requiresBindMount(targetContext, specifiedOrchestrator, dockerCli)
46+
if err != nil {
47+
return err
48+
}
49+
driverImpl, err := prepareDriver(dockerCli, doBindMounts)
4350
if err != nil {
4451
return err
4552
}

internal/commands/uninstall.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ func runUninstall(dockerCli command.Cli, claimName string, opts credentialOption
3939
}
4040
targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext())
4141

42-
driverImpl, err := prepareDriver(dockerCli)
42+
var specifiedOrchestrator string
43+
if rawOrchestrator, ok := c.Parameters["docker.orchestrator"]; ok {
44+
specifiedOrchestrator = rawOrchestrator.(string)
45+
}
46+
doBindMounts, err := requiresBindMount(targetContext, specifiedOrchestrator, dockerCli)
47+
if err != nil {
48+
return err
49+
}
50+
driverImpl, err := prepareDriver(dockerCli, doBindMounts)
4351
if err != nil {
4452
return err
4553
}

internal/commands/upgrade.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,32 @@ func runUpgrade(dockerCli command.Cli, installationName string, opts upgradeOpti
5555
}
5656
c.Bundle = b
5757
}
58-
driverImpl, err := prepareDriver(dockerCli)
58+
c.Parameters, err = mergeBundleParameters(c.Bundle,
59+
withFileParameters(opts.parametersFiles),
60+
withCommandLineParameters(opts.overrides),
61+
)
5962
if err != nil {
6063
return err
6164
}
62-
creds, err := prepareCredentialSet(targetContext, dockerCli.ContextStore(), c.Bundle, opts.credentialsets)
65+
var specifiedOrchestrator string
66+
if rawOrchestrator, ok := c.Parameters["docker.orchestrator"]; ok {
67+
specifiedOrchestrator = rawOrchestrator.(string)
68+
}
69+
doBindMounts, err := requiresBindMount(targetContext, specifiedOrchestrator, dockerCli)
6370
if err != nil {
6471
return err
6572
}
66-
if err := credentials.Validate(creds, c.Bundle.Credentials); err != nil {
73+
driverImpl, err := prepareDriver(dockerCli, doBindMounts)
74+
if err != nil {
6775
return err
6876
}
69-
70-
c.Parameters, err = mergeBundleParameters(c.Bundle,
71-
withFileParameters(opts.parametersFiles),
72-
withCommandLineParameters(opts.overrides),
73-
)
77+
creds, err := prepareCredentialSet(targetContext, dockerCli.ContextStore(), c.Bundle, opts.credentialsets)
7478
if err != nil {
7579
return err
7680
}
77-
81+
if err := credentials.Validate(creds, c.Bundle.Credentials); err != nil {
82+
return err
83+
}
7884
u := &action.Upgrade{
7985
Driver: driverImpl,
8086
}

0 commit comments

Comments
 (0)