From 81a1cc0c7ecdf7a750695bd8f9344ab443b4cbf3 Mon Sep 17 00:00:00 2001 From: Antoine Rey Date: Fri, 5 Jan 2024 08:52:35 +0100 Subject: [PATCH] Add shell scripts and documentation to build and push Docker images for amd64 and arm64 architectures (#252) --- README.md | 56 +++++++++++++++++++++++++++++++++++++++---- pom.xml | 11 ++++++++- scripts/pushImages.sh | 8 +++++++ scripts/tagImages.sh | 8 +++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100755 scripts/pushImages.sh create mode 100755 scripts/tagImages.sh diff --git a/README.md b/README.md index d8d5dd042..dd466438f 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,22 @@ You can tell Config Server to use your local Git repository by using `native` Sp `-Dspring.profiles.active=native -DGIT_REPO=/projects/spring-petclinic-microservices-config` ## Starting services locally with docker-compose -In order to start entire infrastructure using Docker, you have to build images by executing `./mvnw clean install -P buildDocker` . This requires Docker or Docker desktop to be installed and running. - -Alternatively you can also build all the images on Podman, which requires Podman or Podman Desktop to be installed and running. `./mvnw clean install -PbuildDocker -Dcontainer.executable=podman` +In order to start entire infrastructure using Docker, you have to build images by executing +``bash +./mvnw clean install -P buildDocker +`` +This requires `Docker` or `Docker desktop` to be installed and running. + +Alternatively you can also build all the images on `Podman`, which requires Podman or Podman Desktop to be installed and running. +```bash +./mvnw clean install -PbuildDocker -Dcontainer.executable=podman +``` +By default, the Docker OCI image is build for an `linux/amd64` platform. +For other architectures, you could change it by using the `-Dcontainer.platform` maven command line argument. +For instance, if you target container images for an Apple M2, you could use the command line with the `linux/arm64` architecture: +```bash +./mvnw clean install -P buildDocker -Dcontainer.platform="linux/arm64" +``` Once images are ready, you can start them with a single command `docker-compose up` or `podman-compose up`. @@ -144,13 +157,46 @@ All those three REST controllers `OwnerResource`, `PetResource` and `VisitResour | Circuit Breaker | [Resilience4j fallback method](spring-petclinic-api-gateway/src/main/java/org/springframework/samples/petclinic/api/boundary/web/ApiGatewayController.java) | | Grafana / Prometheus Monitoring | [Micrometer implementation](https://micrometer.io/), [Spring Boot Actuator Production Ready Metrics] | - Front-end module | Files | +| Front-end module | Files | |-------------------|-------| | Node and NPM | [The frontend-maven-plugin plugin downloads/installs Node and NPM locally then runs Bower and Gulp](spring-petclinic-ui/pom.xml) | | Bower | [JavaScript libraries are defined by the manifest file bower.json](spring-petclinic-ui/bower.json) | | Gulp | [Tasks automated by Gulp: minify CSS and JS, generate CSS from LESS, copy other static resources](spring-petclinic-ui/gulpfile.js) | | Angular JS | [app.js, controllers and templates](spring-petclinic-ui/src/scripts/) | +## Pushing to a Docker registry + +Docker images for `linux/amd64` and `linux/arm64` platforms have been published into DockerHub +in the [springcommunity](https://hub.docker.com/u/springcommunity) organization. +You can pull an image: +```bash +docker pull springcommunity/spring-petclinic-config-server +``` +You may prefer to build then push images to your own Docker registry. + +### Choose your Docker registry + +You need to define your target Docker registry. +Make sure you're already logged in by running `docker login ` or `docker login` if you're just targeting Docker hub. + +Setup the `REPOSITORY_PREFIX` env variable to target your Docker registry. +If you're targeting Docker hub, simple provide your username, for example: +```bash +export REPOSITORY_PREFIX=springcommunity +``` + +For other Docker registries, provide the full URL to your repository, for example: +```bash +export REPOSITORY_PREFIX=harbor.myregistry.com/petclinic +``` + +To push Docker image for the `linux/amd64` and the `linux/arm64` platform to your own registry, please use the command line: +```bash +mvn clean install -Dmaven.test.skip -P buildDocker -Ddocker.image.prefix=${REPOSITORY_PREFIX} -Dcontainer.build.extraarg="--push" -Dcontainer.platform="linux/amd64,linux/arm64" +``` + +The `scripts/pushImages.sh` and `scripts/tagImages.sh` shell scripts could also be used once you build your image with the `buildDocker` maven profile. +The `scripts/tagImages.sh` requires to declare the `VERSION` env variable. ## Interesting Spring Petclinic forks @@ -163,7 +209,7 @@ If you have a special interest in a different technology stack that could be used to implement the Pet Clinic then please join the community there. -# Contributing +## Contributing The [issue tracker](https://github.com/spring-petclinic/spring-petclinic-microservices/issues) is the preferred channel for bug reports, features requests and submitting pull requests. diff --git a/pom.xml b/pom.xml index af085bafc..aa4b2d129 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,14 @@ springcommunity 9090 ${basedir} + docker + + + linux/amd64 + + + --load @@ -147,11 +154,13 @@ build -f Dockerfile - --load --build-arg ARTIFACT_NAME=${project.build.finalName} --build-arg EXPOSED_PORT=${docker.image.exposed.port} + --platform + ${container.platform} + ${container.build.extraarg} -t ${docker.image.prefix}/${project.artifactId} ${project.build.directory} diff --git a/scripts/pushImages.sh b/scripts/pushImages.sh new file mode 100755 index 000000000..e5f8a70e1 --- /dev/null +++ b/scripts/pushImages.sh @@ -0,0 +1,8 @@ +#!/bin/bash +docker push ${REPOSITORY_PREFIX}/spring-petclinic-config-server:${VERSION} +docker push ${REPOSITORY_PREFIX}/spring-petclinic-discovery-server:${VERSION} +docker push ${REPOSITORY_PREFIX}/spring-petclinic-api-gateway:${VERSION} +docker push ${REPOSITORY_PREFIX}/spring-petclinic-visits-service:${VERSION} +docker push ${REPOSITORY_PREFIX}/spring-petclinic-vets-service:${VERSION} +docker push ${REPOSITORY_PREFIX}/spring-petclinic-customers-service:${VERSION} +docker push ${REPOSITORY_PREFIX}/spring-petclinic-admin-server:${VERSION} diff --git a/scripts/tagImages.sh b/scripts/tagImages.sh new file mode 100755 index 000000000..540c335c3 --- /dev/null +++ b/scripts/tagImages.sh @@ -0,0 +1,8 @@ +#!/bin/bash +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-config-server ${REPOSITORY_PREFIX}/spring-petclinic-config-server:${VERSION} +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-discovery-server ${REPOSITORY_PREFIX}/spring-petclinic-discovery-server:${VERSION} +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-api-gateway ${REPOSITORY_PREFIX}/spring-petclinic-api-gateway:${VERSION} +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-visits-service ${REPOSITORY_PREFIX}/spring-petclinic-visits-service:${VERSION} +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-vets-service ${REPOSITORY_PREFIX}/spring-petclinic-vets-service:${VERSION} +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-customers-service ${REPOSITORY_PREFIX}/spring-petclinic-customers-service:${VERSION} +docker tag ${REPOSITORY_PREFIX}/spring-petclinic-admin-server ${REPOSITORY_PREFIX}/spring-petclinic-admin-server:${VERSION}