Spring Boot 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 Docker image using Cloud Native Buildpacks from an "out-of-the-box" Maven or Gradle task. And once you have the freshly built image of our application, pushing it Microsoft Azure is a walk in the park 🌲.
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.
First, let's create a simple Spring Boot application.
@SpringBootApplication
@RestController
public class HelloDockerAzureApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDockerAzureApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
return "Spring Boot + Docker + Azure = :)";
}
}
Now instead of building it as a jar
file, we are going to build it as a docker
image. All you have to do is to run mvn spring-boot:build-image
if you are using Maven, or gradle bootBuildImage
if you are using Gradle.
And that's it! You can now run and test it locally by simply docker run -p 8080:8080 hello-docker-azure:0.0.1-SNAPSHOT
🚀.
Alright, now on to Azure. First step is to configure a Resource Group
- the Resource Group is a logical grouping of our resources in Azure and makes sure you keep things organized 💼. Assuming you have the Azure CLI installed, all you have to do is to run the following command:
az group create --name myResourceGroup --location eastus
Next you need to create a private Container Registry
where you will store the Docker images you build:
az acr create --resource-group myResourceGroup --name myAzureCR101 --sku Basic
Once the command runs successfully, you should see an output similar to the below:
{
"adminUserEnabled": false,
"creationDate": "2021-03-07T23:35:32.361727+00:00",
"dataEndpointEnabled": false,
"dataEndpointHostNames": [],
"id": "/subscriptions//00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.ContainerRegistry/registries/myAzureCR101",
"location": "eastus",
"loginServer": "myazurecr101.azurecr.io",
"name": "myAzureCR101",
"provisioningState": "Succeeded",
"resourceGroup": "myResourceGroup",
"sku": {
"name": "Basic",
"tier": "Basic"
},
"status": null,
"storageAccount": null,
"tags": {},
"type": "Microsoft.ContainerRegistry/registries",
"zoneRedundancy": "Disabled"
}
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 Login Succeeded
message.
az acr login --name myazurecr101
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:
docker tag hello-docker-azure:0.0.1-SNAPSHOT myazurecr101.azurecr.io/hello-docker-azure:0.0.1-SNAPSHOT
Now you are good to push the image:
docker push myazurecr101.azurecr.io/hello-docker-azure:0.0.1-SNAPSHOT
If you want to validate that last step worked just fine, please run the command below
az acr repository list --name myazurecr101 --output table
As a result you should see the following:
Result
------------------
hello-docker-azure
Now on to the last mile! We will now create a container running the image published in the registry:
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
You will be prompted to enter Image registry username and password. In order to get this information, you will need to run two commands. First, enable admin user:
az acr update --name myazurecr101 --admin-enabled true
Second, retrieve credentials:
az acr credential show --name myazurecr101
That's it! You now have a container running your Spring Boot image on Azure 💎!
You can easily test it by running curl 'http://hello-spring-docker-azure.eastus.azurecontainer.io:8080/hello'
The result should be similar to the below 🎉.
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
And that's all for today! Thank you for getting this far and I wish this was helpful for you.
Happy coding! 😃 💻
Top comments (0)