Skip to main content Link Menu Expand (external link) Document Search Copy Copied

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.

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

  1. 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>
    
  2. From the Git Bash window, set a VERSION environment variable to this version number 3.0.2.

    VERSION=3.0.2
    
  3. 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
    
  4. 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] ------------------------------------------------------------------------
    
  5. 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.

  6. Create a temporary directory for creating the docker images of each microservice and navigate into this directory.

    mkdir -p staging-acr
    cd staging-acr
    
  7. 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.

  8. 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
    
  9. Run an docker build and docker 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. The ARTIFACT_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 port 8080.

  10. Once this command has executed, you can check whether your image is present in your container registry.

    az acr repository list \
       -n $MYACR
    
  11. Now execute the same steps for the admin-server, customers-service, visits-service and vets-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
    
  12. Execute the same steps for the config-server, but use 8888 for the APP_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
    
  13. Execute the same steps for the discovery-server, but use 8761 for the APP_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
    
  14. 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"
     ]
    
  15. 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"
    ]