microsoft / azure-container-apps

Roadmap and issues for Azure Container Apps
MIT License
362 stars 29 forks source link

Service Bindings: Postgresql connection environment variables not available to init containers #1106

Closed jag-eagle-technology closed 5 months ago

jag-eagle-technology commented 7 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. I've tried running from the service itself and this works fine, but trying to get the same environment variable connection string in my init container doesn't work - POSTGRES_CONNECTION_STRING is not set.

Steps to reproduce

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

Expected behavior Environment variable available and able to run migrations

Actual behavior Environment variable not found

Additional context

Deploying using bicep:

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: 'mycontainerreg.azurecr.io'
          identity: identity.id
        }
      ]
    }
    template: {
      serviceBinds: [
        {
          serviceId: testdb.id
          name: 'postgres'
        }
      ]
      initContainers: [
        {
          image: 'mycontainerreg.azurecr.io/testacapostgressetup:latest'
          name: 'testing-setup-container'
        }
      ]
      containers: [
        {
          image: 'mycontainerreg.azurecr.io/testacapostgres:latest'
          name: 'testing-container'
        }
      ]
    }
  }
}
ahmelsayed commented 7 months ago

Yeah, good point. There is no reason not to inject those values to the initContainer other than it was missed in the initial implementation. It should be in the next release which will rollout in 2-3 weeks.

jag-eagle-technology commented 7 months ago

Good to know, thanks

ahmelsayed commented 5 months ago

This should be deployed as of couple of weeks ago

jag-eagle-technology commented 5 months ago

The environment variable is indeed available now, however I'm finding that the connection string works in the main container app but that I get "connection refused" when trying to use it from the init container. Same C# code is used in both to retrieve and parse the connection string from the environment variables. Is there anything special going on with how the networking is setup for these init containers when using bindings?

@ahmelsayed have postgres service bind connections from initCotainers been tested? I've tried to test myself without much luck getting additional information on what might be going on.