mirror of
https://github.com/moby/buildkit.git
synced 2026-08-08 08:40:45 +00:00
dockerfile: add support for RUN --mount=type=ssh
* Needs to be compiled with `dfrunmount dfssh` * Implemented options: * `type`(required): needs to be `ssh` * `target`(optional): the socket path in the container * `id`(optional): id Test script: #!/bin/bash set -exu -o pipefail REF=localhost:5000/dfssh:latest ssh-add -l sudo buildctl build --frontend=dockerfile.v0 --local context=. --local dockerfile=frontend/dockerfile/cmd/dockerfile-frontend \ --frontend-opt "build-arg:BUILDTAGS=dfrunmount dfssh" \ --exporter=image --exporter-opt name=$REF --exporter-opt push=true mkdir -p /tmp/foo cd /tmp/foo cat << EOF > Dockerfile # syntax=$REF FROM alpine RUN apk add --no-cache openssh-client RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts RUN --mount=type=ssh ssh git@gitlab.com # "Welcome to GitLab, @GITLAB_USERNAME_ASSOCIATED_WITH_SSHKEY" should be printed here EOF sudo buildctl build --ssh default=$SSH_AUTH_SOCK --progress=plain --frontend=dockerfile.v0 --local context=. --local dockerfile=. Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
@@ -496,6 +496,12 @@ func SSHID(id string) SSHOption {
|
||||
})
|
||||
}
|
||||
|
||||
func SSHSocketTarget(target string) SSHOption {
|
||||
return sshOptionFunc(func(si *SSHInfo) {
|
||||
si.Target = target
|
||||
})
|
||||
}
|
||||
|
||||
func SSHSocketOpt(target string, uid, gid, mode int) SSHOption {
|
||||
return sshOptionFunc(func(si *SSHInfo) {
|
||||
si.Target = target
|
||||
|
||||
13
frontend/dockerfile/dockerfile2llb/convert_nossh.go
Normal file
13
frontend/dockerfile/dockerfile2llb/convert_nossh.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// +build dfrunmount,!dfssh
|
||||
|
||||
package dockerfile2llb
|
||||
|
||||
import (
|
||||
"github.com/moby/buildkit/client/llb"
|
||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func dispatchSSH(m *instructions.Mount) (llb.RunOption, error) {
|
||||
return nil, errors.Errorf("ssh mounts not allowed")
|
||||
}
|
||||
@@ -65,6 +65,14 @@ func dispatchRunMounts(d *dispatchState, c *instructions.RunCommand, sources []*
|
||||
out = append(out, secret)
|
||||
continue
|
||||
}
|
||||
if mount.Type == instructions.MountTypeSSH {
|
||||
ssh, err := dispatchSSH(mount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, ssh)
|
||||
continue
|
||||
}
|
||||
if mount.ReadOnly {
|
||||
mountOpts = append(mountOpts, llb.Readonly)
|
||||
}
|
||||
|
||||
27
frontend/dockerfile/dockerfile2llb/convert_ssh.go
Normal file
27
frontend/dockerfile/dockerfile2llb/convert_ssh.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// +build dfssh dfextall
|
||||
|
||||
package dockerfile2llb
|
||||
|
||||
import (
|
||||
"github.com/moby/buildkit/client/llb"
|
||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func dispatchSSH(m *instructions.Mount) (llb.RunOption, error) {
|
||||
if m.Source != "" {
|
||||
return nil, errors.Errorf("ssh does not support source")
|
||||
}
|
||||
opts := []llb.SSHOption{llb.SSHID(m.CacheID)}
|
||||
|
||||
if m.Target != "" {
|
||||
// TODO(AkihiroSuda): support specifying permission bits
|
||||
opts = append(opts, llb.SSHSocketTarget(m.Target))
|
||||
}
|
||||
|
||||
if !m.Required {
|
||||
opts = append(opts, llb.SSHOptional)
|
||||
}
|
||||
|
||||
return llb.AddSSHSocket(opts...), nil
|
||||
}
|
||||
7
frontend/dockerfile/instructions/commands_nossh.go
Normal file
7
frontend/dockerfile/instructions/commands_nossh.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// +build !dfssh,!dfextall
|
||||
|
||||
package instructions
|
||||
|
||||
func isSSHMountsSupported() bool {
|
||||
return false
|
||||
}
|
||||
@@ -14,12 +14,14 @@ const MountTypeBind = "bind"
|
||||
const MountTypeCache = "cache"
|
||||
const MountTypeTmpfs = "tmpfs"
|
||||
const MountTypeSecret = "secret"
|
||||
const MountTypeSSH = "ssh"
|
||||
|
||||
var allowedMountTypes = map[string]struct{}{
|
||||
MountTypeBind: {},
|
||||
MountTypeCache: {},
|
||||
MountTypeTmpfs: {},
|
||||
MountTypeSecret: {},
|
||||
MountTypeSSH: {},
|
||||
}
|
||||
|
||||
const MountSharingShared = "shared"
|
||||
@@ -47,6 +49,11 @@ func isValidMountType(s string) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if s == "ssh" {
|
||||
if !isSSHMountsSupported() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
_, ok := allowedMountTypes[s]
|
||||
return ok
|
||||
}
|
||||
|
||||
7
frontend/dockerfile/instructions/commands_ssh.go
Normal file
7
frontend/dockerfile/instructions/commands_ssh.go
Normal file
@@ -0,0 +1,7 @@
|
||||
// +build dfssh dfextall
|
||||
|
||||
package instructions
|
||||
|
||||
func isSSHMountsSupported() bool {
|
||||
return true
|
||||
}
|
||||
@@ -16,4 +16,5 @@ docker run --rm $iid go build ./frontend/dockerfile/cmd/dockerfile-frontend
|
||||
|
||||
docker run --rm $iid go build -tags dfrunmount ./frontend/dockerfile/cmd/dockerfile-frontend
|
||||
docker run --rm $iid go build -tags "dfrunmount dfsecrets" ./frontend/dockerfile/cmd/dockerfile-frontend
|
||||
docker run --rm $iid go build -tags "dfrunmount dfssh" ./frontend/dockerfile/cmd/dockerfile-frontend
|
||||
docker run --rm $iid go build -tags dfextall ./frontend/dockerfile/cmd/dockerfile-frontend
|
||||
|
||||
Reference in New Issue
Block a user