| 1 |
OCI Image Builder |
| 2 |
========================== |
| 3 |
|
| 4 |
[](https://godoc.org/github.com/openshift/imagebuilder) |
| 5 |
[](https://app.travis-ci.com/github/openshift/imagebuilder) |
| 6 |
[](http://webchat.freenode.net/?channels=%23openshift-dev) |
| 7 |
|
| 8 |
Please test your images (and add to our conformance suite)! |
| 9 |
|
| 10 |
This library supports using the Dockerfile syntax to build OCI & Docker |
| 11 |
compatible images, without invoking a container build command such as `buildah bud` or `docker build`. It is intended to give |
| 12 |
clients more control over how they build container images, including: |
| 13 |
|
| 14 |
* Instead of building one layer per line, run all instructions in the |
| 15 |
same container |
| 16 |
* Set HostConfig settings like network and memory controls that |
| 17 |
are not available when running container builds |
| 18 |
* Mount external files into the build that are not persisted as part of |
| 19 |
the final image (i.e. "secrets") |
| 20 |
* If there are no RUN commands in the Dockerfile, the container is created |
| 21 |
and committed, but never started. |
| 22 |
|
| 23 |
The final image should be 99.9% compatible with regular container builds, |
| 24 |
but bugs are always possible. |
| 25 |
|
| 26 |
Future goals include: |
| 27 |
|
| 28 |
* Output OCI compatible images |
| 29 |
* Support other container execution engines, like runc or rkt |
| 30 |
* Better conformance testing |
| 31 |
* Windows support |
| 32 |
|
| 33 |
## Install and Run |
| 34 |
|
| 35 |
To download and install the library and the binary, set up a Golang build environment and with `GOPATH` set run: |
| 36 |
|
| 37 |
``` |
| 38 |
$ go install github.com/openshift/imagebuilder/cmd/imagebuilder@latest |
| 39 |
``` |
| 40 |
|
| 41 |
The included command line takes one argument, a path to a directory containing a Dockerfile. The `-t` option |
| 42 |
can be used to specify an image to tag as: |
| 43 |
|
| 44 |
``` |
| 45 |
$ imagebuilder [-t TAG] DIRECTORY |
| 46 |
``` |
| 47 |
|
| 48 |
To mount a file into the image for build that will not be present in the final output image, run: |
| 49 |
|
| 50 |
``` |
| 51 |
$ imagebuilder --mount ~/secrets/private.key:/etc/keys/private.key path/to/my/code testimage |
| 52 |
``` |
| 53 |
|
| 54 |
Any processes in the Dockerfile will have access to `/etc/keys/private.key`, but that file will not be part of the committed image. |
| 55 |
|
| 56 |
You can also customize which Dockerfile is run, or run multiple Dockerfiles in sequence (the FROM is ignored on |
| 57 |
later files): |
| 58 |
|
| 59 |
``` |
| 60 |
$ imagebuilder -f Dockerfile:Dockerfile.extra . |
| 61 |
``` |
| 62 |
|
| 63 |
will build the current directory and combine the first Dockerfile with the second. The FROM in the second image |
| 64 |
is ignored. |
| 65 |
|
| 66 |
Note that imagebuilder adds the built image to the `docker` daemon's internal storage. If you use `podman` you must first pull the image into its local registry: |
| 67 |
|
| 68 |
``` |
| 69 |
$ podman pull docker-daemon:<IMAGE>:<TAG> # must contain either a tag or a digest |
| 70 |
``` |
| 71 |
|
| 72 |
## Code Example |
| 73 |
|
| 74 |
```go |
| 75 |
f, err := os.Open("path/to/Dockerfile") |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
defer f.Close() |
| 80 |
|
| 81 |
e := builder.NewClientExecutor(o.Client) |
| 82 |
e.Out, e.ErrOut = os.Stdout, os.Stderr |
| 83 |
e.AllowPull = true |
| 84 |
e.Directory = "context/directory" |
| 85 |
e.Tag = "name/of-image:and-tag" |
| 86 |
e.AuthFn = nil // ... pass a function to retrieve authorization info |
| 87 |
e.LogFn = func(format string, args ...interface{}) { |
| 88 |
fmt.Fprintf(e.ErrOut, "--> %s\n", fmt.Sprintf(format, args...)) |
| 89 |
} |
| 90 |
|
| 91 |
buildErr := e.Build(f, map[string]string{"arg1":"value1"}) |
| 92 |
if err := e.Cleanup(); err != nil { |
| 93 |
fmt.Fprintf(e.ErrOut, "error: Unable to clean up build: %v\n", err) |
| 94 |
} |
| 95 |
|
| 96 |
return buildErr |
| 97 |
``` |
| 98 |
|
| 99 |
Example of usage from OpenShift's experimental `dockerbuild` [command with mount secrets](https://github.com/openshift/origin/blob/26c9e032ff42f613fe10649cd7c5fa1b4c33501b/pkg/cmd/cli/cmd/dockerbuild/dockerbuild.go) |
| 100 |
|
| 101 |
## Run conformance tests (very slow): |
| 102 |
|
| 103 |
``` |
| 104 |
docker rmi mirror.gcr.io/alpine; docker pull mirror.gcr.io/alpine |
| 105 |
docker rmi mirror.gcr.io/busybox; docker pull mirror.gcr.io/busybox |
| 106 |
docker rmi public.ecr.aws/docker/library/centos:7; docker pull public.ecr.aws/docker/library/centos:7 |
| 107 |
docker rmi mirror.gcr.io/debian; docker pull mirror.gcr.io/debian |
| 108 |
docker rmi registry.fedoraproject.org/fedora-minimal; docker pull registry.fedoraproject.org/fedora-minimal |
| 109 |
docker rmi registry.fedoraproject.org/fedora-minimal:44-x86_64; docker pull registry.fedoraproject.org/fedora-minimal:44-x86_64 |
| 110 |
docker rmi registry.fedoraproject.org/fedora-minimal:44-aarch64; docker pull registry.fedoraproject.org/fedora-minimal:44-aarch64 |
| 111 |
docker rmi mirror.gcr.io/golang:1.25; docker pull mirror.gcr.io/golang:1.25 |
| 112 |
docker rmi mirror.gcr.io/nginx; docker pull mirror.gcr.io/nginx |
| 113 |
chmod -R go-w ./dockerclient/testdata |
| 114 |
env DOCKER_API_VERSION=1.44 go test ./dockerclient -tags conformance -timeout 30m |
| 115 |
``` |