microsoft / microsoft/aspire

Postgres does not work with volumes when hosted on ACA

Open
#9,824 4 comments 2 reactions 1 assignee Assigned to @Copilot View on GitHub
area-deployment azure-container-apps
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

I would like to reopen: https://github.com/dotnet/aspire/issues/6671 because this can now be fixed

**(Original below)**
Adding a Postgresdb using WithDataVolume fails to start when deployed to Azure.

All the resourses are created and the postgres container is deployed to the container apps spins up but goes into the state of pending because it does not have the permissions to the storage account to create the initdb:

Container log:

```
chmod: changing permissions of '/var/lib/postgresql/data': Operation not permitted
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

initdb: error: could not change permissions of directory "/var/lib/postgresql/data": Operation not permitted
fixing permissions on existing directory /var/lib/postgresql/data ...
```

### Expected Behavior

The container to start and db to be created and persisted.

### Steps To Reproduce

Create a new Aspire host project, add Postgres db and a default web api project:

```
var builder = DistributedApplication.CreateBuilder(args);

var pg = builder.AddPostgres("pgtest")
.WithDataVolume()
.AddDatabase("pg-db");

builder.AddProject("webapplication1");

builder.Build().Run();
```

Build and run azd up

### Exceptions (if any)

_No response_

### .NET Version info

_No response_

### Anything else?

As per https://github.com/microsoft/azure-container-apps/issues/520 and https://github.com/microsoft/azure-container-apps/issues/765 `mountOptions` are now GA.

I have managed to make a postgres container work in azure container apps like this:

```
var builder = DistributedApplication.CreateBuilder(args);

var postgres = builder.AddPostgres("postgres")
.WithDataVolume()
.PublishAsAzureContainerApp((infra, app) =>
{
app.Template.Volumes.First().Value!.MountOptions = "uid=999,gid=999,nobrl,mfsymlinks,cache=none,dir_mode=0750,file_mode=0750";
});

builder.Build().Run();
```

I'm no expert on this so I don't know if some of those options can be removed or should be changed, but it works, so `.WithDataVolume()` should add the volume in a way that works on azure container apps.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.