Skip to content

Commit a5c272b

Browse files
committed
hack: Rework+cleanup container build
Main motivation: I was looking at making more changes here - Use an idiom I'd like to standardize more of copy context to `FROM scratch` image which is then mounted and consumed in other phases by mounting. This helps avoid polluting later containers with intermediate copied files. - Change `build.sh` to handle being run from any directory - Drop the `dev-rootfs` stuff as it's weird and awkward; instead we should encourage multi-step builds deriving from this image - Don't make `bootc.tar.zst` only to immediately untar it; just use `COPY` from the build container - Use heredocs to condense multiple `RUN` invocations to avoid pointless small layers Signed-off-by: Colin Walters <[email protected]>
1 parent 3b41d81 commit a5c272b

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

.dockerignore

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
.cosa
1+
# The big one - this can get HUGE and we don't want
2+
# to copy it around.
23
target
3-
!target/dev-rootfs
4+
# This one can have large .qcow2 files written by coreos-assembler
5+
.cosa
46
# These directories don't contribute to our container build
57
docs/
8+
# TMT interprets these, not the container build
69
plans/
10+
# We don't use this by default in containers
11+
.git
12+
# Avoid changes to this blowing out all layer caches
13+
hack/Containerfile

hack/Containerfile

+28-19
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,42 @@
55
# You can also generate an image with cloud-init and other dependencies
66
# with `--build-arg=tmt` which is intended for use particularly via
77
# https://tmt.readthedocs.io/en/stable/
8+
89
ARG base=quay.io/centos-bootc/centos-bootc:stream9
9-
FROM $base as build
10-
# Keep this stuff before the `COPY . /build` below to ensure that the packages
11-
# are cached, i.e. we don't invalidate the package install stage by editing the source.
10+
11+
FROM scratch as context
12+
# We only need this stuff in the initial context
13+
COPY hack /hack
1214
COPY contrib /contrib
13-
COPY hack/build.sh /build.sh
14-
RUN /build.sh && rm -v /build.sh
15+
16+
FROM $base as build
17+
# This installs our package dependencies, and we want to cache it independently of the rest.
18+
# Basically we don't want changing a .rs file to blow out the cache of packages.
19+
RUN --mount=type=bind,from=context,target=/run/context /run/context/hack/build.sh
20+
# Now copy the rest of the source
1521
COPY . /build
1622
WORKDIR /build
17-
RUN mkdir -p /build/target/dev-rootfs # This can hold arbitrary extra content
1823
# See https://www.reddit.com/r/rust/comments/126xeyx/exploring_the_problem_of_faster_cargo_docker/
1924
# We aren't using the full recommendations there, just the simple bits.
20-
RUN --mount=type=cache,target=/build/target --mount=type=cache,target=/var/roothome make test-bin-archive && mkdir -p /out && cp target/bootc.tar.zst /out
25+
RUN --mount=type=cache,target=/build/target --mount=type=cache,target=/var/roothome \
26+
make && make install-all DESTDIR=/out
2127

2228
FROM $base
2329
# We support e.g. adding cloud-init
2430
ARG variant=
25-
COPY hack/provision-derived.sh /tmp
26-
RUN /tmp/provision-derived.sh "$variant" && rm -f /tmp/*.sh
31+
# First, create a layer that is our new binaries.
32+
COPY --from=build /out/ /
33+
# And this layer has additional stuff for testing, such as nushell etc.
34+
RUN --mount=type=bind,from=context,target=/run/context <<EORUN
35+
set -xeuo pipefail
36+
/run/context/hack/provision-derived.sh "$variant"
37+
# Add some testing kargs into our dev builds
38+
cp -a /run/context/hack/test-kargs /usr/lib/bootc/kargs.d/
2739
# Also copy in some default install configs we use for testing
28-
COPY hack/install-test-configs/* /usr/lib/bootc/install/
29-
# And some test kargs
30-
COPY hack/test-kargs /usr/lib/bootc/kargs.d/
31-
# Inject our built code
32-
COPY --from=build /out/bootc.tar.zst /tmp
33-
RUN tar -C / --zstd -xvf /tmp/bootc.tar.zst && rm -vrf /tmp/*
34-
# Also copy over arbitrary bits from the target root
35-
COPY --from=build /build/target/dev-rootfs/ /
36-
# Test our own linting
37-
RUN bootc container lint --fatal-warnings
40+
cp -a /run/context/hack/install-test-configs/* /usr/lib/bootc/install/
41+
# Finally only in this containerfile, inject a file which signifies
42+
# this comes from this development image.
43+
touch /usr/lib/.bootc-dev-stamp
44+
# Finally, test our own linting
45+
bootc container lint --fatal-warnings
46+
EORUN

hack/build.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/bin/bash
22
set -xeu
33
. /usr/lib/os-release
4+
dn=$(cd $(dirname $0) && pwd)
45
case $ID in
56
centos|rhel) dnf config-manager --set-enabled crb;;
67
fedora) dnf -y install dnf-utils 'dnf5-command(builddep)';;
78
esac
8-
dnf -y builddep ./contrib/packaging/bootc.spec
9+
dnf -y builddep ${dn}/../contrib/packaging/bootc.spec
910
# Extra dependencies
1011
dnf -y install git-core

0 commit comments

Comments
 (0)