37

I am trying to build my docker image within the gitlab ci pipeline.

However it is not able to find the docker command.

/bin/bash: line 69: docker: command not found ERROR: Job failed: error executing remote command: command terminated with non-zero exit code: Error executing in Docker Container: 1

.gitlab-ci.yml

stages:
  - quality
  - test
  - build
  - deploy

image: node:8.11.3

services:
  - mongo
  - docker:dind

before_script:
- npm install

quality:
  stage: quality
  script:
  - npm run-script lint

test:
  stage: test
  script:
  - npm run-script test

build:
  stage: build
  script:
  - docker build -t server .

deploy:
  stage: deploy
  script:
  - echo "TODO deploy push docker image"
Kay
  • 17,906
  • 63
  • 162
  • 270
  • This seems to advise against using DinD for general CI purposes https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/ Might try to follow that process and avoid this being an issue at all – Thymine Jul 05 '18 at 17:15
  • @Thymine the proposed alternative is to mount the docker socket when you start the "external" container, which you cannot do in gitlab-ci. – JulienD Jul 29 '19 at 15:32

4 Answers4

25

you need to choose an image including docker binaries

image: gitlab/dind

services:
  - docker:dind
Hieu Vo
  • 3,105
  • 30
  • 31
  • 9
    Gitlab/dind is deprecated (last update 5 years ago). Official docker dind image should be used instead (called just "docker") – Tobias Mühl Dec 22 '21 at 17:43
7

You have 2 options to fix this. You will need to edit your config.toml file (located wherever you installed your gitlab runner).

OPTION 1

in config.toml:

privileged = true

in .gitlab-ci.yml:

myjob:
  stage: myjob
  image: docker:latest
  services:
    - docker:18.09.7-dind # older version that does not need demand TLS (see below)

OPTION 2

in config.toml:

privileged = true
volumes = ["/certs/client", "/cache"]

in .gitlab-ci.yml:

myjob:
  stage: myjob
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2 # not sure if this is needed
    DOCKER_TLS_CERTDIR: "/certs"

IMPORTANT: ONCE YOU HAVE MADE THE CHANGES TO config.toml YOU WILL PROBABLY NEED TO RESTART THE GITLAB RUNNER (which may vary depending on OS) - I DID RESTART MINE, NOT SURE WHAT WOULD HAPPEN IF YOU DID NOT RESTART IT!

Instructions for restarting gitlab runner are here ... https://docs.gitlab.com/runner/commands/ ... basically gitlab-runner restart but on Windows I had to use Windows "Services" to restart it

Why this problem?

priviledged=true gets rid of the docker: command not found problem

However, docker:dind now requires TLS certs (whatever they are). If you are happy with an older docker version then you can use OPTION 1. If you want the latest you need to setup Gitlab CLI to use them which is OPTION 2. J.E.S.U.S loves you :)

For more info ... https://about.gitlab.com/blog/2019/07/31/docker-in-docker-with-docker-19-dot-03

danday74
  • 52,471
  • 49
  • 232
  • 283
  • Hello! One question, I have checked my gitlab/certs folder and there is nothing there. Is that expected? I am using Gitlab.com directly, no self-host whatsoever, so what certificate should I use? Thank you in advance and regards. – Javier Guzmán Mar 25 '21 at 15:37
  • If I remember correctly ... the gitlab runner runs on your machine and creates the certs folder on your machine. If this is not how you have set it up then Im not sure how useful this answer will be for you. – danday74 Mar 25 '21 at 15:56
  • Thanks! That is what I understood but it is not happening. I already tried to ask on the Gitlab Forum and even raised an issue in the tracket but zero responses yet...Any way, I will continue digging in – Javier Guzmán Mar 25 '21 at 17:07
4

Here is a full example that works perfectly for me:

Image-upload:
  image: docker:dind
  stage: Upload Docker Image
  variables:
    IMAGE_NAME: my.registry.site.com/${CI_PROJECT_PATH_SLUG}:$CI_COMMIT_SHA
  environment: dev
  only:
    - dev
  script:
    - echo Image name ${IMAGE_NAME}
    - docker build -t $IMAGE_NAME -f ./prod.dockerfile .
    - docker login my.registry.site.com --username $DOCKER_USER --password $DOCKER_PASS
    - docker push $IMAGE_NAME
  services:
    - docker:dind

PS. you should use docker:dind instead of gitlab/dind to get the most up to date image.

Kostanos
  • 9,615
  • 4
  • 51
  • 65
2

Problem here is that node docker image does not embed docker binaries.

Two possibilities :

  • split stages to two jobs. One using node images for quality and test, one using docker image for building and deploying. See jobs documentation.

  • build a custom docker image that embed both node and docker and use this image to build your repo.

Note that in both case you will have to enable docker inside your agent. See documentation.

Olivier Cazade
  • 777
  • 5
  • 10