I want to run 2 containers using Azure Container group.In which one is MongoDB and another container is Spring Boot application.Spring Boot application I have created a profile and configured a mongodburl and build docker image and pushed it to ACR.
MongoDB image I am pulling it from Docker Hub public repo during containergroup installation.
My conatiner group is getting created successfully and both the container are getting started without any issue.
But after starting the containers when I see the spring boot container startup log I can see it's failing while connecting to MongoDB container with error as com.mongodb.MongoSocketException: mongodb-container: Name or service not known
Below is my configurations.
apiVersion: 2019-12-01
location: centralus
name: azUserProfileConainerGroup
properties:
containers:
- name: mongodb-container
properties:
image: mongo:latest
resources:
requests:
cpu: 1
memoryInGb: 1.5
ports:
- port: 27017
environmentVariables:
- name: MONGO_INITDB_ROOT_USERNAME
value: az-userprofile_user
- name: MONGO_INITDB_ROOT_PASSWORD
value: az-userprofile_password
- name: MONGO_INITDB_DATABASE
value: az-userprofile-db
- name: az-userprofile-containerinstance
properties:
image: azcontainerregistry2025.azurecr.io/az-userprofile-containerinstance:4.0.0
resources:
requests:
cpu: 1
memoryInGb: 1.5
ports:
- port: 8080
environmentVariables:
- name: SPRING_PROFILES_ACTIVE
value: azstaging
osType: Linux
ipAddress:
type: Public
ports:
- protocol: tcp
port: 8080
imageRegistryCredentials:
- server: azcontainerregistry2025.azurecr.io
username: username
password: password
- server: index.docker.io
username: username
password: password
tags: {exampleTag: tutorial}
type: Microsoft.ContainerInstance/containerGroups
Spring Boot application URL configuration in azstaging profile
spring.data.mongodb.uri=mongodb://az-userprofile_user:az-userprofile_password@mongodb-container:27017/az-userprofile-db?authSource=admin
As per documentation in a container group when two containers run they will run in same network. If so we can able to call and connect the other container (MongoDB) using its container name. But in this case its not working like that. What is the issue in my above configuration?
I want to run 2 containers using Azure Container group.In which one is MongoDB and another container is Spring Boot application.Spring Boot application I have created a profile and configured a mongodburl and build docker image and pushed it to ACR.
MongoDB image I am pulling it from Docker Hub public repo during containergroup installation.
My conatiner group is getting created successfully and both the container are getting started without any issue.
But after starting the containers when I see the spring boot container startup log I can see it's failing while connecting to MongoDB container with error as com.mongodb.MongoSocketException: mongodb-container: Name or service not known
Below is my configurations.
apiVersion: 2019-12-01
location: centralus
name: azUserProfileConainerGroup
properties:
containers:
- name: mongodb-container
properties:
image: mongo:latest
resources:
requests:
cpu: 1
memoryInGb: 1.5
ports:
- port: 27017
environmentVariables:
- name: MONGO_INITDB_ROOT_USERNAME
value: az-userprofile_user
- name: MONGO_INITDB_ROOT_PASSWORD
value: az-userprofile_password
- name: MONGO_INITDB_DATABASE
value: az-userprofile-db
- name: az-userprofile-containerinstance
properties:
image: azcontainerregistry2025.azurecr.io/az-userprofile-containerinstance:4.0.0
resources:
requests:
cpu: 1
memoryInGb: 1.5
ports:
- port: 8080
environmentVariables:
- name: SPRING_PROFILES_ACTIVE
value: azstaging
osType: Linux
ipAddress:
type: Public
ports:
- protocol: tcp
port: 8080
imageRegistryCredentials:
- server: azcontainerregistry2025.azurecr.io
username: username
password: password
- server: index.docker.io
username: username
password: password
tags: {exampleTag: tutorial}
type: Microsoft.ContainerInstance/containerGroups
Spring Boot application URL configuration in azstaging profile
spring.data.mongodb.uri=mongodb://az-userprofile_user:az-userprofile_password@mongodb-container:27017/az-userprofile-db?authSource=admin
As per documentation in a container group when two containers run they will run in same network. If so we can able to call and connect the other container (MongoDB) using its container name. But in this case its not working like that. What is the issue in my above configuration?
To solve the issue where your Spring Boot application is failing to connect to the MongoDB container, set the hostname
for the MongoDB container and update the Spring Boot MongoDB URI as follows:
- name: SPRING_DATA_MONGODB_URI
- value: mongodb://az-userprofile_user:az-userprofile_password@mongodb-container:27017/az-userprofile-db?authSource=admin
Below is full yml
apiVersion: 2019-12-01
location: centralus
name: azUserProfileContainerGroup
properties:
containers:
- name: mongodb-container
properties:
image: mongo:latest
resources:
requests:
cpu: 1
memoryInGb: 1.5
ports:
- port: 27017
environmentVariables:
- name: MONGO_INITDB_ROOT_USERNAME
value: az-userprofile_user
- name: MONGO_INITDB_ROOT_PASSWORD
value: az-userprofile_password
- name: MONGO_INITDB_DATABASE
value: az-userprofile-db
- name: az-userprofile-containerinstance
properties:
image: azcontainerregistry2025.azurecr.io/az-userprofile-containerinstance:4.0.0
resources:
requests:
cpu: 1
memoryInGb: 1.5
ports:
- port: 8080
environmentVariables:
- name: SPRING_PROFILES_ACTIVE
value: azstaging
- name: SPRING_DATA_MONGODB_URI
value: mongodb://az-userprofile_user:az-userprofile_password@mongodb-container:27017/az-userprofile-db?authSource=admin
osType: Linux
ipAddress:
type: Public
ports:
- protocol: tcp
port: 8080
imageRegistryCredentials:
- server: azcontainerregistry2025.azurecr.io
username: <registry-username>
password: <registry-password>
- server: index.docker.io
username: <dockerhub-username>
password: <dockerhub-password>
tags:
exampleTag: tutorial
type: Microsoft.ContainerInstance/containerGroups
Refer to this Microsoft Docs page to set environment variables in Azure Container Instances.
Output:
environmentVariables: - name: SPRING_PROFILES_ACTIVE value: azstaging - name: SPRING_DATA_MONGODB_URI value: mongodb://az-userprofile_user:az-userprofile_password@mongodb-container:27017/az-userprofile-db?authSource=admin osType: Linux
– Sampath Commented Jan 23 at 12:35