Skip to content

Commit

Permalink
Merge pull request #5665 from nalind/fixup-tutorial
Browse files Browse the repository at this point in the history
[CI:DOCS] Update tutorials to keep up with API changes in storage
  • Loading branch information
openshift-merge-bot[bot] authored Aug 7, 2024
2 parents 54f8d1d + 96d8b14 commit f113537
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 309 deletions.
8 changes: 4 additions & 4 deletions docs/tutorials/01-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ Notice that the overlay mount point is somewhere under `/var/lib/containers/stor

Now that we have a new empty container we can install or remove software packages or simply copy content into that container. So let's install `bash` and `coreutils` so that we can run bash scripts. This could easily be `nginx` or other packages needed for your container.

**NOTE:** the version in the example below (35) relates to a Fedora version which is the Linux platform this example was run on. If you are running dnf on the host to populate the container, the version you specify must be valid for the host or dnf will throw an error. I.e. If you were to run this on a RHEL platform, you'd need to specify `--releasever 8.1` or similar instead of `--releasever 35`. If you want the container to be a particular Linux platform, change `scratch` in the first line of the example to the platform you want, i.e. `# newcontainer=$(buildah from fedora)`, and then you can specify an appropriate version number for that Linux platform.
**NOTE:** the version in the example below (40) relates to a Fedora version which is the Linux platform this example was run on. If you are running dnf on the host to populate the container, the version you specify must be valid for the host or dnf will throw an error. I.e. If you were to run this on a RHEL platform, you'd need to specify `--releasever 8.1` or similar instead of `--releasever 40`. If you want the container to be a particular Linux platform, change `scratch` in the first line of the example to the platform you want, i.e. `# newcontainer=$(buildah from fedora)`, and then you can specify an appropriate version number for that Linux platform.

# dnf install --installroot $scratchmnt --releasever 35 bash coreutils --setopt install_weak_deps=false -y
# dnf install --installroot $scratchmnt --releasever 40 bash coreutils --setopt install_weak_deps=false -y

Let's try it out (showing the prompt in this example to demonstrate the difference):

Expand All @@ -142,7 +142,7 @@ With `buildah` files can be copied into the new container. We can then use `bui

# To test with Podman, first install via:
# dnf -y install podman
# buildah copy $newcontainer ./runecho.sh /usr/bin
# buildah copy $newcontainer ./runecho.sh /usr/bin/
# buildah config --cmd /usr/bin/runecho.sh $newcontainer
# buildah commit $newcontainer newimage

Expand Down Expand Up @@ -180,7 +180,7 @@ It works! Congratulations, you have built a new OCI container image from scratch
Back to Buildah, let's add some more configuration information.

# buildah config --created-by "ipbabble" $newcontainer
# buildah config --author "wgh at redhat.com @ipbabble" --label name=fedora35-bashecho $newcontainer
# buildah config --author "wgh at redhat.com @ipbabble" --label name=fedora40-bashecho $newcontainer

We can inspect the working container's metadata using the `inspect` command:

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/02-registries-repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ You can use `--log-level=debug` on any Buildah command.

The registry is running and is waiting for requests to process. Notice that this registry is a Docker registry that we pulled from Docker Hub and we are running it for this example using `buildah run`. There is no Docker daemon running at this time.

Let's push our image to the private registry. By default, Buildah is set up to only make secure connections to a registry. Therefore we will need to turn the TLS verification off using the `--tls-verify` flag. We also need to tell Buildah that the registry is on this local host ( i.e. localhost) and listening on port 5000. Similar to what you'd expect to do on multi-tenant Docker Hub, we will explicitly specify that the registry is to store the image under the `ipbabble` repository - so as not to clash with other users' similarly named images.
Let's push our image to the private registry. By default, Buildah is set up to only make secure connections to a registry. Therefore we will need to turn the TLS verification off using the `--tls-verify` flag. We also need to tell Buildah that the registry is on this local host (i.e. localhost) and listening on port 5000. Similar to what you'd expect to do on multi-tenant Docker Hub, we will explicitly specify that the registry is to store the image under the `ipbabble` repository - so as not to clash with other users' similarly named images.

# buildah push --tls-verify=false fedora-bashecho docker://localhost:5000/ipbabble/fedora-bashecho:latest

Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/03-on-build.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ First create two Dockerfiles:

```
$ cat << EOF > Dockerfile
FROM fedora:latest
FROM registry.fedoraproject.org/fedora:latest
RUN touch /foo
ONBUILD RUN touch /bar
EOF
Expand Down Expand Up @@ -83,7 +83,7 @@ Instead of using a Dockerfile to create the onbuild-image, Buildah allows you to
First a Fedora container will be created with `buildah from`, then the `/foo` file will be added with `buildah run`. The `buildah config` command will configure ONBUILD to add `/bar` when a container image is created from the primary image, and finally the image will be saved with `buildah commit`.

```
# buildah from --format=docker --name onbuild-container fedora:latest
# buildah from --format=docker --name onbuild-container registry.fedoraproject.org/fedora:latest
# buildah run onbuild-container touch /foo
# buildah config --onbuild="RUN touch /bar" onbuild-container
# buildah commit --format=docker onbuild-container onbuild-image
Expand Down Expand Up @@ -188,6 +188,6 @@ If you have any suggestions or issues please post them at the [Buildah Issues pa
For more information on Buildah and how you might contribute please visit the [Buildah home page on GitHub](https://github.com/containers/buildah).

[Podman's site]: https://podman.io/
[image specification]: https://github.com/opencontainers/runtime-spec
[image specification]: https://github.com/opencontainers/image-spec/blob/main/spec.md
[Introduction Tutorial]: 01-intro.md
[Open Container Initiative]: https://www.opencontainers.org/
24 changes: 12 additions & 12 deletions docs/tutorials/04-include-in-your-build-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ Next, we should import Buildah as a dependency. However, make sure that you have
developer packages installed:

```shell
dnf install btrfs-progs-devel gpgme-devel
dnf install btrfs-progs-devel gpgme-devel passt
```

Depending on your Linux distribution, the names of the packages can be slightly different. For instance, on
OpenSUSE it would be

```shell
zypper in libbtrfs-devel libgpgme-devel
zypper install libbtrfs-devel libgpgme-devel passt
```

On Debian and Ubuntu, it would be

```shell
apt install libbtrfs-dev libgpgme-dev
apt install libbtrfs-dev libgpgme-dev passt
```

Now import Buildah as a dependency:
Expand All @@ -48,20 +48,20 @@ go get github.com/containers/buildah

## Build the image

Now you can develop your application. To access to the build features of Buildah, you need to instantiate `buildah.Builder`. This struct has methods to configure the build, define the build steps and run it.
Now you can develop your application. To access to the build features of Buildah, you need to instantiate a `buildah.Builder`. This struct has methods to configure the build, define the build steps and run it.

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.DefaultStoreOptionsAutoDetectUID()
buildStoreOptions, err := storage.DefaultStoreOptions()
buildStore, err := storage.GetStore(buildStoreOptions)
```

Define the builder options:

```go
builderOpts := buildah.BuilderOptions{
FromImage: "node:12-alpine", // base image
FromImage: "docker.io/library/node:12-alpine", // base image
}
```

Expand All @@ -71,7 +71,7 @@ Now instantiate the `Builder`:
builder, err := buildah.NewBuilder(context.TODO(), buildStore, builderOpts)
```

Let's add our JS file (assuming is in your local directory with name `script.js`):
Let's add our JS file (assuming it is in your local directory with name `script.js`):

```go
err = builder.Add("/home/node/", false, buildah.AddAndCopyOptions{}, "script.js")
Expand All @@ -89,7 +89,7 @@ Before completing the build, create the image reference:
imageRef, err := is.Transport.ParseStoreReference(buildStore, "docker.io/myusername/my-image")
```

Now you can run commit the build:
Now you can commit the container to create the image:

```go
imageId, _, _, err := builder.Commit(context.TODO(), imageRef, buildah.CommitOptions{})
Expand All @@ -106,7 +106,7 @@ 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:
To enable rootless mode, import `github.com/containers/storage/pkg/unshare` and add this code at the beginning of your main() method:

```go
if buildah.InitReexec() {
Expand All @@ -115,7 +115,7 @@ if buildah.InitReexec() {
unshare.MaybeReexecUsingUserNamespace(false)
```

This code ensures that your application is re-executed in a user namespace where it has root privileges.
This code ensures that your application is re-executed in a user namespace where it has root privileges. The reexec mechanism which `buildah.InitReexec()` sets up is also used for various other internal functions, so it should always be called.

## Complete code

Expand All @@ -140,7 +140,7 @@ func main() {
}
unshare.MaybeReexecUsingUserNamespace(false)

buildStoreOptions, err := storage.DefaultStoreOptionsAutoDetectUID()
buildStoreOptions, err := storage.DefaultStoreOptions()
if err != nil {
panic(err)
}
Expand All @@ -161,7 +161,7 @@ func main() {
defer buildStore.Shutdown(false)

builderOpts := buildah.BuilderOptions{
FromImage: "node:12-alpine",
FromImage: "docker.io/library/node:12-alpine",
Capabilities: capabilitiesForRoot,
}

Expand Down
Loading

0 comments on commit f113537

Please sign in to comment.