Search code examples
postgresqlazuredockerpgadmin-4azure-container-apps

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.


Solution

  • 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