Create container images and push them to Azure Container Registry
As a next step you will need to containerize your different microservice applications. You can do so by using the below starter for containerizing a spring boot application.
- Containerize Spring Boot applications
- Quickstart: Build and run a container image using Azure Container Registry Tasks
You can use the below Dockerfile as a basis for your own Dockerfile.
FROM mcr.microsoft.com/openjdk/jdk:17-mariner
ARG ARTIFACT_NAME
ARG APP_PORT
EXPOSE ${APP_PORT} 8778 9779
# The application's jar file
ARG JAR_FILE=${ARTIFACT_NAME}
# Add the application's jar to the container
ADD ${JAR_FILE} app.jar
# Run the jar file
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Step by step guidance
-
In the parent pom.xml file in the java-microservices-aks-lab/src directory double check the version number on line 9.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.2</version> </parent>
-
From the Git Bash window, set a
VERSION
environment variable to this version number3.0.2
.VERSION=3.0.2
-
You will start by building all the microservice of the spring petclinic application. To accomplish this, run
mvn clean package
in the root directory of the application.cd ~/workspaces/java-microservices-aks-lab/src mvn clean package -DskipTests
-
Verify that the build succeeds by reviewing the output of the
mvn clean package -DskipTests
command, which should have the following format:[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for spring-petclinic-microservices 3.0.2: [INFO] [INFO] spring-petclinic-microservices ..................... SUCCESS [ 0.249 s] [INFO] spring-petclinic-admin-server ...................... SUCCESS [ 16.123 s] [INFO] spring-petclinic-customers-service ................. SUCCESS [ 6.749 s] [INFO] spring-petclinic-vets-service ...................... SUCCESS [ 4.845 s] [INFO] spring-petclinic-visits-service .................... SUCCESS [ 5.063 s] [INFO] spring-petclinic-config-server ..................... SUCCESS [ 1.777 s] [INFO] spring-petclinic-discovery-server .................. SUCCESS [ 2.563 s] [INFO] spring-petclinic-api-gateway ....................... SUCCESS [ 15.582 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 55.901 s [INFO] Finished at: 2023-06-02T14:07:49Z [INFO] ------------------------------------------------------------------------
-
As a next step you will need to log in to your Azure Container Registry.
az acr login --name $MYACR
When your executing this lab on a local environment, make sure the docker daemon is running. If not the above statement will fail.
-
Create a temporary directory for creating the docker images of each microservice and navigate into this directory.
mkdir -p staging-acr cd staging-acr
-
Create a Dockerfile in this new directory and add the below content.
FROM mcr.microsoft.com/openjdk/jdk:17-mariner ARG ARTIFACT_NAME ARG APP_PORT EXPOSE ${APP_PORT} 8778 9779 # The application's jar file ARG JAR_FILE=${ARTIFACT_NAME} # Add the application's jar to the container ADD ${JAR_FILE} app.jar # Run the jar file ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
You will reuse this Dockerfile for each microservice, each time using specific arguments.
-
Copy all the compiled jar files to this directory.
cp ../spring-petclinic-api-gateway/target/spring-petclinic-api-gateway-$VERSION.jar spring-petclinic-api-gateway-$VERSION.jar cp ../spring-petclinic-admin-server/target/spring-petclinic-admin-server-$VERSION.jar spring-petclinic-admin-server-$VERSION.jar cp ../spring-petclinic-customers-service/target/spring-petclinic-customers-service-$VERSION.jar spring-petclinic-customers-service-$VERSION.jar cp ../spring-petclinic-visits-service/target/spring-petclinic-visits-service-$VERSION.jar spring-petclinic-visits-service-$VERSION.jar cp ../spring-petclinic-vets-service/target/spring-petclinic-vets-service-$VERSION.jar spring-petclinic-vets-service-$VERSION.jar cp ../spring-petclinic-config-server/target/spring-petclinic-config-server-$VERSION.jar spring-petclinic-config-server-$VERSION.jar cp ../spring-petclinic-discovery-server/target/spring-petclinic-discovery-server-$VERSION.jar spring-petclinic-discovery-server-$VERSION.jar
-
Run an
docker build
anddocker push
command to build the container image for the api-gateway and push it to your Azure Container Registry.docker build -t $MYACR.azurecr.io/spring-petclinic-api-gateway:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-api-gateway-$VERSION.jar \ --build-arg APP_PORT=8080 \ . docker image list docker push $MYACR.azurecr.io/spring-petclinic-api-gateway:$VERSION
You are indicating here that the image should be tagged as
<your-registry-name>.azurecr.io/spring-petclinic-api-gateway:3.0.2
. TheARTIFACT_NAME
is the jar file you want to copy into the container image which is needed to run the application. Each microservice also needs a port it will be exposed on. For the api-gateway this is port8080
. -
Once this command has executed, you can check whether your image is present in your container registry.
az acr repository list \ -n $MYACR
-
Now execute the same steps for the
admin-server
,customers-service
,visits-service
andvets-service
.docker build -t $MYACR.azurecr.io/spring-petclinic-admin-server:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-admin-server-$VERSION.jar \ --build-arg APP_PORT=8080 \ . docker push $MYACR.azurecr.io/spring-petclinic-admin-server:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-customers-service:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-customers-service-$VERSION.jar \ --build-arg APP_PORT=8080 \ . docker push $MYACR.azurecr.io/spring-petclinic-customers-service:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-visits-service:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-visits-service-$VERSION.jar \ --build-arg APP_PORT=8080 \ . docker push $MYACR.azurecr.io/spring-petclinic-visits-service:$VERSION docker build -t $MYACR.azurecr.io/spring-petclinic-vets-service:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-vets-service-$VERSION.jar \ --build-arg APP_PORT=8080 \ . docker push $MYACR.azurecr.io/spring-petclinic-vets-service:$VERSION
-
Execute the same steps for the
config-server
, but use8888
for theAPP_PORT
.docker build -t $MYACR.azurecr.io/spring-petclinic-config-server:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-config-server-$VERSION.jar \ --build-arg APP_PORT=8888 \ . docker push $MYACR.azurecr.io/spring-petclinic-config-server:$VERSION
-
Execute the same steps for the
discovery-server
, but use8761
for theAPP_PORT
.docker build -t $MYACR.azurecr.io/spring-petclinic-discovery-server:$VERSION \ --build-arg ARTIFACT_NAME=spring-petclinic-discovery-server-$VERSION.jar \ --build-arg APP_PORT=8761 \ . docker push $MYACR.azurecr.io/spring-petclinic-discovery-server:$VERSION
-
You can now list the contents of your repository again, you should see all your container images there.
az acr repository list \ -n $MYACR
This will output the full list of repositories you created:
[ "spring-petclinic-admin-server", "spring-petclinic-api-gateway", "spring-petclinic-config-server", "spring-petclinic-customers-service", "spring-petclinic-discovery-server", "spring-petclinic-vets-service", "spring-petclinic-visits-service" ]
-
You can also show all the tags in one of these repositories.
az acr repository show-tags \ -n $MYACR \ --repository spring-petclinic-customers-service
This will show you the version you created:
[ "3.0.2" ]