I try to update a JSON file with Github build information and add it to my docker container. A sample buildinfo.json is part of the C# project. When the build is done, the JSON file in the container remains the sample one and I cannot see what goes wrong here.
Thanks for your help!
My action looks like this:
env:
# Project to publish
BASE_FOLDER: src/WebService
PROJECT_NAME: removed
PROJECT_DOCKERFILE: Dockerfile
PROJECT_CONTAINERNAME: api/removed
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
# Version settings
NETCORE_VERSION: '6.0.x'
# Docker settings
DOCKER_REGISTRY: removed
DOCKER_NAMESPACE: removed
DOCKER_BUILDKIT: 1
jobs:
build:
runs-on: 'ubuntu-latest'
steps:
- name: Checkout
uses: actions/checkout#v2
with:
token: ${{ secrets.REPOSITORY_PAT }}
lfs: 'true'
submodules: 'recursive'
fetch-depth: 0
- name: Setup .NET Core ${{ env.NETCORE_VERSION }}
uses: actions/setup-dotnet#v1
with:
dotnet-version: ${{ env.NETCORE_VERSION }}
include-prerelease: false
env:
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Emit build details as JSON
run: |
echo -e -n "{\"buildNumber\":\"${{ github.run_id }}.${{ github.run_number }}.${{ github.run_attempt }}\",\"buildId\":\"${{ github.run_id }}\",\"branchName\":\"${{ github.ref_name }}\",\"commitHash\":\"${{ github.sha }}\"}" > "${{ env.BASE_FOLDER }}/${{ env.PROJECT_NAME }}/buildinfo.json"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
- name: Log in to registry
uses: docker/login-action#v1
with:
registry: removed
username: ${{ secrets.AZURE_ACR_USERNAME }}
password: ${{ secrets.AZURE_ACR_PASSWORD }}
- name: Build and push container image to registry
uses: docker/build-push-action#v2
with:
push: true
tags: ${{ env.DOCKER_REGISTRY }}.azurecr.io/${{ env.DOCKER_NAMESPACE }}/${{ env.PROJECT_CONTAINERNAME }}:${{ github.sha }}
file: ${{ env.BASE_FOLDER }}/${{ env.PROJECT_NAME }}/${{ env.PROJECT_DOCKERFILE }}
With this dockerfile:
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 ["nuget.config", "."]
COPY ["src/WebService/PROJECTNAME/PROJECTNAME.csproj", "src/WebService/PROJECTNAME/"]
RUN dotnet restore "src/WebService/PROJECTNAME/PROJECTNAME.csproj"
COPY . .
WORKDIR "/src/src/WebService/PROJECTNAME"
RUN dotnet build "PROJECTNAME.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "PROJECTNAME.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PROJECTNAME.dll"]
Related
I run commands to build and publish the docker image to registry but it fails with this error:
MSBUILD : error MSB1009: Project file does not exist.
Switch: ./Project.Api/Project.Api.csproj
Project structure
src
|--Project.Api
| |--Project.Api.csproj
|-- Dockerfile
tests
azure-pipelines.yml
Docker task in pipeline
stages:
...
- stage: build
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: build_container
displayName: 'Build docker container image'
steps:
- bash: |
docker login -u $(REGISTRY_USER) -p $REGISTRY_PASSWORD $(REGISTRY_URL)
docker build -t $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk . -f $(build.sourcesDirectory)/src/Dockerfile
docker push $(REGISTRY_URL)/$(IMAGE_REPOSITORY):kkk
displayName: 'Build and push docker image'
env:
REGISTRY_PASSWORD: $(REGISTRY_PASSWORD)
my Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . /src
RUN dotnet restore ./Project.Api/Project.Api.csproj
RUN dotnet publish ./Project.Api/Project.Api.csproj --no-restore -c Release -o /app
FROM build AS publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Project.Api.dll"]
Does anyone know that is the cause of this error and how to fix it? Thank you.
Follow standard troubleshooting steps: Check what directory you're running that bash command out of. Put a RUN ls or similar command in your Dockerfile so you can check the directory structure within your container during build and confirm it's what you expect.
Your Docker context is likely set a directory level higher than you think it is.
You can set your working directory appropriately from there.
- bash: |
docker ..
workingDirectory: $(build.sourcesDirectory)/src
I'd guess that your code is ending up in the directory /src/src/ in your Dockerfile.
I am using github to host private nuget packages, and the project I am trying to build an image for uses one of those packages. When image is being built via a github action I successfully add the nuget source, but restore fails to find it. However if I run it locally everything works just fine.
Action yaml:
name: Docker
on:
release:
branches: [ master, qa ]
tags: [ 'v*.*.*' ]
types: [ published ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ph/logging-api #<------------------------Container name
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout#v2
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action#79abd3f86f79a9d68a23c75a09a9a85889262adf
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action#28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action#98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action#ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
file: api.Dockerfile #<----------------------------------------- Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
REGISTRY_USER=${{ github.actor }}
REGISTRY_TOKEN=${{ secrets.GITHUB_TOKEN }}
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
ARG REGISTRY_USER
ARG REGISTRY_TOKEN
RUN dotnet nuget add source --username $REGISTRY_USER --password $REGISTRY_TOKEN --store-password-in-clear-text --name github "https://nuget.pkg.github.com/ph/index.json"
COPY ["ph.LogApi/ph.LogApi.csproj", "ph.LogApi/"]
RUN dotnet restore "ph.LogApi/ph.LogApi.csproj"
COPY . .
WORKDIR "/src/ph.LogApi"
RUN dotnet build "ph.LogApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ph.LogApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ph.LogApi.dll"]
If I execute the docker file directly and pass in the build-args it works great. However in the github action it successfully adds the nuget source and then fails to find the package in either of them.
#10 [build 3/8] RUN dotnet nuget add source --username h-w -*** --store-password-in-clear-text --name github "https://nuget.pkg.github.com/ph/index.json"
#10 0.305 Package source with Name: github added successfully.
#10 DONE 0.3s
#11 [build 4/8] COPY [ph.LogApi/ph.LogApi.csproj, ph.LogApi/]
#11 DONE 0.0s
#12 [build 5/8] RUN dotnet restore "ph.LogApi/ph.LogApi.csproj"
#12 0.792 Determining projects to restore...
#12 4.158 /src/ph.LogApi/ph.LogApi.csproj : error NU1101: Unable to find package ph.Configuration. No packages exist with this id in source(s): github, nuget.org
#12 4.343 Failed to restore /src/ph.LogApi/ph.LogApi.csproj (in 3.35 sec).
#12 ERROR: process "/bin/sh -c dotnet restore \"ph.LogApi/ph.LogApi.csproj\"" did not complete successfully: exit code: 1
------
> [build 5/8] RUN dotnet restore "ph.LogApi/ph.LogApi.csproj":
#12 0.792 Determining projects to restore...
#12 4.158 /src/ph.LogApi/ph.LogApi.csproj : error NU1101: Unable to find package ph.Configuration. No packages exist with this id in source(s): github, nuget.org
#12 4.343 Failed to restore /src/ph.LogApi/ph.LogApi.csproj (in 3.35 sec).
------
api.Dockerfile:18
--------------------
16 | RUN dotnet nuget add source --username $REGISTRY_USER -*** --store-password-in-clear-text --name github "https://nuget.pkg.github.com/PhoeniqsTech/index.json"
17 | COPY ["ph.LogApi/ph.LogApi.csproj", "ph.LogApi/"]
18 | >>> RUN dotnet restore "ph.LogApi/ph.LogApi.csproj"
19 | COPY . .
20 | WORKDIR "/src/ph.LogApi"
--------------------
error: failed to solve: process "/bin/sh -c dotnet restore \"ph.LogApi/ph.LogApi.csproj\"" did not complete successfully: exit code: 1
Error: buildx call failed with: error: failed to solve: process "/bin/sh -c dotnet restore \"ph.LogApi/ph.LogApi.csproj\"" did not complete successfully: exit code: 1
Any input as to what the problem could be is greatly appreciated!
I have private Nuget feed from Azure Artifact that I want to use inside docker, but I am getting the following error:
/usr/share/dotnet/sdk/5.0.402/NuGet.targets(131,5): error : Unable to load the service index for source http://test/DefaultCollection/test/_packaging/company/nuget/v3/index.json. [/src/App1/App1.csproj]
/usr/share/dotnet/sdk/5.0.402/NuGet.targets(131,5): error : NTLM authentication is not possible with default credentials on this platform. [/src/App1/App1.csproj]
The command '/bin/sh -c dotnet restore "App1/App1.csproj" --configfile nuget.config' returned a non-zero code: 1
##[error]The command '/bin/sh -c dotnet restore "App1/App1h.csproj" --configfile nuget.config' returned a non-zero code: 1
##[error]/usr/bin/docker failed with return code: 1
I tried few thing, but I am getting same issues. Do you have any suggestion how to fix this?
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
#############
## Stage 1 ##
#############
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
#Personal Access Token to Authenticate To Artifactory
ARG FEED_URL
ARG PAT
RUN apt-get update && apt-get install -y --no-install-recommends gss-ntlmssp
#ENV DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER=0
#ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED true
#ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS {\"endpointCredentials\": [{\"endpoint\":\"${FEED_URL}\", \"username\":\"App1\", \"password\":\"${PAT}\"}]}
#
#
#RUN wget -qO- https://raw.githubusercontent.com/Microsoft/artifacts-credprovider/master/helpers/installcredprovider.sh | bash
#
WORKDIR /src
## Copy the applications .csproj
COPY ["App1/App1.csproj", "App1/"]
COPY ["App2/App2.csproj", "App2/"]
COPY nuget.config ./
## Restore packages
#RUN dotnet restore "App1.csproj" -s "${FEED_URL}" -s "https://api.nuget.org/v3/index.json"
#RUN sed -i "s|</configuration>|<packageSourceCredentials><Company><add key=\"Username\" value=\"Company\" /><add key=\"ClearTextPassword\" value=\"${PAT}\" /></Company></packageSourceCredentials></configuration>|" nuget.config
#RUN dotnet restore . -s "${FEED_URL}" -s "https://api.nuget.org/v3/index.json" "App1.csproj"
RUN dotnet restore "App1.csproj" --configfile nuget.config
#RUN dotnet restore \
#-s https://api.nuget.org/v3/index.json \
#-s ${FEED_URL} \
#"App1.csproj"
## Copy everything else
COPY . .
WORKDIR "/src/App1"
## Build the app
RUN dotnet build "App1.csproj" -c Release -o /app/build
## Run dotnet test setting the output on the /coverage folder
## ...
## Create the code coverage file in sonarqube format using the cobertura file generated from the dotnet test command
## ...
## Publish the app
FROM build AS publish
RUN dotnet publish "App1.csproj" -c Release -o /app/publish
#############
## Stage 2 ##
#############
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "App1.dll"]
My task for building docker container:
- task: Docker#0
displayName: 'Build an image: App1'
inputs:
containerregistrytype: 'Azure Container Registry'
azureSubscription: 'azure connection'
azureContainerRegistry: '{"loginServer":"test.azurecr.io", "id" : "/subscriptions/be40d993-b17f-4873-a724-11111111111/resourceGroups/aks-test/providers/Microsoft.ContainerRegistry/registries/test"}'
action: 'Build an image'
dockerFile: 'App1/App1/Dockerfile'
buildArguments: |
PAT=$(PAT)
FEED_URL=$(FEED_URL)
defaultContext: false
imageName: '$(Build.Repository.Name):$(Build.BuildId)'
I am using private agents on Azure DevOps Server.
I have a .Net webapi project, and I want to dockerize it. Without docker, dotnet sdk(6.0) can restore everything correctly and there is no obstacle for me to publish the app. But when I use docker to do that, it gives me tones of errors with the error codes CS0246, CS8714, CS0103, CS0535, CS0738. You can find my project folder structure and relevant information below:
src
|-->API
| |-->API.csproj
| |-->Some other stuff
tools
|-->Docker
| |-->Dockerfile
docker-compose.yml
#./src/Docker/Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
COPY *.sln ./
COPY ./src/API src/API
WORKDIR /src/API
RUN dotnet restore
RUN dotnet publish -c release -o /src/API
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /src/API
COPY --from=build /src/API ./
EXPOSE 80/tcp
ENV ASPNETCORE_URLS http://*:80
ENTRYPOINT ["dotnet", "API.dll"]
#./docker-compose.yml
version: '3'
services:
api:
build:
context: .
dockerfile: tools/Docker/Dockerfile
ports:
- "5009:5009"
environment:
ASPNETCORE_ENVIRONMENT: "prod"
the .NET 6.0 SDK image is not official yet
you can use some RC version instead
try
ARG REPO=mcr.microsoft.com/dotnet/aspnet
FROM $REPO:6.0.0-rc.1-alpine3.14-amd64
follow here for updates:
https://hub.docker.com/_/microsoft-dotnet-sdk?tab=description
How can I restore nuget packages hosted in GitHub packages within a Dockerfile?
When I create a NuGet.config file, I need to set a token there (only a readonly token) to access GitHub Packages, even on public repositories :-( . This password will be deleted by GitHub immediately. Is there another way?
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Deploy_O_Mat.API/Deploy_O_Mat.API.csproj", "Deploy_O_Mat.API/"]
COPY ["NuGet.config", "Deploy_O_Mat.API/"]
RUN dotnet restore "Deploy_O_Mat.API/Deploy_O_Mat.API.csproj"
COPY . .
WORKDIR "/src/Deploy_O_Mat.API"
RUN dotnet build "Deploy_O_Mat.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Deploy_O_Mat.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Deploy_O_Mat.API.dll"]
Action (build - restore works without NuGet.config file):
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.100
source-url: https://nuget.pkg.github.com/Marcel-B/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
Action (Docker image build and deply without NuGet.config - doesn't work):
name: Publish Docker
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action#master
with:
name: millegalb/deploy-o-mat
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
source-url: https://nuget.pkg.github.com/Marcel-B/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}