✅ Docker Compose problem with command execution after start

Hi guys, I'm trying to execute bash commands from my .NET API to migrate my entities into my PostgreSQL database in another container, here is how my docker-compose looks like:
services:
  apitest:
    container_name: apitest
    hostname: apitest
    post_start:
      - command: ["/config/api.sh"]
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      postgresdb: 
        condition: service_healthy
    ports:
      - "5000:8080"
      - "5001:5001"
    networks:
      - anydb
    volumes:
      - "./config:/config"
  postgresdb:
    container_name: postgresdb
    hostname: postgresdb
    image: postgres:latest
    environment:
      - POSTGRES_DB=pets
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - "./.containers/postgresdb:/var/lib/postgresql/docker"
    ports:
      - "5432:5432"
    networks:
      - anydb
    healthcheck:
      test: ["CMD-SHELL", "sh -c 'pg_isready -U postgres -d pets'"]
      interval: 10s
      timeout: 3s
      retries: 3
networks: 
  anydb:
    name: anydb
    driver: bridge

When I run the docker compose it throws an exit code of 255 from my api container, but if I go inside it after the containers are running and execute the /config/api.sh command again it executes fine and run the migrations. Am I doing the post_start wrong? Shouldn't the API container only run when the database is healthy? How can I fix this or maybe check in my shell script if the database is accessible? Thanks!!
Was this page helpful?