When building docker image with GitLab CI, wait until docker daemon starts

background

When I was investigating the cause of moss in Job that builds docker image with GitLab CI, I found that it was moss when I did docker info.

docker info failed.png

docker is a client-server application, but if you type the docker info command before the docker daemon on the server side starts, you will get angry with" I don't have a daemon ... ".

Cannot connect to the Docker daemon at tcp://docker:2375. Is the docker daemon running?

The docker in docker dind is used correctly, and it's really just a delay in booting, so if you wait like sleep 10, the build will pass. However, I don't want to put on a mysterious sleep, so I want to wait until the daemon starts.

What i did

I wrote a shell script and added it to the project. Fortunately, I know that docker info will fail without daemon, so I will use it.

wait.sh


#!/bin/sh

#A script that waits for the dind docker daemon to start
while :
do
    if docker info > /dev/null 2>&1; then
        break
    fi
    echo 'waiting docker daemon...'
    sleep 1
done

exit 0

Put the script before docker info in .gitlab-ci.yml. Don't forget chmod to make it executable.

yml:.gitlab-ci.yml


before_script:
  - chmod u+x wait.sh && ./wait.sh
  - docker info

result

The build no longer fails because it waits for startup.

docker daemonを待つようになった

Recommended Posts

When building docker image with GitLab CI, wait until docker daemon starts
Image flew when updating Docker with WSL2
Clone your own web app on GitLab when building a Docker image
Until you try running Apache Kafka with docker image
Solution when building Docker image stops at time zone selection
Measures for permissions when building MySQL with Docker on WSL2
Set up GitLab with docker
Run lambda with custom docker image
Restart apache with docker php-apache image
Problems I was addicted to when building the digdag environment with docker