4

When I use gitlab with docker in the log of the job I can get this information:

Running with gitlab-ci-multi-runner 9.5.0 (413da38)
  on platform-docker-orc (2c06225e)
Using Docker executor with image registry:5000/local_image: ...
Using docker image sha256:db4434f2a9c3529af30397031df5bc1277f13882e0f6613a8c8f9c059645c04d for predefined container...
Pulling docker image registry:5000/local_image ...
Using docker image registry:5000/local_image ID=sha256:8d1cac8ae6371b01505e9cd3aaf654696cc144117a9c89dcd21cf4c0d9cfa709 for build container...
Running on runner-2c06225e-project-99-concurrent-0 via a96c0c765ce7...

How can I get the container id where the gitlab job is executed?

JuanPablo
  • 23,792
  • 39
  • 118
  • 164

1 Answers1

7

You can obtain the container id by leveraging the labels on the container:

docker ps -q -f "label=com.gitlab.gitlab-runner.job.id=$CI_JOB_ID" -f "label=com.gitlab.gitlab-runner.type=build"

Specifying the label=com.gitlab.gitlab-runner.type=build filter will limit it to the build container. Else you will also get services container defined as part of your job.

Kevin Wittek
  • 1,369
  • 9
  • 26
  • Do you know if this is documented anywhere? Finding it hard to believe it isn't exposed in a variable similar to `$CI_JOB_ID`.. can't find anything, though. – Damien Roche Nov 25 '18 at 13:36
  • 1
    I haven't found this documented, but you can just checkout the labels from running containers and see what's useful for your use case. – Kevin Wittek Nov 25 '18 at 15:33
  • 1
    I ended up using your snippet which was the final piece of a 6 hour long nightmare around container networking on my CI server... so thanks! :) – Damien Roche Nov 25 '18 at 15:40
  • 3
    Just a quick note since I found myself here again -- the above filter will catch any services you also have specified in gitlab. I had to add another filter: `-f "label=com.gitlab.gitlab-runner.type=build"` – Damien Roche Dec 28 '18 at 07:17
  • 1
    Thanks, that's great, I'll edit the answer accordingly. – Kevin Wittek Dec 29 '18 at 11:25