Search code examples
mongodbazure-container-apps

How to host mongo db as azure container app?


Could someone please help me with working example? As far as I understand its ingress of TCP transport has to be enabled on container environment level.

Thanks in advance


Solution

  • This worked for me.

    targetScope = 'resourceGroup'
    param location string = resourceGroup().location
    
    resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
      name: uniqueString(resourceGroup().name)
      location: location
      sku: {
        name: 'Premium_LRS'
      }
      kind: 'FileStorage'
      properties: {
        minimumTlsVersion: 'TLS1_2'
        supportsHttpsTrafficOnly: true
        allowBlobPublicAccess: false
        allowSharedKeyAccess: true
        publicNetworkAccess: 'Enabled'
        networkAcls: {
          defaultAction: 'Allow'
          bypass: 'AzureServices'
        }
      }
    
      resource fileService 'fileServices@2022-09-01' = {
        name: 'default'
        resource fileShare 'shares@2022-09-01' = {
          name: 'mongodb-share'
          properties: {
            enabledProtocols: 'SMB'
            accessTier: 'Premium'
          }
        }
      }
    }
    
    resource appEnvironment 'Microsoft.App/managedEnvironments@2023-04-01-preview' = {
      name: 'app-environment'
      location: location
      properties: {
        appLogsConfiguration: {
          destination: 'azure-monitor'
        }
      }
    
      resource azureFilesStorage 'storages@2022-11-01-preview' = {
        name: 'azurefilesstorage'
        properties: {
          azureFile: {
            accountName: storageAccount.name
            shareName: storageAccount::fileService::fileShare.name
            accessMode: 'ReadWrite'
            accountKey: storageAccount.listKeys().keys[0].value
          }
        }
      }
    }
    
    resource mongodb 'Microsoft.App/containerApps@2023-04-01-preview' = {
      name: 'mongodb'
      location: location
      properties: {
        environmentId: appEnvironment.id
        configuration: {
          ingress: {
            external: false
            transport: 'tcp'
            targetPort: 27017
          }
        }
        template: {
          containers: [
            {
              name: 'mongodb'
              image: 'bitnami/mongodb:latest'
              env: [
                {
                  name: 'MONGODB_ROOT_USER'
                  value: 'root'
                }
                {
                  name: 'MONGODB_ROOT_PASSWORD'
                  value: 'password'
                }
              ]
              volumeMounts: [
                {
                  mountPath: '/bitnami/mongodb'
                  volumeName: 'mongo-volume'
                }
              ]
              resources: {
                cpu: json('1.0')
                memory: '2.0Gi'
              }
            }
          ]
          scale: {
            minReplicas: 1
            maxReplicas: 1
          }
          volumes: [
            {
              mountOptions: 'dir_mode=0777,file_mode=0777,uid=1001,gid=1001,mfsymlinks,nobrl'
              name: 'mongo-volume'
              storageName: appEnv::azureFilesStorage.name
              storageType: 'AzureFile'
            }
          ]
        }
      }
    }
    
    resource mongodbExpress 'Microsoft.App/containerApps@2023-04-01-preview' = {
      name: 'mongodb-express'
      location: location
      properties: {
        environmentId: appEnvironment.id
        configuration: {
          ingress: {
            external: true
            transport: 'http'
            targetPort: 8081
          }
        }
        template: {
          containers: [
            {
              name: 'mongodb'
              image: 'mongo-express:latest'
              env: [
                {
                  name: 'ME_CONFIG_MONGODB_URL'
                  value: 'mongodb://root:password@mongodb:27017'
                }
                {
                  name: 'ME_CONFIG_MONGODB_ADMINUSERNAME'
                  value: 'root'
                }
                {
                  name: 'ME_CONFIG_MONGODB_ADMINPASSWORD'
                  value: 'password'
                }
              ]
              resources: {
                cpu: json('1.0')
                memory: '2.0Gi'
              }
            }
          ]
          scale: {
            minReplicas: 1
            maxReplicas: 1
          }
        }
      }
    }