Dotnet not found error when generating Dockerfile - docker

I have a Gitlab-ci pipeline that should automatically generate a Dockerfile for a given .Net Core project if it does not already exist and after that execute a docker build.
I am getting an error when attempting to generate the Dockerfile :
section_end:1657538312:prepare_script
[0Ksection_start:1657538312:get_sources
[0K[0K[36;1mGetting source from Git repository[0;m[0;m
[32;1mFetching changes with git depth set to 20...[0;m
Initialized empty Git repository in /builds/demo/devops/labs/.git/
[32;1mCreated fresh repository.[0;m
[32;1mChecking out c2e2d044 as ingress...[0;m
[32;1mSkipping Git submodules setup[0;m
section_end:1657538314:get_sources
[0Ksection_start:1657538314:download_artifacts
[0K[0K[36;1mDownloading artifacts[0;m[0;m
[32;1mDownloading artifacts for publish_job (36805)...[0;m
Downloading artifacts from coordinator... ok [0;m id[0;m=36805 responseStatus[0;m=200 OK token[0;m=-1ETTroN
section_end:1657538315:download_artifacts
[0Ksection_start:1657538315:step_script
[0K[0K[36;1mExecuting "step_script" stage of the job script[0;m[0;m
[32;1m$ dotnet restore --packages $NUGET_PACKAGES_DIRECTORY[0;m
/scripts-1175-36806/step_script: eval: line 118: dotnet: not found
section_end:1657538316:step_script
[0Ksection_start:1657538316:cleanup_file_variables
[0K[0K[36;1mCleaning up project directory and file based variables[0;m[0;m
section_end:1657538316:cleanup_file_variables
[0K[31;1mERROR: Job failed: command terminated with exit code 127
[0;m
However the dotnet command executes well in the publish stage. I am not sure why dotnet is not being recognized in the docker context.
The pipeline:
before_script:
- 'dotnet restore --packages $NUGET_PACKAGES_DIRECTORY'
build_job:
tags:
- labs
only:
- develop
- ingress
stage: build
script:
- dotnet build --configuration Release --no-restore
publish_job:
tags:
- labs
only:
- develop
- ingress
stage: publish
artifacts:
name: "$CI_COMMIT_SHA"
paths:
- ./$PUBLISH_DIR
script:
- dotnet publish ./src --configuration Release --output $(pwd)/$PUBLISH_DIR
docker_build_dev:
tags:
- labs
image:
name: gcr.io/kaniko-project/executor:v1.6.0-debug
entrypoint: [""]
only:
- develop
- ingress
stage: docker
script: |
echo "checking dockerfile existence"
if ! [ -e Dockerfile ]; then
echo "dockerfile doesn't exist. Trying to create a new dockerfile from csproj."
docker_entrypoint=$(grep -m 1 AssemblyName ./src/*.csproj | sed -r 's/\s*<[^>]*>//g' | sed -r 's/\r$//g').dll
cat > Dockerfile << EOF
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN dotnet --version
WORKDIR /app
COPY ./publish .
ENTRYPOINT dotnet $docker_entrypoint
EOF
echo "dockerfile created"
else
echo "dockerfile exists"
fi
What am I missing ?

eval: line 118: dotnet: not found
It looks like you haven't defined an image: for this job and whatever default image you are using does not have dotnet installed (or if you're using a shell runner, your runner system does not have dotnet installed/on PATH).
To address this, you should either (1) specify an image: that has dotnet available (or install it on your runner if using a shell runner and ensure it is on PATH) or (2) install dotnet as part of your job.
For example:
myjob:
image: mcr.microsoft.com/dotnet/sdk
# ...
Alternatively, you can use a dotnet install script to install dotnet in your job.

Related

gitlab runner shell executer path finding problems

I'm kinda stuck in how to deploy something.
As a dotnet developer I created a webapp (just the simple weatherforecastteamplte) and tried to deploy this.
My ci-cd has 3 stages: build, test and deploy.
My first approach was to use aa ssh key to ssh into my buildserver and transfer the files, because I had gitlab runner running on my gitlab server rather than my buildserver.
For security reasons and other reasons , I have installed gitlab-runner on my buildserver.
My buildserver includes all the tools I nedd: docker, docker-compose, dotnet-npm, ...
So i registered a new runner with shell.
It starts to build the image but when it comes in my dokcerfile it gets stuck because it can't find the cproj file. Currently I have been trying to figure out where the executer is looking for the project file but currently I can't figure it out.
Hope someone can help me.
my dockerfile (generated by visual studio':
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Test/Test.csproj", "Test/"]
RUN dotnet restore "Test/Test.csproj"
COPY . .
WORKDIR "/src/Test"
RUN dotnet build "Test.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Test.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Test.dll"]
my gitlab ci cd file:
image: mcr.microsoft.com/dotnet/sdk:latest
stages:
- build
- test
- deploy
before_script:
- dotnet restore Test.sln
build:
stage: build
tags:
- dev
script:
- 'dotnet build --no-restore Test.sln'
tests:
stage: test
tags:
- dev
script:
- 'dotnet test --no-restore Test.sln'
deploy:
stage: deploy
environment: production
tags:
- dev
before_script:
- cd Test
script:
- 'docker build - < Dockerfile'

Gitlab Pipeline to build docker Image of dotnetcore application and deploy

I have Installed Gitlab in one of the Ubuntu machine. And I have dotnetcore project in the name of ABC in the Gitlab.
But, In that ABC repo have multiple small doetnetcore application with different different directory like abc1 abc2 abc3 abc4.
I want to write a single pipeline under ABC to create the docker Image whenever developer push the code in the respective directory. but that need to be create docker Image for that only directory.
eg: Developer push the code under abc3 directory, that time pipeline run and create the docker Image for only abc3 directory.
Please help me out with it.
Thanks in Advance...!!!
Below is my pipeline what I have written also Docker file:
stages:
- docker
- build
services:
- docker:dind
before_script:
- "echo $gitlab"
docker-job:
stage: docker
image: docker:dind
script:
- docker login -u username -p password $CI_REGISTRY
- docker build -t dotnetcore .
#- docker push $IMAGE_PUSH:latest
build:
stage: build
tags:
- shell
image: mcr.microsoft.com/dotnet/sdk
script:
- dotnet restore
- dotnet build
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["dotnetcore.csproj","./"]
RUN dotnet restore "dotnetcore.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "dotnetcore.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "dotnetcore.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "dotnetcore.dll"]
In this pipeline and dockerfile I am only able to build "dotnetcore" project. But I have dotnetcore1 doctnetcore2 dotnetcore3 projects under the same Repo.
gitlab ci has a only:changes feature. You can define multiple jobs, each for one docker.
job-a:
script: docker build a
only:
changes:
- a/**/*
job-b:
script: docker build b
only:
changes:
- b/**/*
Assuming you have a small number of directories. You can create a job for each directory. Use rules with changes to check if changes were made in the particular directory.

Issue in YAML file in Azure Build pipeline when building docker file and pushing to Docker Hub

I can't find the issue in YAML azure-pipeline file when I it create the docker build file, then I want to pushing it to my private repository 'username/docker-images' on Docker Hub.
Maybe someone can tell me what I'm doing wrong?
I have the following solution folder structure:
/src/Project1/Project1.csproj
/src/Project2/Project2.csproj
/Dockerfile.PRJ1
/Dockerfile.PRJ2
/Solution1.sln
When I build it with the following command on local Windows machine it succeeds:
C:\> docker build -t Project1 -f src/Project1/Dockerfile .
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
COPY ./*.sln ./
COPY ./src/Project1/Project1.csproj ./src/Project1/
WORKDIR /src
RUN dotnet restore ./Project1/Project1.csproj
COPY . /
RUN dotnet build ./Project1/Project1.csproj -c release --no-restore
#FROM build AS test
#RUN dotnet test
FROM build AS publish
RUN dotnet publish ./Project1/Project1.csproj -c release --no-build -o /app/publish
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS runtime
WORKDIR /app
COPY --from=publish /app/publish ./
RUN apk add --no-cache icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENTRYPOINT ["dotnet", "Project1.dll"]]
EXPOSE 443
YAML Build and release pipeline:
trigger:
- master
resources:
- repo: self
variables:
tag: '$(Build.BuildId)'
buildConfiguration: 'Release'
stages:
- stage: VSBuild
displayName: Build and Release to private DockerHub
jobs:
- job: VSBuild
displayName: Build software
pool:
vmImage: ubuntu-latest
steps:
- task: DotNetCoreCLI#2
inputs:
command: 'restore'
projects: '**/*.csproj'
displayName: 'Restore Nuget Packages'
- task: DotNetCoreCLI#2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--no-restore'
displayName: 'Build projects'
- task: DotNetCoreCLI#2
inputs:
command: 'publish'
publishWebProjects: false
projects: '**/src/Project1/Project1.csproj'
arguments: '--configuration $(buildConfiguration) --no-restore'
modifyOutputPath: false
displayName: 'Publish'
- task: Docker#2
displayName: build
inputs:
containerRegistry: {GUID}
repository: docker-images
command: build
Dockerfile: '**/Dockerfile.PRJ1' #LINE UPDATED
tags: idp-$(Build.BuildId)
- task: Docker#2
displayName: push
inputs:
containerRegistry: {GUID}
repository: docker-images
command: push
tags: idp-$(Build.BuildId)
I get the following error in the Azure Pipeline:
...
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-alpine
---> 9e12b747545b
Step 2/29 : COPY ./*.sln ./
COPY failed: no source files were specified
##[error]COPY failed: no source files were specified
##[error]The process '/usr/bin/docker' failed with exit code 1
Solution was: Move the /Project1/Dockerfile to .SLN level it will go further.
But now it comes with the following error, what should be standing at this step?
Step 11/29 : RUN dotnet test
---> Running in 86a9fa6e54d5
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
The command '/bin/sh -c dotnet test' returned a non-zero code: 1
##[error]The command '/bin/sh -c dotnet test' returned a non-zero code: 1
My solution was to removed the 2 lines from the Dockerfile:
FROM build AS test
RUN dotnet test
According your yaml settings, we find your docker file is under the folder 'src/Project1'.
And based on the operating rules of docker file, the copied file must be in the same directory as the docker file.
So, we recommend you can move your docker file to the folder same with /Solution1.sln, and modify your yaml file.

Deploy images on Rancher from Docker Hub

I am trying to deploy my docker images from Docker Hub to Rancher. When I do that, I am getting only updating status and then after some time, I am getting deployment timeout. I am new to Rancher and trying to go through documents but not able to understand it properly. Can anyone assist me to do this?
I already tried to pull the image and after some time it shows "deployment does not have minimum availability".
Below is my .gitlab-ci.yml file configuration:
stages:
- build
- publish
.build: &build_template
stage: build
image: microsoft/dotnet:2.2-sdk-alpine
cache:
key: "$CI_PROJECT_NAMESPACE-$CI_PROJECT_NAME"
paths:
- .nuget/
script:
- dotnet restore -s https://api.nuget.org/v3/index.json --packages ./.nuget/
- dotnet publish --no-restore -c Release -o ./docker/publish/
develop_build:
<<: *build_template
only:
- develop
artifacts:
expire_in: 1 day
paths:
- docker/
.docker: &docker_template
stage: publish
image: docker:stable-dind
script:
- mkdir docker/app/
- mv docker/publish/$ASSEMBLY_NAME.* docker/app/
- mv docker/publish/*.config docker/app/
- mv docker/publish/*.json docker/app/
- cp Dockerfile docker/
- docker build -t $TAG ./docker/
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker push $TAG
develop_docker:
<<: *docker_template
only:
- develop
dependencies:
- develop_build
variables:
ASSEMBLY_NAME: app
TAG: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_PIPELINE_ID
Below is my DockerFile:
# Dockerfile
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers set
COPY ./DataStore/DataStore/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY *[^.csproj] ./
RUN dotnet publish -c Release -o out
# Build runtime images
FROM microsoft/dotnet:aspnetcore-runtime
ENTRYPOINT ["dotnet", "DataStore.dll"]
I want to run my C# web APIs after deploying it to Rancher.

Gitlab CI - Docker in Docker - How to maintain a pipeline?

I am trying to use gitlab CI for docker in docker builds and i have it up and running.
My issue is that the nice pipeline functionality of gitlab is somewhat lost since the whole build,test,etc... stages are defined in the DockerFile and not yaml.
I only have a single stage in the yaml right now.
Is there a way to use docker in docker and still leverage gitlab pipelines?
Relevant files.
Dockerfile
# Stage 1 - require and update image
FROM microsoft/aspnetcore-build:2.0.0 AS build
# Update the imgage OS
RUN apt-get update \
&& apt-get install -y
# set the container working directory
WORKDIR /code
# Stage 2 - copy data
# caches restore result by copying csproj file separately
COPY src/Nuget.Config ./src/
COPY src/Inflow.NewsMl.FileLoader/Inflow.NewsMl.FileLoader.csproj ./src/Inflow.NewsMl.FileLoader/
RUN dotnet restore src/Inflow.NewsMl.FileLoader/Inflow.NewsMl.FileLoader.csproj --configfile /code/src/Nuget.Config
COPY src/Inflow.NewsMl.FileLoader.Test/Inflow.NewsMl.FileLoader.Test.csproj ./src/Inflow.NewsMl.FileLoader.Test/
RUN dotnet restore src/Inflow.NewsMl.FileLoader.Test/Inflow.NewsMl.FileLoader.Test.csproj --configfile /code/src/Nuget.Config
# Copy the rest
COPY . .
# Stage 3 - Build and test
RUN dotnet test src/Inflow.NewsMl.FileLoader.Test/Inflow.NewsMl.FileLoader.Test.csproj
# Stage 4 - Deploym to image
RUN dotnet publish src/Inflow.NewsMl.FileLoader --output /code/output --configuration Release
FROM microsoft/aspnetcore:2.0.0
COPY --from=build /code/output /app
WORKDIR /app
ENTRYPOINT [ "dotnet", "Inflow.NewsMl.FileLoader.dll" ]
and the ci runner yaml
image: docker:latest
.before_script:
- docker info
variables:
DOCKER_DRIVER: overlay
CONTAINER_TEST_IMAGE: infomedia/newsml.fileloader:CI_COMMIT_SHA
#$CI_COMMIT_TAG
services:
- docker:dind
stages:
- build
build:
stage: build
#only:
# - tags
#except:
# - master
script:
- docker build -t $CONTAINER_TEST_IMAGE .

Resources