© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3mo ago•
4 replies
Carlos Saraiva

✅ 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
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
/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!!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ ✅ problem with docker compose build
C#CC# / help
3y ago
❔ doubts with docker-compose
C#CC# / help
3y ago
Problem with Docker
C#CC# / help
2y ago
Problem with docker/nginx
C#CC# / help
2y ago