user3836484
user3836484

Reputation: 205

gitlab-runner - docker in docker (dind) and push to GitLab registry

I wish to use GitLab Container Registry to temporary store my newly built Docker image; in order to have Docker function (i.e. docker login, docker build, docker push), I applied docker-in-docker executor; then from GitLab Piplelines error messages, I realize I need to place a Dockerfile at project root:-

$ docker build --pull -t $CONTAINER_TEST_IMAGE .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /builds/xxxxx.com/laravel/Dockerfile: no such file or directory

My Dockerfile includes centos:7, php, nodejs, composer and sass installations. I observe after each commit, GitLab runner will go through the Dockerfile once and install all of them from beginning, which makes the whole build stage very slow and funny - how come I just want to amend 1 word in my project but I need to install so many things again for deployment?


From my imagination, it will be nice if I can pre-build a Docker image from a Dockerfile that contains the installations mentioned above plus Docker (so that docker login, docker build and docker push can work) and stored in the GitLab-runner server, and after each commit, this image can be reused to build the new image to be pushed to GitLab Container Registry.

However, I faced 2 problems:-

1) Even I include Docker installation in the pre-build a Docker image, I cannot systemctl docker start, due to some D-bus problem

Failed to get D-Bus connection: Operation not permitted

moreover some articles also mentioned a docker in container shall not run background services;

2) when I use dind, it will require a Dockerfile at project root; with the pre-build a Docker image, actually I have nothing to do with this Dockerfile at project root; hence is dind a wrong option?

Acutally, what is the proper way to push a Laravel project image to GitLab Container Registry? (where to place those npm install and composer install commands?)

image: docker:latest
services:
- docker:dind

stages:
  - build
  - test
  - deploy

variables:
  CONTAINER_TEST_IMAGE: xxxx
  CONTAINER_RELEASE_IMAGE: yyyy

before_script:  
  - docker login -u xxx -p yyy registry.gitlab.com 

build:
  stage: build
  script:
    - npm install here?
    - docker build --pull -t $CONTAINER_TEST_IMAGE .
    - docker push $CONTAINER_TEST_IMAGE

Upvotes: 0

Views: 1838

Answers (1)

aitorhh
aitorhh

Reputation: 2451

There are many questions in your post. I would like to target them as follows:

  1. You can pre-build a docker image and then use it in your gitlab-ci.yaml file. This can be used to add your specific dependencies.

    image: my custom image
    services:
     - docker:dind
    

    Important to add the service to the configuration.

  2. You problem about trying to run the docker service inside the gitlab-ci.yml. You actually don't need to do that. Gitlab exposes the docker engine to the executor (either via unix:///var/run/docker.sock or tcp://localhost:2375/). Note, that if the runners are executed in a kubernetes environment, you need to specify the DOCKER_HOST as follows:

    variable:
       DOCKER_HOST: tcp://localhost:2375/
    
  3. You question about where to place npm install is more a fundamental question about how docker images are build. In short, npm install should be place in the Dockerfile. For a long description, please this for example.

Some references:

Upvotes: 0

Related Questions