5

I am using gitlab-ci with docker:dind as a service.

problem

I am trying in Gitlab-CI run npm run build followed by docker build.

I am able this way to build using docker in docker this way:

This is my runner config.toml:

$ cat /etc/gitlab-runner/config.toml 
concurrent = 4
check_interval = 0

[[runners]]
  name = "developers_gitlab_school-gitlab-runner-docker"
  url = "https://school.domain.com"
  token = "cd09f40c6a4....a44751fec795e35"
  executor = "docker"
  builds_dir = "/mnt/mesos/sandbox/builds"
  cache_dir = "/mnt/mesos/sandbox/cache"
  [runners.docker]
    tls_verify = false
    image = "docker:latest"
    privileged = true
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]

This is an example of .gitlab-ci.yml

image: docker:latest
# image: mcasimir/dind-node-build-runner:latest

variables:
  DOCKER_DRIVER: overlay2

services:
  - docker:dind

before_script:
  - docker info
  - docker --version
  - docker login -u ${REGISTRY_USERNAME} -p ${REGISTRY_PASSWORD} ${REGISTRY_URL}

stages:
  - build

# Job: Build
build_dev:
  stage: build
  script:
    - docker build -t group/mail-server/dev:${CI_JOB_ID} .
  only:
    - dev
  tags:
    - docker

build_master:
  stage: build
  script:
    - docker build -t domain/mail-server:${CI_JOB_ID} .
    - docker build -t domain/mail-server:latest .
  only:
    - master
  tags:
    - docker
  1. When I use an image with docker and npm for my build mcasimir/dind-node-build-runner:latest I have :

    Cannot connect to the Docker daemon. Is the docker daemon running on this host?. 
    
  2. When I use image docker:latest, docker-in-docker work fine but I still need npm.

question:

Because the Dockerfile of docker:latest is not public, and because I wasn't able to use apt-get from this image, I would like to know:

  • If there a way to have an image that can run docker and npm in gitlab-ci ?
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204

2 Answers2

12

Because it is an alpine-based image, you don't have apt-get, you have apk. So with the default docker:latest just add this apk --update add nodejs:

before_script:
    - apk --update add nodejs npm

And you are ready with dind, nodejs and npm.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
Robert
  • 33,429
  • 8
  • 90
  • 94
  • May I know the difference between doing that and just extending the `docker:latest` with the content of [this official alpine node Dockerfile](https://github.com/nodejs/docker-node/blob/581eebd097343c9f1c1ceb5260cd2ec770410e29/7.10/alpine/Dockerfile) ? – Dimitri Kopriwa May 26 '17 at 21:29
  • That alpine-node doesn't have a docker in docker install. That's the regular node base image. Does it make sense? – Robert May 26 '17 at 21:34
  • I have created my own Docker image using `FROM docker:latest` and I appended the content of this file. I think it does but maybe I am wrong. – Dimitri Kopriwa May 26 '17 at 21:36
  • That's ok if you want it. It's more robust but it's complex. Did it work for you? – Robert May 26 '17 at 21:38
  • Yes it worked, but I have another problem, I can't use the gitlab-ci example of ssh-agent login with alpine – Dimitri Kopriwa May 26 '17 at 23:05
  • 1
    Do you mean [this](https://docs.gitlab.com/ee/ci/ssh_keys/README.html#ssh-keys-when-using-the-docker-executor)? Remember that apk is the equivalent of apt get – Robert May 26 '17 at 23:11
  • apk --update add openssh-client https://pkgs.alpinelinux.org/contents?branch=edge&name=openssh-client&arch=x86_64&repo=main – Robert May 26 '17 at 23:14
  • Thanks a lot. And by any chance maybe you know how to translate `ssh-add <(echo "$SSH_PRIVATE_KEY")` for **sh** ? – Dimitri Kopriwa May 26 '17 at 23:20
  • Shoud be `cat $SSH_PRIVATE_KEY | ssh-add` – Robert May 26 '17 at 23:34
  • cat is a bit insecure. I have opened a new question for this https://stackoverflow.com/questions/44211396/is-there-a-ssh-add-linux-alpine-one-liner – Dimitri Kopriwa May 26 '17 at 23:34
  • And cat is incorrect because the var is not a filename. Can you install bash for the meantime? – Robert May 26 '17 at 23:44
-1

If we look into the source code for the docker container we can see one variable being set up: $DOCKER_HOST, and that's all you need just addexport DOCKER_HOST='tcp://docker:2375' as a step in your build or set DOCKER_HOST as a global variable. And then you can use any image you want, not just docker:latest.

dockeralpine:
  image: alpine
  services:
   - docker:dind
  variables:
    DOCKER_HOST: "tcp://docker:2375"
  tags: 
   - docker
  script:
   - apk --update add docker
   - docker ps

dockerubuntu:
  image: ubuntu
  services:
   - docker:dind
  variables:
    DOCKER_HOST: "tcp://docker:2375"
  tags: 
   - docker
  script:
   - apt update
   - apt install -yqq docker.io
   - docker ps
Jakub Kania
  • 15,665
  • 2
  • 37
  • 47