Skip to content

Commit

Permalink
support local output in build
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
  • Loading branch information
ehazlett committed Sep 27, 2021
1 parent 0deac64 commit e78e41c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ To build an image using BuildKit:
# nerdctl run -it --rm foo
```

To build and send output to a local directory using BuildKit:
```console
# nerdctl build -o type=local,dest=. /some-dockerfile-directory
```

To run containers from `docker-compose.yaml`:
```console
# nerdctl compose -f ./examples/compose-wordpress/docker-compose.yaml up
Expand Down Expand Up @@ -119,7 +124,7 @@ Major:
- [Running encrypted images using ocicrypt (imgcrypt)](./docs/ocicrypt.md)

Minor:
- Namespacing: `nerdctl --namespace=<NS> ps` .
- Namespacing: `nerdctl --namespace=<NS> ps` .
(NOTE: All Kubernetes containers are in the `k8s.io` containerd namespace regardless to Kubernetes namespaces)
- Exporting Docker/OCI dual-format archives: `nerdctl save` .
- Importing OCI archives as well as Docker archives: `nerdctl load` .
Expand Down Expand Up @@ -261,7 +266,7 @@ It does not necessarily mean that the corresponding features are missing in cont
<!-- END doctoc generated TOC please keep comment here to allow auto update -->



## Run & Exec
### :whale: nerdctl run
Run a command in a new container.
Expand Down Expand Up @@ -505,7 +510,7 @@ Flags:
Unimplemented `docker inspect` flags: `--size`, `--type`

### :whale: nerdctl logs
Fetch the logs of a container.
Fetch the logs of a container.

:warning: Currently, only containers created with `nerdctl run -d` are supported.

Expand Down Expand Up @@ -718,7 +723,7 @@ Create a network
Usage: `nerdctl network create [OPTIONS] NETWORK`

Flags:
- :whale: `--subnet`: Subnet in CIDR format that represents a network segment, e.g. "10.5.0.0/16"
- :whale: `--subnet`: Subnet in CIDR format that represents a network segment, e.g. "10.5.0.0/16"
- :whale: `--label`: Set metadata on a network

Unimplemented `docker network create` flags: `--attachable`, `--aux-address`, `--config-from`, `--config-only`, `--driver`, `--gateway`, `--ingress`, `--internal`, `--ip-range`, `--ipam-driver`, `--ipam-opt`, `--ipv6`, `--opt`, `--scope`
Expand Down
18 changes: 15 additions & 3 deletions cmd/nerdctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"fmt"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -65,6 +66,11 @@ var buildCommand = &cli.Command{
Name: "no-cache",
Usage: "Do not use cache when building the image",
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Value: "type=docker",
},
&cli.StringFlag{
Name: "progress",
Usage: "Set type of progress output (auto, plain, tty). Use plain to show container output",
Expand Down Expand Up @@ -106,8 +112,10 @@ func buildAction(clicontext *cli.Context) error {
return err
}

if err = loadImage(buildctlStdout, clicontext); err != nil {
return err
if !isLocalBuild(clicontext) {
if err = loadImage(buildctlStdout, clicontext); err != nil {
return err
}
}

if err = buildctlCmd.Wait(); err != nil {
Expand All @@ -131,7 +139,7 @@ func generateBuildctlArgs(clicontext *cli.Context) (string, []string, error) {
return "", nil, err
}

output := "--output=type=docker"
output := fmt.Sprintf("--output=%s", clicontext.String("output"))
if tagSlice := strutil.DedupeStrSlice(clicontext.StringSlice("tag")); len(tagSlice) > 0 {
if len(tagSlice) > 1 {
return "", nil, errors.Errorf("specifying multiple -t is not supported yet")
Expand Down Expand Up @@ -180,3 +188,7 @@ func generateBuildctlArgs(clicontext *cli.Context) (string, []string, error) {

return buildctlBinary, buildctlArgs, nil
}

func isLocalBuild(clicontext *cli.Context) (bool, error) {
return len(strutil.DedupeStrSlice(clicontext.StringSlice("tag"))) > 0
}
26 changes: 26 additions & 0 deletions cmd/nerdctl/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ CMD ["echo", "nerdctl-build-test-string"]
base.Cmd("run", "--rm", imageName).AssertOutContains("nerdctl-build-test-string")
}

func TestBuildLocal(t *testing.T) {
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
const testFileName = "nerdctl-build-test"
outputDir, err := os.MkdirTemp("", "nerdctl-build-test-")
assert.NilError(t, err)
defer os.RemoveAll(outputDir)

dockerfile := fmt.Sprintf(`FROM scratch
COPY %s /`,
testFileName)

buildCtx, err := createBuildContext(dockerfile)
assert.NilError(t, err)
defer os.RemoveAll(buildCtx)

if err := ioutil.WriteFile(filepath.Join(buildCtx, testFileName), []byte("nerdctl-test"), 0644); err != nil {
t.Fatal(err)
}

base.Cmd("build", "-o", fmt.Sprintf("type=local,dest=%s", outputDir), buildCtx).AssertOK()
if _, err := os.Stat(filepath.Join(outputDir, testFileName)); err != nil {
t.Fatal(err)
}
}

func createBuildContext(dockerfile string) (string, error) {
tmpDir, err := ioutil.TempDir("", "nerdctl-build-test")
if err != nil {
Expand Down

0 comments on commit e78e41c

Please sign in to comment.