<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ermathias</title>
    <description>The latest articles on DEV Community by ermathias (@ermathias).</description>
    <link>https://dev.to/ermathias</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F592178%2F4519548f-091d-4311-9a8f-cf0c0391a35b.jpeg</url>
      <title>DEV Community: ermathias</title>
      <link>https://dev.to/ermathias</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ermathias"/>
    <language>en</language>
    <item>
      <title>Build your Spring Boot microservice as a Docker image and deploy it to an Azure Container Instance</title>
      <dc:creator>ermathias</dc:creator>
      <pubDate>Mon, 08 Mar 2021 00:29:27 +0000</pubDate>
      <link>https://dev.to/ermathias/build-your-spring-boot-microservice-as-docker-image-and-deploy-to-azure-container-instance-3f6i</link>
      <guid>https://dev.to/ermathias/build-your-spring-boot-microservice-as-docker-image-and-deploy-to-azure-container-instance-3f6i</guid>
      <description>&lt;p&gt;&lt;a href="https://spring.io/projects/spring-boot"&gt;Spring Boot&lt;/a&gt; is the de facto standard for microservices development in the enterprise. And since version 2.3.x, you can now easily build your application as a &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; image using &lt;a href="https://buildpacks.io/"&gt;Cloud Native Buildpacks&lt;/a&gt; from an "out-of-the-box" &lt;a href="https://maven.apache.org/"&gt;Maven&lt;/a&gt; or &lt;a href="https://gradle.org/"&gt;Gradle&lt;/a&gt; task. And once you have the freshly built image of our application, pushing it &lt;a href="https://azure.microsoft.com/en-us/"&gt;Microsoft Azure&lt;/a&gt; is a walk in the park 🌲.&lt;/p&gt;

&lt;p&gt;In this article I will walk you through the basic steps to create a simple Spring Boot microservice, build it as a Docker image and deploy it to Azure Container Instance.&lt;/p&gt;

&lt;p&gt;First, let's create a simple Spring Boot application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;
&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloDockerAzureApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HelloDockerAzureApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Spring Boot + Docker + Azure = :)"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now instead of building it as a &lt;code&gt;jar&lt;/code&gt; file, we are going to build it as a &lt;code&gt;docker&lt;/code&gt; image. All you have to do is to run &lt;code&gt;mvn spring-boot:build-image&lt;/code&gt; if you are using Maven, or &lt;code&gt;gradle bootBuildImage&lt;/code&gt; if you are using Gradle.&lt;/p&gt;

&lt;p&gt;And that's it! You can now run and test it locally by simply &lt;code&gt;docker run -p 8080:8080 hello-docker-azure:0.0.1-SNAPSHOT&lt;/code&gt; 🚀.&lt;/p&gt;

&lt;p&gt;Alright, now on to Azure. First step is to configure a &lt;code&gt;Resource Group&lt;/code&gt; - the Resource Group is a logical grouping of our resources in Azure and makes sure you keep things organized 💼. Assuming you have the &lt;a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli"&gt;Azure CLI&lt;/a&gt; installed, all you have to do is to run the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;az group create --name myResourceGroup --location eastus&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next you need to create a private &lt;code&gt;Container Registry&lt;/code&gt; where you will store the Docker images you build:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;az acr create --resource-group myResourceGroup --name myAzureCR101 --sku Basic&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once the command runs successfully, you should see an output similar to the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"adminUserEnabled"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"creationDate"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-03-07T23:35:32.361727+00:00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dataEndpointEnabled"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dataEndpointHostNames"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/subscriptions//00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/myAzureCR101"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"location"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"eastus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"loginServer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"myazurecr101.azurecr.io"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"myAzureCR101"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"provisioningState"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Succeeded"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"resourceGroup"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"myResourceGroup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"sku"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Basic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"tier"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Basic"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"storageAccount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"tags"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Microsoft.ContainerRegistry/registries"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"zoneRedundancy"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Disabled"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before start to pull and push images from your brand new Container Registry, you must login into it. Just run the below command. You should get a &lt;code&gt;Login Succeeded&lt;/code&gt; message.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;az acr login --name myazurecr101&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Remember the Docker image we have built for our Spring Boot microservice? In order to push it to the Container Registry, you must tag it using the FQDN of the registry:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker tag hello-docker-azure:0.0.1-SNAPSHOT myazurecr101.azurecr.io/hello-docker-azure:0.0.1-SNAPSHOT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now you are good to push the image:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker push myazurecr101.azurecr.io/hello-docker-azure:0.0.1-SNAPSHOT&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you want to validate that last step worked just fine, please run the command below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;az acr repository list --name myazurecr101 --output table&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As a result you should see the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Result
------------------
hello-docker-azure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now on to the last mile! We will now create a container running the image published in the registry:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;az container create --resource-group myResourceGroup --name mycontainer --image myazurecr101.azurecr.io/hello-docker-azure:0.0.1-SNAPSHOT --dns-name-label hello-spring-docker-azure --ports 8080&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will be prompted to enter &lt;em&gt;Image registry username&lt;/em&gt; and &lt;em&gt;password&lt;/em&gt;. In order to get this information, you will need to run two commands. First, enable admin user:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;az acr update --name myazurecr101 --admin-enabled true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Second, retrieve credentials:&lt;br&gt;
&lt;code&gt;az acr credential show --name myazurecr101&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's it! You now have a container running your Spring Boot image on Azure 💎!&lt;/p&gt;

&lt;p&gt;You can easily test it by running &lt;code&gt;curl 'http://hello-spring-docker-azure.eastus.azurecontainer.io:8080/hello'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result should be similar to the below 🎉.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;StatusCode        : 200
StatusDescription :
Content           : Spring Boot + Docker + Azure = :)
RawContent        : HTTP/1.1 200
                    Keep-Alive: timeout=60
                    Connection: keep-alive
                    Content-Length: 33
                    Content-Type: text/plain;charset=UTF-8
                    Date: Mon, 08 Mar 2021 00:13:45 GMT

                    Spring Boot + Docker + Azure = :)
Forms             : {}
Headers           : {[Keep-Alive, timeout=60], [Connection, keep-alive], [Content-Length, 33], [Content-Type, text/plain;charset=UTF-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all for today! Thank you for getting this far and I wish this was helpful for you. &lt;/p&gt;

&lt;p&gt;Happy coding! 😃 💻&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>azure</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
