Skip to content

Commit

Permalink
Docker image tool: implement local image builds
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed Feb 29, 2024
1 parent 375c72c commit 86cd998
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 45 deletions.
20 changes: 15 additions & 5 deletions tools/dockerbuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ The `build-push-images.sh` script is meant to build Java and optionally native i
restricted to (or specialized for) `:nessie-quarkus`, but can be used with basically any Quarkus
application.

Minimal options (use `./build-push-images.sh -h` for an up-to-date listing) are:
Minimal options (use `./build-push-images.sh --help` for an up-to-date listing) are:

* `-g <gradle-project-name>` in the `:my-project-name` syntax
* `-p <gradle-project-directory>` like `servers/quarkus-server`
* `--gradle-project <gradle-project-name>` in the `:my-project-name` syntax
* `--project-dir <gradle-project-directory>` like `servers/quarkus-server`
* the image name as an argument, for example `projectnessie/nessie`

The image name also defines where the built images are pushed to. By (Docker) default, it's Docker
Expand All @@ -20,13 +20,23 @@ example (see local Docker registry notes below):

```bash
tools/dockerbuild/build-push-images.sh \
-g :nessie-quarkus \
-p servers/quarkus-server \
--gradle-project :nessie-quarkus \
--project-dir servers/quarkus-server \
localhost:5000/projectnessie/nessie-local
```

Hint: The images are pushed to the local registry, those will **not** show up in `docker images`.

## Build an image for local use

```bash
tools/dockerbuild/build-push-images.sh \
--gradle-project :nessie-quarkus \
--project-dir servers/quarkus-server \
--local \
localhost/projectnessie/nessie-local
```

## Updating your local Docker registry

There's a `docker-registry` Debian/Ubuntu package. The registry configuration might need some tweaks
Expand Down
102 changes: 62 additions & 40 deletions tools/dockerbuild/build-push-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ set -e

IMAGE_NAME=""
GITHUB=0
LOCAL=0
ARTIFACTS=""
GRADLE_PROJECT=""
PROJECT_DIR=""
DOCKERFILE="Dockerfile-server"

TOOL="$(which docker > /dev/null && echo docker || echo podman)"

if [[ -n ${GITHUB_ENV} ]]; then
GITHUB=1
fi
Expand All @@ -45,12 +48,17 @@ function usage() {
-d | --dockerfile <file> Dockerfile to use (default: Dockerfile-server)
-gh | --github GitHub actions mode
-a | --artifacts-dir <dir> Directory to place uber-jars in
-l | --local Only build the image for local use (not multi-platform),
not pushed to a registry. Can build with 'docker' and 'podman'.
Note: multiplatform builds only work with docker buildx, not implemented for podman.
GitHub mode is automatically enabled, when GITHUB_ENV is present. -a is mandatory in GitHub mode.
Examples:
$0 -d Dockerfile-server -g :nessie-quarkus -p servers/quarkus-server nessie-unstable
$0 -d Dockerfile-gctool -g :nessie-gc-tool -p gc/gc-tool nessie-gc-unstable
$0 --local --dockerfile Dockerfile-server --gradle-project :nessie-quarkus --project-dir servers/quarkus-server nessie-local
$0 --dockerfile Dockerfile-server --gradle-project :nessie-quarkus --project-dir servers/quarkus-server nessie-unstable
$0 --dockerfile Dockerfile-gctool --gradle-project :nessie-gc-tool --project-dir gc/gc-tool nessie-gc-unstable
!
}

Expand Down Expand Up @@ -88,6 +96,9 @@ while [[ $# -gt 0 ]]; do
-gh | --github)
GITHUB=1
;;
-l | --local)
LOCAL=1
;;
-h | --help)
usage
exit 0
Expand Down Expand Up @@ -136,47 +147,58 @@ echo "Placing binaries in: ${ARTIFACTS}"
mkdir -p "${ARTIFACTS}"
gh_endgroup

gh_group "Prepare buildx"
docker buildx use default
docker buildx create \
--platform linux/amd64,linux/arm64 \
--use \
--name nessiebuild \
--driver-opt network=host || docker buildx use nessiebuild
# Note: '--driver-opt network=host' is needed to be able to push to a local registry (e.g. localhost:5000)
gh_endgroup

gh_group "Docker buildx info"
docker buildx inspect
gh_endgroup
if [[ ${LOCAL} == 0 ]] ; then
gh_group "Prepare buildx"
${TOOL} buildx use default
${TOOL} buildx create \
--platform linux/amd64,linux/arm64 \
--use \
--name nessiebuild \
--driver-opt network=host || docker buildx use nessiebuild
# Note: '--driver-opt network=host' is needed to be able to push to a local registry (e.g. localhost:5000)
gh_endgroup

gh_group "Docker buildx info"
${TOOL} buildx inspect
gh_endgroup
fi

#
# Java multiplatform image
# Gradle Build
#

gh_group "Build Java linux/amd64"
gh_group "Build Java"
./gradlew "${GRADLE_PROJECT}:clean" "${GRADLE_PROJECT}:build" -x "${GRADLE_PROJECT}:check"
gh_endgroup

gh_group "Docker buildx build"
# All the platforms that are available
PLATFORMS="linux/amd64,linux/arm64/v8,linux/ppc64le,linux/s390x"
docker buildx build \
-f "${BASE_DIR}/tools/dockerbuild/docker/${DOCKERFILE}" \
--platform "${PLATFORMS}" \
-t "${IMAGE_NAME}:latest" \
-t "${IMAGE_NAME}:latest-java" \
-t "${IMAGE_NAME}:${IMAGE_TAG_BASE}" \
-t "${IMAGE_NAME}:${IMAGE_TAG_BASE}-java" \
"${BASE_DIR}/${PROJECT_DIR}" \
--push \
--provenance=false --sbom=false \
--output type=registry
# Note: '--output type=registry' is needed to be able to push to a local registry (e.g. localhost:5000)
# Note: '--provenance=false --sbom=false' work around UI issues in ghcr + quay showing 'unknown/unknown' architectures
gh_summary "## Java image tags, built for ${PLATFORMS}"
gh_summary "* \`docker pull ${IMAGE_NAME}:latest\`"
gh_summary "* \`docker pull ${IMAGE_NAME}:latest-java\`"
gh_summary "* \`docker pull ${IMAGE_NAME}:${IMAGE_TAG_BASE}\`"
gh_summary "* \`docker pull ${IMAGE_NAME}:${IMAGE_TAG_BASE}-java\`"
gh_endgroup
if [[ ${LOCAL} == 1 ]] ; then
gh_group "Docker build"
${TOOL} build \
--file "${BASE_DIR}/tools/dockerbuild/docker/${DOCKERFILE}" \
--tag "${IMAGE_NAME}:latest" \
--tag "${IMAGE_NAME}:${IMAGE_TAG_BASE}" \
"${BASE_DIR}/${PROJECT_DIR}"
gh_endgroup
else
gh_group "Docker buildx build"
# All the platforms that are available
PLATFORMS="linux/amd64,linux/arm64/v8,linux/ppc64le,linux/s390x"
${TOOL} buildx build \
--file "${BASE_DIR}/tools/dockerbuild/docker/${DOCKERFILE}" \
--platform "${PLATFORMS}" \
--tag "${IMAGE_NAME}:latest" \
--tag "${IMAGE_NAME}:latest-java" \
--tag "${IMAGE_NAME}:${IMAGE_TAG_BASE}" \
--tag "${IMAGE_NAME}:${IMAGE_TAG_BASE}-java" \
"${BASE_DIR}/${PROJECT_DIR}" \
--push \
--provenance=false --sbom=false \
--output type=registry
# Note: '--output type=registry' is needed to be able to push to a local registry (e.g. localhost:5000)
# Note: '--provenance=false --sbom=false' work around UI issues in ghcr + quay showing 'unknown/unknown' architectures
gh_summary "## Java image tags, built for ${PLATFORMS}"
gh_summary "* \`docker pull ${IMAGE_NAME}:latest\`"
gh_summary "* \`docker pull ${IMAGE_NAME}:latest-java\`"
gh_summary "* \`docker pull ${IMAGE_NAME}:${IMAGE_TAG_BASE}\`"
gh_summary "* \`docker pull ${IMAGE_NAME}:${IMAGE_TAG_BASE}-java\`"
gh_endgroup
fi

0 comments on commit 86cd998

Please sign in to comment.