Gitlab Runner dind, dotnet 6, error: /bin/sh: eval: line 141: dotnet: not found

I need to start by saying that I am a beginner with CI/CD and with Gitlab. That being said, I have managed to cobble together (using examples from the web) this “almost” working pipeline configuration:


image: mcr.microsoft.com/dotnet/sdk:6.0

variables:
  OBJECTS_DIRECTORY: 'obj'
  NUGET_PACKAGES_DIRECTORY: '.nuget'
  SOURCE_CODE_PATH: '*/*/'
  TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
  TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA

stages:
  - build
  - test
  - docker
  - deploy

cache:
  key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG"
  paths:
    - '$SOURCE_CODE_PATH$OBJECTS_DIRECTORY/project.assets.json'
    - '$SOURCE_CODE_PATH$OBJECTS_DIRECTORY/*.csproj.nuget.*'
    - '$NUGET_PACKAGES_DIRECTORY'
  policy: pull-push

before_script:
  - 'dotnet restore --packages $NUGET_PACKAGES_DIRECTORY'

build:
  stage: build
  script:
  - 'dotnet build --no-restore'

tests:
  stage: test
  script:
    - 'dotnet test --no-restore'

docker:
  stage: docker
  image: docker:latest
  services:
    - docker:18.09.7-dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  only:
    - main
before_script:
    - apt-get update
    - apt-get install docker -y
  script:
    - docker login -u "gitlab-ci-token" -p "$CI_JOB_TOKEN" $CI_REGISTRY
    - docker build --tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" --tag "$CI_REGISTRY_IMAGE:latest" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    - docker push "$CI_REGISTRY_IMAGE:latest"

deploy:
  stage: deploy
  image: alpine:latest
  services:
  - docker:dind

  script:
  - chmod og= $ID_RSA
  - apk update && apk add openssh-client
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker pull $TAG_COMMIT"
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker container rm -f atlas-portal-2.0 || true"
  - ssh -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker run -d -p 80:80 --name atlas-portal-2.0 $TAG_COMMIT"
  environment: 
    name: production
  only:
  - main

When I run this pipeline, it successfully gets to the “docker” stage where I hope to build and store the docker image in the gitlab container registry. Instead I get the following error:
/bin/sh: eval: line 141: dotnet: not found

I know that somehow I need to “install” dotnet in the docker image that is going to be building the docker image to store in gitlab, but I am not sure how to go about doing that. One thing I also attempted was using the “mcr.microsoft.com/dotnet/sdk:6.0” docker image for the “docker” section, which then complains about “docker: not found”…

Seems like the microsoft image gets me close, but not sure how to add the “docker-cli” from the pipeline…

And once I get past this “docker” stage I feel confident that the “deploy” stage is going to fail too. I am a newbie :slight_smile:

EDIT:
I got it to install “docker” but it throws the error: " /bin/bash: line 149: docker: command not found"
I added this to my pipeline config before the “script:” section in the “docker” section:
before_script: - apt-get update - apt-get install docker -y

Anyone have any ideas on how to make this work?

1 Like

Someone on Stackoverflow was able to help me get past the Docker stage issue.
https://stackoverflow.com/questions/75545950/ci-cd-pipeline-config-for-gitlab-to-publish-asp-net-core-6-web-app-in-docker-con/75546853#75546853