Gitlab CI - docker: command not found

36,748

Solution 1

you need to choose an image including docker binaries

image: gitlab/dind

services:
  - docker:dind

Solution 2

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

Solution 3

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.

Share:
36,748

Related videos on Youtube

Kay
Author by

Kay

Updated on April 05, 2021

Comments

  • Kay
    Kay about 3 years

    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"
    
    • Thymine
      Thymine almost 6 years
      This seems to advise against using DinD for general CI purposes jpetazzo.github.io/2015/09/03/… Might try to follow that process and avoid this being an issue at all
    • JulienD
      JulienD almost 5 years
      @Thymine the proposed alternative is to mount the docker socket when you start the "external" container, which you cannot do in gitlab-ci.
  • Javier Guzmán
    Javier Guzmán about 3 years
    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.
  • danday74
    danday74 about 3 years
    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.
  • Javier Guzmán
    Javier Guzmán about 3 years
    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
  • Tobias Mühl
    Tobias Mühl over 2 years
    Gitlab/dind is deprecated (last update 5 years ago). Official docker dind image should be used instead (called just "docker")