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

1

9

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?

Presidio answered 25/3, 2018 at 17:0 Comment(5)
the script waits till docker-compose up -d is finished, but it only tell docker container to start them and works good. Unfortunately this command doesnt know what is inside your containers. That is why you should implement HEALTHCHECKDatnow
@MazelTov thanks man, this was the impulse i needed!Presidio
Hey can you show us your solution for this. I am having the same problemToffic
Another possible solution would be to poll docker-compose logs, with the --tail option to limit output size.Zugzwang
@MazelTov masel tov!Alexandrite
F
3

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.

Fernferna answered 7/4, 2022 at 21:30 Comment(2)
Thanks for this, simple and does the job without any crazy dependenciesAlfrediaalfredo
I faced a thing where docker logs prints to the stderr not stdout so I had to do this: docker logs ai-server-dev --tail=1 2>&1 | grep .. to remap stderr to stdoutMachellemachete

© 2022 - 2024 — McMap. All rights reserved.