--build-arg is not working for my docker build - docker

I am trying to add a custom NuGet repo to my docker image but I can't set credentials with --build-arg here my dockerfile
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /app
COPY . .
RUN dotnet nuget add source --username $USERNAME --store-password-in-clear-text --password $GITHUB_TOKEN --name notification "https://nuget.github.xxx.com/notification/index.json"
RUN dotnet restore
RUN dotnet publish -c Release -o /app/publish
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime
WORKDIR /app
COPY --from=build /app/publish /app
ENTRYPOINT ["dotnet", "notification-api.dll"]
EXPOSE 80
I am trying to start build with this command.
docker build --build-arg GITHUB_TOKEN=my_token --build-arg USERNAME=my_user_name -t notification-api .
I also tried with curly braces like this ${GITHUB_TOKEN} but it keeps failing.
If I hardcode my username and token in dockerfile it works.

The Dockerfile needs to explicitly specify with the ARG instruction:
ARG USERNAME
ARG GITHUB_TOKEN

Related

Dockerfile - repository does not exist or may require 'docker login

I'm following this tutorial and this is my Dockerfile
FROM public.ecr.aws/lambda/dotnet:core3.1 AS base
FROM mcr.microsoft.com/dotnet/sdk:3.1 as build
WORKDIR /src
COPY ["s3Listener.csproj", "base/"]
RUN dotnet restore "base/s3Listener.csproj"
WORKDIR "/src"
COPY . .
RUN dotnet build "s3Listener.csproj" --configuration Release --output /app/build
FROM build AS publish
RUN dotnet publish "s3Listener.csproj" \
--configuration Release \
--runtime linux-x64 \
--self-contained false \
--output /app/publish \
-p:PublishReadyToRun=true
FROM base AS final
WORKDIR /var/task
COPY --from=publish /app/publish .
CMD ["s3Listener::s3Listener.Function::FunctionHandler"]
when I try to build an image using
docker build -t s3listener .
Step 11/14 : FROM base AS final pull access denied for base,
repository does not exist or may require 'docker login': denied:
requested access to the resource is denied
What I'm doing wrong here?
The Issue here :
FROM public.ecr.aws/lambda/dotnet:core3.1 AS base
may you use the following with considering the dotnet version?
public.ecr.aws/lambda/dotnet:6
as this error has a lot of meaning
it really requires a login before the command docker build in case you confirm that this is an image only contained on your repos
Misspelling in your image name after from or the image name you make it from

Using Entrypoint with arguments to decide which .dll to run

I am experimenting with Docker and using arguments to decide what code in the image should start.
I have these two entrypoints which both work if run by themselves.
ENTRYPOINT ["dotnet", "MultiStart.API.dll"]
ENTRYPOINT ["dotnet", "MultiStart.Worker1.dll"]
I would like to decide which one is run by using arguments when i call docker run
I've tried several things along the lines of
CMD ["dotnet", $start]
ENTRYPOINT ./dotnet $start
ENTRYPOINT ["dotnet", $start]
ENTRYPOINT ["dotnet" $start]
and then calling it using something like this.
docker run -d -p 8080:80 -e start=MultiStart.API.dll --name multi multistart
But no matter what I do I end up with this error (or some variant there of):
/bin/sh: 1: ./dotnet: not found
Any suggestions as to how this can be achieved?
Ill include the entire Dockerfile that will start one of the two processes for clarity.
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 ["src", "src"]
COPY ["*.sln", ""]
RUN dotnet restore "src/MultiStart.API/MultiStart.API.csproj"
RUN dotnet restore "src/MultiStart.Worker1/MultiStart.Worker1.csproj"
COPY . .
WORKDIR "/src/src"
RUN dotnet build "MultiStart.API/MultiStart.API.csproj" -c Release -o /app/build
RUN dotnet build "MultiStart.Worker1/MultiStart.Worker1.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MultiStart.API/MultiStart.API.csproj" -c Release -o /app/publish
RUN dotnet publish "MultiStart.Worker1/MultiStart.Worker1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# both of these work by themselves.
#ENTRYPOINT ["dotnet", "MultiStart.API.dll"]
ENTRYPOINT ["dotnet", "MultiStart.Worker1.dll"]
docker run cannot pass variable like that, after all, docker file is not bash script.
The simplist way is
dockerfile
ENTRYPOINT ["command"]
docker run
docker run imagename argument list
If you want to declare variables in docker file, you can use ARG or ENV.

Docker image refuse to run dotnet test command

I'm trying to embed tests building in docker build process and abort it in case of either of tests fail. My docker file looks like below:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
COPY ["WebApplication1.Tests/WebApplication1.Tests.csproj", "WebApplication1.Tests/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build
FROM build AS test
WORKDIR /src/WebApplication1.Tests
RUN dotnet test --logger:trx
FROM build AS publish
WORKDIR /src/WebApplication1
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]
Does anybody have an idea how to deal with it?
Thanks you
You have a multistage docker file, https://docs.docker.com/develop/develop-images/multistage-build/
For your case this will work
docker build --target test -t test-tag .
docker build -t webapp-image .
docker run -ti test-tag # will run tests and give you results
If you want to see output of other stages you need to do the same
Docker build only generates the docker image from your docker file. It does not run it.
You need to use "docker run" after building your image to actually start the image as a container (which will then perform your entry point command)
https://docs.docker.com/engine/reference/run/

How can we create Dockerfile for Angular with dotnet core api project?

I have created the Dockerfile using visual studio for my dotnet api project but how can we integrate the client app in the docker file. I have created the docker file which looks like:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Locomote-e-Client.Service/Locomote-e-Client.Service.csproj", "Locomote-e-Client.Service/"]
COPY ["Locomote-e-Client.Core/Locomote-e-Client.Core.csproj", "Locomote-e-Client.Core/"]
COPY ["Locomote-e-Client.Common/Locomote-e-Client.Common.csproj", "Locomote-e-Client.Common/"]
COPY ["Locomote-e-Client.Data/Locomote-e-Client.Data.csproj", "Locomote-e-Client.Data/"]
RUN dotnet restore "Locomote-e-Client.Service/Locomote-e-Client.Service.csproj"
COPY . .
WORKDIR "/src/Locomote-e-Client.Service"
RUN dotnet build "Locomote-e-Client.Service.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Locomote-e-Client.Service.csproj" -c Release -o /app/publish
FROM node:10.21 as nodebuilder
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH
COPY Locomote-e-ClientApp/package.json /usr/src/app/package.json
RUN npm install
COPY Locomote-e-ClientApp/. /usr/src/app
RUN npm run build
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN mkdir -p /app/ClientApp/dist
COPY --from=nodebuilder /usr/src/app/dist/. /app/ClientApp/dist/
ENTRYPOINT ["dotnet", "Locomote-e-Client.Service.dll"]
Whenever I have created the image by using this docker file then it will create the image successfully. But when I tried to run that image using container then it is not running in container.
Command to build the image
docker build -t ashishrajput194/locomate:v1 .
Command to run as container
docker run -p 80:80 -d ashishrajput194/locomate:v1
It is not showing any error but the container exits automatically. I am not able to run the application.

How to build dockerfile created by visual studio 2019 from command line?

I have dockerfile created by visual studio 2019.
How can i run it manually?
bellow is the dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["Client/Client.csproj", "Client/"]
RUN dotnet restore "Client/Client.csproj"
COPY . .
WORKDIR "/src/Client"
RUN dotnet build "Client.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Client.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Client.dll"]
Navigate exactly to the project folder (not solution) where the dockerfile is located.
Run the command below in terminal:
docker build -f Dockerfile ..
docker-build
Open a terminal in the folder where dockerfile is located and execute:
docker build -t image_name .
On successful build, execute:
docker run -d image_name
I advise you to read the official documentation for docker build and docker run commands. You can find additional flags which may be useful for your case.

Resources