Docker Compose Wait for X contaier before starting Y

How can I setup a docker container in docker compose to wait for another container before starting.

I have tried using “depends_on” and health checks but cant seem to get it right.

What i think could achieve my goal in my situation is adding a health check to make sure /media/data is fully accessible or maybe even attaching and detaching before starting contaier1 but unfortunately I have no clue how to set this up properly.

here is an example of my setup, any suggestions are greatly appreciated I look forward to your response.

Host: ProxMox
OS: Ubuntu 22.04 VM
Docker compose Version: 2.20
Portianer Version: 2.18.4

container1:
    image: container1/contaier1:latest
    stdin_open: true
    tty: true
    volumes:
      - /home/dlone/Configs/config1/config:/config 
      - /home/dlone/Configs/config1/log:/log # logs will be here on the host
      - /media/data:/data:shared 
    environment:
      - TZ=America/Los_Angeles
    devices:
      - /dev/fuse:/dev/fuse:rwm
    cap_add:
      - SYS_ADMIN
    security_opt:
      - apparmor:unconfined
      - no-new-privileges
    healthcheck:
      interval: 10s
      timeout: 5s
      retries: 5

  container2:
    depends_on: 
      container1: 
        condition: service_healthy  
    image: container2/container2:latest
    container_name: container2
    network_mode: bridge
    devices:
      - /dev/dri:/dev/dri
    ports:
      - 32400:32400
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=America/Los_Angeles
    volumes:
      - /home/dlone/Configs/Config2:/config
      - /media/data/:/media/data
    healthcheck:
      interval: 10s
      timeout: 5s
      retries: 5
    restart: unless-stopped

The healthcheck lacks the actual health check :slight_smile:

  container1:
    ...
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

You can run a command, or start a script. The test expects successful checks to return status code 0, and failed checks to return status code 1.

See:
https://docs.docker.com/compose/compose-file/05-services/#healthcheck

i forgot to mention i am using Portianer, when adding the following to my portainer compose i was not able to deploy the stack anymore. not sure whats going on there.

 test: ["CMD", "curl", "-f", "http://localhost"]

Doesn’t Portainer show error messages? If nowhere else, you can check the container logs.
Of course, you need to have curl in the container, but the lack of curl should just make the healthcheck fail, not prevent you from deploying the app I guess.