简体   繁体   中英

GitLab CI & Docker: wait till docker-compose up is finished

I'm trying to build a CI Pipeline with Gitlab CI/CD. My project is a really simple API based on Symfony. To create a consistent environment i'm using docker-compose with four very simple containers (nginx,PHP,MySQL & composer). My.gitlab-ci.yaml looks like this:

stages:
    - setup

setup:
  stage: setup

  before_script:
    - docker-compose up -d

  script:
    - sleep 15
    - docker-compose exec -T php php bin/console doctrine:schema:create
  after_script:
    - [...] 
    - docker-compose down

The problem I'm encountering is that the ci script does not wait till the docker-compose up -d is finished. To bypass this I've added this stupid sleep.

Is there a better way to do this?

To save some time for the people searching it, I implemented the solution commented by @gbrener.

The idea: Wait until getting the log that shows that the container is up, then continue the pipeline.

1 - Get the log to be the checkpoint. I used the last log of my container. Ex: Generated backend app.

2 - Get the container name. Ex: ai-server-dev.

3 - Create a sh script like the below and name it something.sh. Ex:

#!/bin/bash
while ! docker logs ai-server-dev --tail=1 | grep -q "Generated backend app";
do
    sleep 10
    echo "Waiting for backend to load ..."
done

4 - Replace the 'sleep 15' with 'sh wait_server.sh' as in the question to run the script in your pipeline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM