Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WaitForCompletion for ContainerResource resources #8001

Open
1 task done
jbogard opened this issue Mar 11, 2025 · 3 comments
Open
1 task done

Support WaitForCompletion for ContainerResource resources #8001

jbogard opened this issue Mar 11, 2025 · 3 comments
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Comments

@jbogard
Copy link

jbogard commented Mar 11, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I'm converting my Docker Compose configuration to Aspire for my team and one scenario doesn't seem to be supported - using depends_on with service_completed_successfully. My specific case is OpenFGA with the following docker compose YAML:

version: '3.8'

networks:
  openfga:

services:
  postgres:
    image: postgres:14
    container_name: postgres
    networks:
      - openfga
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5

  migrate:
    depends_on:
      postgres:
        condition: service_healthy
    image: openfga/openfga:latest
    container_name: migrate
    command: migrate
    environment:
      - OPENFGA_DATASTORE_ENGINE=postgres
      - OPENFGA_DATASTORE_URI=postgres://postgres:password@postgres:5432/postgres?sslmode=disable
    networks:
      - openfga

  openfga:
    depends_on:
      migrate:
        condition: service_completed_successfully
    image: openfga/openfga:latest
    container_name: openfga
    environment:
      - OPENFGA_DATASTORE_ENGINE=postgres
      - OPENFGA_DATASTORE_URI=postgres://postgres:password@postgres:5432/postgres?sslmode=disable
      - OPENFGA_LOG_FORMAT=json
    command: run
    networks:
      - openfga
    ports:
      # Needed for the http server
      - "8080:8080"
      # Needed for the grpc server (if used)
      - "8081:8081"
      # Needed for the playground (Do not enable in prod!)
      - "3000:3000"

The openfga container has two commands, migrate and run, which are mutually exclusive. In the above setup, the migrate service runs first (after Postgres is ready). That service runs once to completion, then openfga starts up.

I looked at using WaitForCompletion but there's two issues here:

  • The migrate service runs again and again (maybe because Aspire assumes an container exiting is faulted and restarts it?)

This isn't the same as #2866 as I'm not using Azure Container Apps.

Describe the solution you'd like

First, some sort of "run once" lifetime for container resources, perhaps:

.WithLifetime(ContainerLifetime.RunOnce)?

Then the ability to wait for the migrate container to run to completion, something like:

var openFgaPgSqlUser = builder.AddParameter("openfga-pgsql-user");
var openFgaPgSqlPassword = builder.AddParameter("openfga-pgsql-password");

var openfga_pgsql = builder.AddPostgres("openfga-postgres", openFgaPgSqlUser, openFgaPgSqlPassword, 5432)
        .WithLifetime(ContainerLifetime.Persistent)
        .WithDataVolume("openfga_postgres_data");

var openfga_migrate = builder.AddContainer("openfga-migrate", "openfga/openfga")
    .WithEnvironment("OPENFGA_DATASTORE_ENGINE", "postgres")
    .WithEnvironment("OPENFGA_DATASTORE_URI",
        $"postgres://{openFgaPgSqlUser.Resource.Value}:{openFgaPgSqlPassword.Resource.Value}@{openfga_pgsql.Resource.Name}:5432/postgres?sslmode=disable")
    .WithArgs("migrate")
    .WaitFor(openfga_pgsql)
    .WithLifetime(ContainerLifetime.RunOnce)
    ;

builder.AddContainer("openfga", "openfga/openfga")
    .WithEndpoint(8080, 8080, name: "http")
    .WithEndpoint(8081, 8081, name: "grpc")
    .WithEndpoint(3000, 3000, name: "noclue")
    .WithEnvironment("OPENFGA_DATASTORE_ENGINE", "postgres")
    .WithEnvironment("OPENFGA_DATASTORE_URI",
        $"postgres://{openFgaPgSqlUser.Resource.Value}:{openFgaPgSqlPassword.Resource.Value}@{openfga_pgsql.Resource.Name}:5432/postgres?sslmode=disable")
    .WithEnvironment("OPENFGA_LOG_FORMAT", "postgres")
    .WithArgs("run")
    .WaitForCompletion(openfga_migrate)
    ;

Additional context

No response

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 11, 2025
@eerhardt eerhardt added area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 11, 2025
@davidfowl
Copy link
Member

I'm lost, why doesn't WaitForCompletion work? Aspire doesnt auto restart failing containers or processes.

@jbogard
Copy link
Author

jbogard commented Mar 12, 2025

I don't know why WaitForCompletion doesn't work. In the logs for the container, it looks like it's running over and over again:


2025-03-12T10:13:59 Waiting for resource 'openfga-postgres' to enter the 'Running' state.
2
2025-03-12T10:14:00 Waiting for resource 'openfga-postgres' to become healthy.
3
2025-03-12T10:14:00 Finished waiting for resource 'openfga-postgres'.
4
2025-03-12T10:14:00 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
5
2025-03-12T10:14:03 
6
2025-03-12T10:14:03 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
7
2025-03-12T10:14:03
stderr 2025/03/12 15:14:03 current version 5
8
2025-03-12T10:14:03
stderr 2025/03/12 15:14:03 running all migrations
9
2025-03-12T10:14:03
stderr 2025/03/12 15:14:03 migration done
10
2025-03-12T10:14:04 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
11
2025-03-12T10:14:04
stderr 2025/03/12 15:14:04 current version 5
12
2025-03-12T10:14:04
stderr 2025/03/12 15:14:04 running all migrations
13
2025-03-12T10:14:04
stderr 2025/03/12 15:14:04 migration done
14
2025-03-12T10:14:05 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
15
2025-03-12T10:14:05
stderr 2025/03/12 15:14:05 current version 5
16
2025-03-12T10:14:05
stderr 2025/03/12 15:14:05 running all migrations
17
2025-03-12T10:14:05
stderr 2025/03/12 15:14:05 migration done
18
2025-03-12T10:14:06 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
19
2025-03-12T10:14:06
stderr 2025/03/12 15:14:06 current version 5
20
2025-03-12T10:14:06
stderr 2025/03/12 15:14:06 running all migrations
21
2025-03-12T10:14:06
stderr 2025/03/12 15:14:06 migration done
22
2025-03-12T10:14:08 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
23
2025-03-12T10:14:08
stderr 2025/03/12 15:14:08 current version 5
24
2025-03-12T10:14:08
stderr 2025/03/12 15:14:08 running all migrations
25
2025-03-12T10:14:08
stderr 2025/03/12 15:14:08 migration done
26
2025-03-12T10:14:11 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
27
2025-03-12T10:14:11
stderr 2025/03/12 15:14:11 current version 5
28
2025-03-12T10:14:11
stderr 2025/03/12 15:14:11 running all migrations
29
2025-03-12T10:14:11
stderr 2025/03/12 15:14:11 migration done
30
2025-03-12T10:14:17 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
31
2025-03-12T10:14:17
stderr 2025/03/12 15:14:17 current version 5
32
2025-03-12T10:14:17
stderr 2025/03/12 15:14:17 running all migrations
33
2025-03-12T10:14:17
stderr 2025/03/12 15:14:17 migration done
34
2025-03-12T10:14:31 0a30af52611e955b3ef430274a995b1791c16430a9920e8d85e70d08d8ee8082
35
2025-03-12T10:14:31
stderr 2025/03/12 15:14:31 current version 5
36
2025-03-12T10:14:31
stderr 2025/03/12 15:14:31 running all migrations
37
2025-03-12T10:14:31
stderr 2025/03/12 15:14:31 migration done

I can see in Docker Desktop that it looks like the container starts and stops multiple times (the status icon flashes going from exited to running again and again).

Running via docker compose or pure docker (docker run --rm -it etc), I just see one set of logs where the container runs and exits:

2025-03-12 10:27:27 2025/03/12 15:27:27 current version 0
2025-03-12 10:27:27 2025/03/12 15:27:27 running all migrations
2025-03-12 10:27:27 2025/03/12 15:27:27 migration done

I've worked around this by manually calling out to docker CLI to get the behavior I want:

var openfga_migrate = builder.AddExecutable("openfga-migrate", "docker", "./",
    "run",
    "--rm",
    "openfga/openfga",
    "migrate",
    "--datastore-engine", "postgres",
    "--datastore-uri", "postgres://postgres:[email protected]:5433/postgres?sslmode=disable"
    )
    .WaitFor(openfga_pgsql);

@davidfowl
Copy link
Member

@dbreshears can you take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

No branches or pull requests

4 participants