Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-in-Docker with Gitlab Shared runner for building and pushing docker images to registry

Been trying to set-up Gitlab CI which can build a docker image, and came across that DinD was enabled initially only for separate runners and Blog Post suggest it would be enabled soon for shared runners,

Running DinD requires enabling privileged mode in runners, which is set as a flag while registering runner, but couldn't find an equivalent mechanism for Shared Runners

like image 415
Somasundaram Sekar Avatar asked Sep 21 '16 06:09

Somasundaram Sekar


People also ask

How does Docker integrate with GitLab?

Run your CI/CD jobs in Docker containers. For example, you can tell GitLab CI/CD to use a Node image that's hosted on Docker Hub or in the GitLab Container Registry. Your job then runs in a container that's based on the image. The container has all the Node dependencies you need to build your app.

Does GitLab have a Docker registry?

Context. In milestone 8.8, GitLab launched the MVC of the Container Registry. This feature integrated the Docker Distribution registry into GitLab so that any GitLab user could have a space to publish and share container images.


1 Answers

The shared runners are now capable of building Docker images. Here is the job that you can use:

stages:
  - build
  - test
  - deploy

# ...
# other jobs here
# ...

docker:image:
  stage: deploy
  image: docker:1.11
  services:
    - docker:dind
  script:
    - docker version
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    # push only for tags
    - "[[ -z $CI_BUILD_TAG ]] && exit 0"
    - docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_BUILD_TAG
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker push $CI_REGISTRY_IMAGE:$CI_BUILD_TAG

This job assumes that you are using the Container Registry provided by Gitlab. It pushes the images only when the build commit is tagged with a version number.

  • Documentation for Predefined variables.

  • Note that you will need to cache or generate as temporary artifacts of any dependencies for your service which are not committed in the repository. This is supposed to be done in other jobs. e.g. node_modules are not generally contained in the repository and must be cached from the build/test stage.

like image 52
activatedgeek Avatar answered Sep 19 '22 12:09

activatedgeek