azure - Spring Boot application is not resolving MongoDB container host when running in Container Group - Stack Overflow

admin2025-04-26  7

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?

Share Improve this question asked Jan 15 at 3:49 springbootlearnerspringbootlearner 1,4105 gold badges28 silver badges58 bronze badges 5
  • Deploy a multi-container group using a YAML file – Sampath Commented Jan 15 at 10:49
  • I am already using multi container group yaml – springbootlearner Commented Jan 15 at 13:28
  • At the bottom, enter your Environment Variable ( your connection string) like this – Sampath Commented Jan 20 at 4:13
  • can you use 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
  • Output – Sampath Commented Jan 24 at 3:40
Add a comment  | 

1 Answer 1

Reset to default 0

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:

转载请注明原文地址:http://anycun.com/QandA/1745600401a91014.html