microsoft / azure-container-apps

Roadmap and issues for Azure Container Apps
MIT License
355 stars 27 forks source link

Unable to connect to bound Postgres test service from init container #1155

Open jag-eagle-technology opened 2 months ago

jag-eagle-technology commented 2 months ago

This issue is a: (mark with an x)

Issue description

I'm trying to run database migration scripts on a Postgresql database bound to my container app. My setup service and main container use exactly the same code to retrieve and parse the postgres connection string. I'm able to connect to and modify the database from the service itself, but when trying to connect in the init container I receive "connection refused".

Followup from #1106 - environment variable is now available but doesn't seem to work.

Steps to reproduce

As above - can provide further info if required (replicated using basic ASP.NET Service container & .NET Console App / DBUP init container).

Expected behavior Able to connect to postgres and run migrations in init container

Actual behavior Receive error "connection refused"

Additional context

Deploying using bicep:

resource testdb 'Microsoft.App/containerApps@2023-04-01-preview' = {
  name: 'testdb'
  location: location
  properties: {
    environmentId: environment.id
    configuration: {
      service: {
        type: 'postgres'
      }
    }
  }
}

resource testapp 'Microsoft.App/containerApps@2023-05-01' = {
  name: 'testapp'
  location: location
  dependsOn: [
    roleAssignment
  ]
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${identity.id}': {}
    }
  }
  properties: {
    managedEnvironmentId: environment.id
    configuration: {
      ingress: {
        targetPort: 8080
        external: true
      }
      registries: [
        {
          server: 'myreg.azurecr.io'
          identity: identity.id
        }
      ]
    }
    template: {
      serviceBinds: [
        {
          serviceId: testdb.id
          name: 'postgres'
        }
      ]
      initContainers: [
        {
          image: 'myreg.azurecr.io/testacapostgressetup:rev24'
          name: 'testing-setup-container'
        }
      ]
      containers: [
        {
          image: 'myreg.azurecr.io/testacapostgres:rev16'
          name: 'testing-container'
        }
      ]
    }
  }
}