From 8abeffde590e3aea1a1468ffa4b2a6e7fbee00d6 Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Wed, 7 Jun 2017 15:32:02 +0100 Subject: [PATCH] Add Docker output format This will add a Dockerfile which will build the contents into an image and then call `tinit` to start it. This is fairly experimental, but is a prototype for other non LinuxKit outputs. The container will need to run as `privileged` as `runc` needs quite a few capabilities and `containerd` needs to mount. Signed-off-by: Justin Cormack --- cmd/moby/build.go | 47 +++++++++++++++++++++++++++++++++++++++++--- cmd/moby/linuxkit.go | 2 +- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cmd/moby/build.go b/cmd/moby/build.go index 66b91a0da..93da8d180 100644 --- a/cmd/moby/build.go +++ b/cmd/moby/build.go @@ -35,7 +35,37 @@ func (o *outputList) Set(value string) error { } var streamable = map[string]bool{ - "tar": true, + "docker": true, + "tar": true, +} + +type addFun func(*tar.Writer) error + +const dockerfile = ` +FROM scratch + +COPY . ./ +RUN rm -f Dockerfile + +ENTRYPOINT ["/sbin/tini", "--", "/bin/rc.init"] +` + +var additions = map[string]addFun{ + "docker": func(tw *tar.Writer) error { + log.Infof(" Adding Dockerfile") + hdr := &tar.Header{ + Name: "Dockerfile", + Mode: 0644, + Size: int64(len(dockerfile)), + } + if err := tw.WriteHeader(hdr); err != nil { + return err + } + if _, err := tw.Write([]byte(dockerfile)); err != nil { + return err + } + return nil + }, } // Process the build arguments and execute build @@ -127,6 +157,7 @@ func build(args []string) { } var outputFile *os.File + var addition addFun if *buildOutputFile != "" { if len(buildOut) > 1 { log.Fatal("The -output option can only be specified when generating a single output format") @@ -150,6 +181,7 @@ func build(args []string) { } defer outputFile.Close() } + addition = additions[buildOut[0]] } size, err := getDiskSizeMB(*buildSize) @@ -194,7 +226,7 @@ func build(args []string) { buf = new(bytes.Buffer) w = buf } - buildInternal(moby, w, *buildPull) + buildInternal(moby, w, *buildPull, addition) if outputFile == nil { image := buf.Bytes() @@ -272,7 +304,7 @@ func enforceContentTrust(fullImageName string, config *TrustConfig) bool { // Perform the actual build process // TODO return error not panic -func buildInternal(m Moby, w io.Writer, pull bool) { +func buildInternal(m Moby, w io.Writer, pull bool, addition addFun) { iw := tar.NewWriter(w) if m.Kernel.Image != "" { @@ -341,6 +373,15 @@ func buildInternal(m Moby, w io.Writer, pull bool) { if err != nil { log.Fatalf("failed to add filesystem parts: %v", err) } + + // add anything additional for this output type + if addition != nil { + err = addition(iw) + if err != nil { + log.Fatalf("Failed to add additional files") + } + } + err = iw.Close() if err != nil { log.Fatalf("initrd close error: %v", err) diff --git a/cmd/moby/linuxkit.go b/cmd/moby/linuxkit.go index e4d2a9f1b..03169ef8a 100644 --- a/cmd/moby/linuxkit.go +++ b/cmd/moby/linuxkit.go @@ -60,7 +60,7 @@ func ensureLinuxkitImage(name string) error { } // TODO pass through --pull to here buf := new(bytes.Buffer) - buildInternal(m, buf, false) + buildInternal(m, buf, false, nil) image := buf.Bytes() kernel, initrd, cmdline, err := tarToInitrd(image) if err != nil {