Merge pull request #570 from aluzzardi/docker-socket

docker socket forwarding support
This commit is contained in:
Andrea Luzzardi 2021-06-07 13:32:24 -07:00 committed by GitHub
commit 4f887b2295
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 4 deletions

View File

@ -114,7 +114,11 @@ func (c *Client) buildfn(ctx context.Context, st *state.State, env *environment.
// Setup solve options // Setup solve options
opts := bk.SolveOpt{ opts := bk.SolveOpt{
LocalDirs: localdirs, LocalDirs: localdirs,
Session: []session.Attachable{auth, secrets}, Session: []session.Attachable{
auth,
secrets,
solver.NewDockerSocketProvider(),
},
} }
// Call buildkit solver // Call buildkit solver

View File

@ -0,0 +1,62 @@
package solver
import (
"context"
"fmt"
"net"
"time"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/sshforward"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
const (
DockerSocketID = "docker.sock"
DockerSocketPath = "/var/run/docker.sock"
)
type DockerSocketProvider struct {
}
func NewDockerSocketProvider() session.Attachable {
return &DockerSocketProvider{}
}
func (sp *DockerSocketProvider) Register(server *grpc.Server) {
sshforward.RegisterSSHServer(server, sp)
}
func (sp *DockerSocketProvider) CheckAgent(ctx context.Context, req *sshforward.CheckAgentRequest) (*sshforward.CheckAgentResponse, error) {
id := sshforward.DefaultID
if req.ID != "" {
id = req.ID
}
if id != DockerSocketID {
return &sshforward.CheckAgentResponse{}, fmt.Errorf("invalid socket forward key %s", id)
}
return &sshforward.CheckAgentResponse{}, nil
}
func (sp *DockerSocketProvider) ForwardAgent(stream sshforward.SSH_ForwardAgentServer) error {
id := sshforward.DefaultID
opts, _ := metadata.FromIncomingContext(stream.Context()) // if no metadata continue with empty object
if v, ok := opts[sshforward.KeySSHID]; ok && len(v) > 0 && v[0] != "" {
id = v[0]
}
if id != DockerSocketID {
return fmt.Errorf("invalid socket forward key %s", id)
}
conn, err := net.DialTimeout("unix", DockerSocketPath, time.Second)
if err != nil {
return fmt.Errorf("failed to connect to %s: %w", DockerSocketPath, err)
}
defer conn.Close()
return sshforward.Copy(context.TODO(), conn, stream, nil)
}

View File

@ -157,7 +157,11 @@ func (s Solver) Export(ctx context.Context, st llb.State, img *dockerfile2llb.Im
opts := bk.SolveOpt{ opts := bk.SolveOpt{
Exports: []bk.ExportEntry{output}, Exports: []bk.ExportEntry{output},
Session: []session.Attachable{s.opts.Auth, s.opts.Secrets}, Session: []session.Attachable{
s.opts.Auth,
s.opts.Secrets,
NewDockerSocketProvider(),
},
} }
ch := make(chan *bk.SolveStatus) ch := make(chan *bk.SolveStatus)

View File

@ -106,6 +106,11 @@ setup() {
assert_output "secret=mySecret" assert_output "secret=mySecret"
} }
@test "compute: docker socket" {
skip "docker socket support disabled"
run "$DAGGER" compute "$TESTDIR"/compute/dockersocket
}
@test "compute: exclude" { @test "compute: exclude" {
"$DAGGER" up -w "$TESTDIR"/compute/exclude "$DAGGER" up -w "$TESTDIR"/compute/exclude
} }

View File

@ -0,0 +1,18 @@
package main
import (
"dagger.io/dagger/op"
"dagger.io/docker"
)
TestDockerSocket: #up: [
op.#Load & {
from: docker.#Client
},
op.#Exec & {
always: true
mount: "/var/run/docker.sock": "docker.sock"
args: ["docker", "info"]
},
]