Home > Mobile >  ci/cd "docker" command not found in image built with docker installed
ci/cd "docker" command not found in image built with docker installed

Time:05-22

I have this .gitlab-ci.yml file wanting to automate the docker image building, basically I'm using the one from the template:

docker-build:
  image: my_image_build_with_docker_inside_inprivate_repo
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  # Default branch leaves tag empty (= latest tag)
  # All other branches are tagged with the escaped branch name (commit ref slug)
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag=""
        echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
      else
        tag=":$CI_COMMIT_REF_SLUG"
        echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
      fi
    - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
    - docker push "$CI_REGISTRY_IMAGE${tag}"
  # Run this job in a branch where a Dockerfile exists
  rules:
    - if: $CI_COMMIT_BRANCH
      exists:
        - Dockerfile

#$CI_REGISTRY_IMAGE = my_image_build_with_docker_inside_inprivate_repo

When I run it get this error:

$ docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGIST
/bin/bash: line 132: docker: command not found
Cleaning up project directory and file based variables
ERROR: Job failed: exit code 1

This is the dockerfile used to build the custom image:

FROM debian:stable

RUN apt-get update -y \
 && apt-get install -y python3 \
 && apt-get install -y python3-venv \
 && apt-get install -y python3-pip \
 && apt-get install -y git \
 && apt-get install -y docker-compose 

# Custom cache invalidation
ARG CACHEBUST=1

# Clone netdd repository  
RUN git clone https:.../netdd_demo.git

# Replace 'activate' cmd by setting the environment variables
RUN mkdir /netdd_demo/venv
ENV VIRTUAL_ENV=/netdd_demo/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# Persistent volume mount point
VOLUME /netdd_demo/files

# Install dependencies:
RUN python3 -m pip install --upgrade pip \ 
    && pip3 install -r /netdd_demo/requirements.txt

# Run the application:
WORKDIR /netdd_demo
ENTRYPOINT ["python3", "netdd_main.py"]

What do I need to modify in the files above to make this work?

With : RUN apt-get install -y docker-compose , I get everything needed for docker to run in the debian image. But I still get the error of docker command not found as above. What are the other steps needed to run docker daemon from your custom image?

CodePudding user response:

In the stage docker-build, the job is based on an image that does not have the docker installed.

As a result when you try to communicate with the docker:dind service, the job can't not find the docker command

Change

docker-build:
  image: cr.s.com/ccp/vm:v7
  stage: build

To

docker-build:
  image: docker:latest
  stage: build

CodePudding user response:

You can try installing docker in the Dockerfile to build your custom image.

You can follow the steps defined in the official docs https://docs.docker.com/engine/install/debian/ which would look like something like this in your Dockerfile

RUN apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
RUN echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
RUN apt-get update -y
RUN apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
  • Related