skip to Main Content

I have a docker compose setup that I use for running tests on a component. I start the necessary background services (ie. DB) and run the tests. As I am running this in an Azure CI/CD pipeline, I use the --abort-on-container-exit flag to ensure the process stops after the tests are executed.

Recently, my project started using spicedb for RBAC and I have included some tests for those functions as well. For this, I added a postgresql db and a spicedb service to my docker compose. My new docker compose file looks like this:

services:
    mssql-db:
        build: 
            context: .
            dockerfile: registry.db.local.mssql.arm64.Dockerfile
        ports:
            - "1453:1433"
        container_name: mssql-local-bcentral-auth-api
        environment: 
          ACCEPT_EULA: Y
          SA_PASSWORD: pwd_123
    database:
        image: "postgres"
        ports:
        - "5432:5432"
        container_name: postgres-db-auth-api
        environment:
        - "POSTGRES_PASSWORD=postgrespw"
        - "POSTGRES_DB=spicedb"
        depends_on:
        - "mssql-db"
        healthcheck:
            test: ["CMD-SHELL", "pg_isready -U postgres"]
            interval: 5s
            timeout: 5s
            retries: 5
    migrate:
        image: "authzed/spicedb"
        command: "migrate head"
        container_name: migrate-spicedb
        restart: "on-failure"
        environment:
        - "SPICEDB_DATASTORE_ENGINE=postgres"
        - "SPICEDB_DATASTORE_CONN_URI=postgres://postgres:postgrespw@postgres-db-auth-api:5432/spicedb?sslmode=disable"
        depends_on:
          database:
            condition: service_healthy
    spicedb:
        image: "authzed/spicedb"
        command: "serve"
        restart: "always"
        ports:
        - "8080:8080"
        - "9090:9090"
        - "50051:50051"
        environment:
        - "SPICE_DB_HOST_CONF=spicedb"
        - "SPICEDB_GRPC_PRESHARED_KEY=foobar"
        - "SPICEDB_DATASTORE_ENGINE=postgres"
        - "SPICEDB_DATASTORE_CONN_URI=postgres://postgres:postgrespw@postgres-db-auth-api:5432/spicedb?sslmode=disable"
        depends_on:
          - migrate
    auth-api:
        image: ballerina-central/v2/auth-api-build
        build: 
            context: .
            dockerfile: registry.auth.api.build.Dockerfile
        depends_on:
            - spicedb
        command: [ "bal", "test"]
        environment:
            - SPICE_DB_HOST_CONF=spicedb
            - SPICEDB_GRPC_PRESHARED_KEY=foobar
            - TOKENS_DB_PASSWORD=pwd_123
            - ASGARDEO_SERVICE_PROVIDER_WEB_OAUTH_CLIENT_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxx
            - ASGARDEO_SERVICE_PROVIDER_WEB_OAUTH_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

The problem I have now is that, there is this migrate service which migrates the datastore to the desired version for spicedb, however, this service exits with code 0 after the migration is done. As I have passed the --abort-on-container-exit flag, this causes the docker compose to exit before the tests are executed. If I remove the flag, the docker compose process does not stop after the tests are executed, which does not work for me as the pipeline has to finish after the tests are passed.

This is how I declare this step in my Azure pipeline:

  - task: DockerCompose@1
    displayName: test auth-api
    inputs:
      action: Run a Docker Compose command
      dockerComposeFile: projects/auth-api-docker-compose.yml
      dockerComposeCommand: up --build --force-recreate --abort-on-container-exit --remove-orphans
      dockerComposePath: "/usr/libexec/docker/cli-plugins/docker-compose"
      projectName: "test-auth-api"

I want to stop docker compose only when the auth-api container has finished running the tests. Is there a way to do this?

2

Answers


  1. Chosen as BEST ANSWER

    It seems like docker compose up is not meant to be used for one-off tasks as mentioned in this answer. So I changed my pipeline step to use docker run instead:

    - task: DockerCompose@1
        displayName: test auth-api
        inputs:
          action: Run a Docker Compose command
          dockerComposeFile: projects/auth-api-docker-compose.yml
          dockerComposeCommand: run --rm --build --remove-orphans auth-api
          dockerComposePath: "/usr/libexec/docker/cli-plugins/docker-compose"
          projectName: "test-auth-api"
    

    This runs the tests as expected but I faced two new problems with this approach:

    1. Adding the --rm flag removes the auth-api container but not its depends_on containers. This meant my mssql container was hanging and it affected the consequent pipeline steps. To overcome this I had to add another step to run docker compose down to make sure all the containers are removed after the run.

    2. docker compose run olny shows the log output of the container that we run with the command. I cannot see the log output from any of my depends_on containers. I have to detach from the run and observe the logs via a separate command if I want to see the logs from all my containers. This does not work for me in my scenario.

    Ultimate solution for me was to combine the migrate and spicedb tasks to a single service in order to avoid the early exit. This cannot be done with the authzed/spicedb image as it does not offer shell access. Need to use authzed/spicedb:debug-latest and give bash -c "migrate head && serve" as the command.


  2. You can try to use the –exit-code-from flag to specify which service’s exit code should be used as the exit code for the docker-compose command.

    enter image description here

    Sample task below:

    - task: DockerCompose@1
      displayName: test auth-api
      inputs:
        action: Run a Docker Compose command
        dockerComposeFile: projects/auth-api-docker-compose.yml
        dockerComposeCommand: up --build --force-recreate --abort-on-container-exit --remove-orphans --exit-code-from auth-api
        dockerComposePath: "/usr/libexec/docker/cli-plugins/docker-compose"
        projectName: "test-auth-api"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search