1
0
mirror of https://github.com/containers/buildah.git synced 2026-02-05 09:45:38 +01:00

docs/tutorials/04: add defaults for Run()

In the tutorial, switch to calling `DefaultStoreOptionsAutoDetectUID()`
instead of `DefaultStoreOptions()`, which should figure things out
better.

In the tutorial, add an example of using Run(), where for API backward
compatibility reasons, we can't tell the difference between "grant no
capabilties by default" and "grant the default set of capabilities by
default".

The default isolation can be set automatically, so start doing that at
run-time, but have the tutorial look it up anyway because the tutorial
on the web will be newer than our current release for at least a while.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
Nalin Dahyabhai
2023-02-20 14:30:28 -05:00
parent 5a63d7f619
commit 0b7a7f80bb
4 changed files with 49 additions and 6 deletions

View File

@@ -53,7 +53,7 @@ Now you can develop your application. To access to the build features of Buildah
To instantiate a `Builder`, you need a `storage.Store` (the Store interface found in [store.go](https://github.com/containers/storage/blob/main/store.go)) from [`github.com/containers/storage`](https://github.com/containers/storage), where the intermediate and result images will be stored:
```go
buildStoreOptions, err := storage.DefaultStoreOptions(unshare.IsRootless(), unshare.GetRootlessUID())
buildStoreOptions, err := storage.DefaultStoreOptionsAutoDetectUID()
buildStore, err := storage.GetStore(buildStoreOptions)
```
@@ -95,6 +95,15 @@ Now you can run commit the build:
imageId, _, _, err := builder.Commit(context.TODO(), imageRef, buildah.CommitOptions{})
```
## Supplying defaults for Run()
If you need to run a command as part of the build, you'll have to dig up a couple of defaults that aren't picked up automatically:
```go
conf, err := config.Default()
capabilitiesForRoot, err := conf.Capabilities("root", nil, nil)
isolation, err := parse.IsolationOption("")
```
## Rootless mode
To enable rootless mode, import `github.com/containers/storage/pkg/unshare` and add this code at the beginning of your main method:
@@ -118,6 +127,8 @@ import (
"fmt"
"github.com/containers/buildah"
"github.com/containers/buildah/pkg/parse"
"github.com/containers/common/pkg/config"
is "github.com/containers/image/v5/storage"
"github.com/containers/storage"
"github.com/containers/storage/pkg/unshare"
@@ -129,7 +140,16 @@ func main() {
}
unshare.MaybeReexecUsingUserNamespace(false)
buildStoreOptions, err := storage.DefaultStoreOptions(unshare.IsRootless(), unshare.GetRootlessUID())
buildStoreOptions, err := storage.DefaultStoreOptionsAutoDetectUID()
if err != nil {
panic(err)
}
conf, err := config.Default()
if err != nil {
panic(err)
}
capabilitiesForRoot, err := conf.Capabilities("root", nil, nil)
if err != nil {
panic(err)
}
@@ -142,6 +162,7 @@ func main() {
builderOpts := buildah.BuilderOptions{
FromImage: "node:12-alpine",
Capabilities: capabilitiesForRoot,
}
builder, err := buildah.NewBuilder(context.TODO(), buildStore, builderOpts)
@@ -155,6 +176,16 @@ func main() {
panic(err)
}
isolation, err := parse.IsolationOption("")
if err != nil {
panic(err)
}
err = builder.Run([]string{"sh", "-c", "date > /home/node/build-date.txt"}, buildah.RunOptions{Isolation: isolation, Terminal: buildah.WithoutTerminal})
if err != nil {
panic(err)
}
builder.SetCmd([]string{"node", "/home/node/script.js"})
imageRef, err := is.Transport.ParseStoreReference(buildStore, "docker.io/myusername/my-image")