Search code examples
docker-composemakefile

In Makefile, how to depend on the fact that the docker compose services are up?


In Makefile, I want to start services to run some commands.

How to ensure that those services are up only for some target?

Ex:

build:
    docker compose run php composer install

This command require the stack to be up and running...


Solution

  • We can detect the docker compose stack status with:

    DC_TARGETS=$(shell docker compose config --services | sort | xargs echo)
    DC_RUNNING=$(shell docker compose ps --services --status="running" )
    ifeq ($(DC_TARGETS), $(DC_RUNNING))
        DC_MUST_START=
    else
        DC_MUST_START=start-containers
    endif
    

    After, for targets that require the stack to be up:

    build: $(DC_MUST_START)
        docker compose exec php composer install
    

    PS: start-containers is a makefile target... that start the containers