infaz.r
infaz.r

Reputation: 123

Deploying Postgres DB in Azure Container App

Hi I am new to Azure and I got a requirement to deploy Postgres DB to Azure Container app. I know its not a good practice to host a DB inside a containerize environment.

I followed this link from the Microsoft documentation to deploy the app and following are my configurations,

enter image description here

I created a VNET since I need to select TCP as the Ingress type and mapped the target port and and exposed port port as 5432 to 5432

enter image description here

After I deploy the application and try to connect to the DB using Pgadmin I am getting the following error.

enter image description here

I want to make sure the Postgres DB running successfully inside the Azure Container App and need to connect from Pgadmin.

Upvotes: 3

Views: 5434

Answers (2)

t.j.
t.j.

Reputation: 1347

In addition to the accepted answer, MS has since updated their documentation with examples for deploying an instance of pgweb (the "Simple web-based and cross platform PostgreSQL database explorer").

The documentation currently (1/2024) advises "See Bicep or azd example", which contains the following bicep file (postgres-dev.bicep)...

resource pgweb 'Microsoft.App/containerApps@2023-04-01-preview' = {
  name: 'pgweb'
  location: location
  properties: {
    environmentId: appEnvironment.id
    configuration: {
      ingress: {
        external: true
        targetPort: 8081
      }
    }
    template: {
      serviceBinds: [
        {
          serviceId: postgres.id
          name: 'postgres'
        }
      ]
      containers: [
        {
          name: 'pgweb'
          image: 'docker.io/sosedoff/pgweb:latest'
          command: [
            '/bin/sh'
          ]
          args: [
            '-c'
            'PGWEB_DATABASE_URL=$POSTGRES_URL /usr/bin/pgweb --bind=0.0.0.0 --listen=8081'
          ]
        }
      ]
    }
  }
}

output pgwebUrl string = 'https://${pgweb.properties.configuration.ingress.fqdn}'

"Deploy the bicep template with the... command."

az deployment group create -g $RESOURCE_GROUP \
    --query 'properties.outputs.*.value' \
    --template-file postgres-dev.bicep

"The Bicep command returns a URL. Copy this URL to your browser to visit the deployed site."

Upvotes: 1

ahmelsayed
ahmelsayed

Reputation: 7402

There are 2 problems here:

  • Your app definition is incorrect (particularly around "Command override")
  • You could fix that part and Postgres would start, but you will run into a persistent storage issue. Azure ContainerApps today only supports AzureFiles SMB shares for persistent storage. However, Postgres requires hardlinks and AzureFiles SMB doesn't support hardlinks. So you won't be able to mount a persistent storage that works for postgres. Once ContainerApps can mount AzureFiles NFS shares, this will work.

Regarding the first problem, the "Command override" is for overriding the command running in your docker image, not the docker command itself.

So those values will need to be put in the environment variables section below:

like:

enter image description here

or using the azure cli

az containerapp create \
  --name $POSTGRES_INSTANCE_NAME \
  --resource-group $RESOURCE_GROUP \
  --environment $CONTAINERAPPS_ENVIRONMENT  \
  --image docker.io/postgres:15 \
  --secrets pgpass="$POSTGRES_PASSWORD" \
  --env-vars POSTGRES_USER="$POSTGRES_USER" POSTGRES_DB="$POSTGRES_DB" POSTGRES_PASSWORD=secretref:pgpass \
  --transport tcp \
  --target-port 5432 \
  --ingress external \
  --min-replicas 1 \
  --max-replicas 1

you could change --ingress external to --ingress internal then deploy pgadmin on the same environment. Then pgadmin should be able to reach postgres on $POSTGRES_INSTANCE_NAME:5432

to deploy pgadmin

az containerapp create \
  --name pgadmin \
  --resource-group $RESOURCE_GROUP \
  --environment $CONTAINERAPPS_ENVIRONMENT  \
  --image dpage/pgadmin4:6.15 \
  --secrets pgpass="$PGADMIN_PASSWORD" \
  --env-vars PGADMIN_DEFAULT_EMAIL="$PGADMIN_EMAIL" PGADMIN_LISTEN_PORT="8080" PGADMIN_DEFAULT_PASSWORD=secretref:pgpass \
  --transport http \
  --target-port 8080 \
  --ingress external \
  --min-replicas 1 \
  --max-replicas 1

Upvotes: 4

Related Questions