How to pass command line argument to docker run and print it? - c#

I created a .Net core 2.1 console application with docker (linux) support. Here is the system generated Dockerfile.
FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY MyApp/MyApp.fsproj MyApp/
RUN dotnet restore MyApp/MyApp.fsproj
COPY . .
WORKDIR /src/MyApp
RUN dotnet build MyApp.fsproj -c Release -o /app
FROM build AS publish
RUN dotnet publish MyApp.fsproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]
The console program just prints out the argument. (Console.WriteLine("The args are {0}", args);)
However, docker run MyApp:dev doesn't print anything. And docker run MyApp:dev ABC got the following error.
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"ABC\": executable file not found in $PATH": unknown.
I tried docker run -it MyApp:dev dotnet /app/MyApp.dll ABC and it got the error of
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
Running docker run -it MyApp:dev dotnet shows the Usage of dotnet.
PS C:\Temp> docker run -it MyApp:dev dotnet
Usage: dotnet [options]
Usage: dotnet [path-to-application]
Options:
-h|--help Display help.
--info Display .NET Core information.
--list-sdks Display the installed SDKs.
--list-runtimes Display the installed runtimes.
I tried docker run -it MyApp:dev bash and found the dirctory /app is empty. find . -name MyApp.dll cannot find anything?

It looks like what you're doing should work. I just made a repo here:
https://github.com/rquackenbush/DotNetCoreArgs
After building:
docker build .
I was able to run:
docker run <imageid> ABC
And I got:
Hello World!
There are 1 args.
ABC

Related

Unable to load the service index for source error for a CodeArtifact feed using a Dockerfile

It's my first question here so let me know if I have to change something or add more information.
We’re using AWS CodeArtifact for storing our packages and when we try to build a Docker image from our Dockerfile it fails because it's unable to load the source during the restore process.
We have a web API in .Net we want to deploy using AWS Fargate. This project runs smoothly from Visual Studio 2022 using Docker but we can’t build the image from PowerShell after adding our packages from CodeArtifact.
Our approach to include the credentials in the build is to pass the NuGet.Config stored on the host using Buildkit.
This’s our Dockerfile:
#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
ENV ASPNETCORE_URLS=http://+:49151
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
WORKDIR /src
COPY ["Src/Presentation/Project.API/Project.API.csproj", "Src/Presentation/Project.API/"]
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
--mount=type=secret,id=nugetconfig \
dotnet restore "Src/Presentation/Project.API/Project.API.csproj" \
--configfile /run/secrets/nugetconfig
COPY . .
WORKDIR "/src/Src/Presentation/Project.API"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet build "Project.API.csproj" -c Release -o /app/build \
--no-restore
FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish "Project.API.csproj" -c Release -o /app/publish \
--no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.API.dll"]
Our script in PowerShell:
docker buildx build --secret id=nugetconfig,src=$HOME\AppData\Roaming\NuGet\NuGet.Config -f "Src\Presentation\Project.API\Dockerfile" -t my-dotnet-image .
Output:
#14 [build 6/9] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages --mount=type=secret,id=nugetconfig dotnet restore "Src/Presentation/Project.API/Project.API.csproj"
--configfile /run/secrets/nugetconfig
#14 1.175 Determining projects to restore...
#14 2.504 /src/Src/Presentation/Project.API/Project.API.csproj : error NU1301: Unable to load the service index for source
https://domain-123456789012.d.codeartifact.us-east-2.amazonaws.com/nuget/repository/v3/index.json.
What are we missing here? Once we have the Dockerfile working we want to use CDK for deploying the Docker image alongside our infrastructure.
We’re using aws codeartifact login command to authenticate with the service. We work on windows with a Linux container

Docker dotnet restore: Unable to load the service index for source https://api.nuget.org/v3/index.json

Objective -> I am currently trying to integrate Dapr with a small sample project provided by the .Net project templates. At the end of my test cases, I would like to use Docker Compose to launch multiple containers for a web API and a web app to communicate with each other using the Dapr sidecars.
Problem -> Whenever I run docker compose up on the sample application, the container fails to be created and is stopped by this error:
> [build 4/7] RUN dotnet restore "MyBackEnd/MyBackEnd.csproj":
#0 0.945 Determining projects to restore...
#0 7.179 /src/MyBackEnd/MyBackEnd.csproj : error NU1301: Unable to load the service index for source https://api.nuget.org/v3/index.json.
#0 7.202 Failed to restore /src/MyBackEnd/MyBackEnd.csproj (in 6.09 sec).
Steps to Reproduce ->
The following are the steps I took with this project:
Ran mkdir sample_project && cd sample_project
Ran dotnet new webapi -o MyBackEnd followed by cd MyBackEnd
Ran touch Dockerfile && cd .. && touch docker-compose.yml
I then put this in my Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
RUN apt-get update && apt-get install -y libcurl4
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["MyBackEnd/MyBackEnd.csproj", "MyBackEnd/"]
RUN dotnet restore "MyBackEnd/MyBackEnd.csproj"
COPY . .
WORKDIR "/src/MyBackEnd"
RUN dotnet build "MyBackEnd.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyBackEnd.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyBackEnd.dll"]
And this was what I did with my Docker Compose YAML file:
version: '3.4'
services:
mybackend:
image: ${DOCKER_REGISTRY-}mybackend
build:
context: .
dockerfile: MyBackEnd/Dockerfile
ports:
- "3000:80"
Finally, I ran docker compose up to arrive at the issue
Additional Information -> I am not running this on a Windows system. I am using Ubuntu 20, and thus, I do not have access to Visual Studio to handle the launching and configuration for me. I am aware that this is a heavily replicated question, but I have yet to see anything that specifically applies to users that do not have access to Visual Studio or on Ubuntu. Thanks!

Kafka with .net core docker file config

My app is .net core 3.1 based. Trying to connect to kafka , publish and consume topics. Locally the code is working fine no error .Running the code in wsl2 ubuntu env using vs code. Now I want to create a docker image for the same code and publish in azure. The docker image is getting created but while trying to run the image getting error
No provider for SASL mechanism GSSAPI:Recompile librdkafka with libsasal2 or openssl support. Current build options:Plain Sasl_scram ouathbearer.
Please help with the docker file as what exactly to install and how to do so
Docker file :
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
RUN apt-get update
RUN apt-get install -y sasl2-bin \
libsasl2-modules-gssapi-mit\
krb5-user \
libkrb5-dev \
libsasl2-dev\
libzstd-dev
RUN echo "Done"
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["abc.csproj", "./"]
RUN dotnet restore "./abc.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./abc.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "abc.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "abc.dll"]

why dotnet 5 docker container fail in run time?

I have a dotnet5 Console application that is inside a solution and has dependencies on other sibling projects. I create the Docker file with visual studio tools and this is the Docker file:
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj", "src/MyProject/MyProject.Launcher/"]
COPY ["src/MyProject/MyProject.DataAccess/MyProject.DataAcess.csproj", "src/MyProject/MyProject.DataAccess/"]
COPY ["src/MyProject/MyProject.Service/MyProject.Service.csproj", "src/MyProject/MyProject.Service/]
COPY ["src/Domain/MyProject.Domain.Model/MyProject.Domain.Model.csproj", "src/Domain/MyProject.Domain.Model/"]
RUN dotnet restore "src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj"
COPY . .
WORKDIR "/src/src/MyProject/MyProject.Launcher"
RUN dotnet build "MyProject.Launcher.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyProject.Launcher.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.Launcher.dll"]
building image is ok and it added normally to my local image registry but when I try to run this image and create a container I am getting this error:
It was not possible to find any compatible framework version The
framework 'Microsoft.AspNetCore.App', version '5.0.0' was not found.
No frameworks were found.
You can resolve the problem by installing the specified framework
and/or SDK.
Use this as your base image:
FROM mcr.microsoft.com/dotnet/aspnet:5.0.0-buster-slim AS base
I Think You should check docker hub and search for .NET 5 SDK then install that image on your container then its will be fixed :
I Found Dotnet 3.1 SDK but try to find dotnet 5 SDK
I think after installation this error will be fix
Sorry For my Bad English
:)

Dotnet restore Docker fail

There is problem with Docker and command dotnet restore.
I have the docker file:
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 ["AuthAPI/AuthAPI.csproj", "AuthAPI/"]
COPY ["CommonCoreLibrary/CommonCoreLibrary.csproj", "CommonCoreLibrary/"]
RUN dotnet restore "AuthAPI/AuthAPI.csproj"
COPY . .
WORKDIR "/src/AuthAPI"
RUN dotnet build "AuthAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "AuthAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY wait /wait
RUN chmod +x /wait
COPY /cert /root/.dotnet/https
ENTRYPOINT /wait && dotnet AuthAPI.dll
and docker compose file:
authapi:
image: ${DOCKER_REGISTRY-}authapi
build:
context: .
dockerfile: AuthAPI/Dockerfile
networks:
- peopleapp
environment:
WAIT_HOSTS: db:5432
WAIT_HOSTS_TIMEOUT: 300
ports:
- "8002:443"
When I run command
docker-compose -f "docker-compose.yml" build
I have a error
Step 10/23 : RUN dotnet restore "AnalyticAPI/AnalyticAPI.csproj"
---> Running in 2d29e1367d51
Determining projects to restore...
/usr/share/dotnet/sdk/3.1.300/NuGet.targets(128,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/src/AnalyticAPI/AnalyticAPI.csproj]
/usr/share/dotnet/sdk/3.1.300/NuGet.targets(128,5): error : Connection refused [/src/AnalyticAPI/AnalyticAPI.csproj]
ERROR: Service 'analyticapi' failed to build: The command '/bin/sh -c dotnet restore "AnalyticAPI/AnalyticAPI.csproj"' returned a non-zero code: 1
Sometimes it works fine. There are warning about Connection refused, but restoring is success.
I use Windows 10 + Hyper-V + Docker Desktop + Linux Containers + NET Core 3.1.
I had a similar problem.
It's works for me:
Add curl command to dockerfile before restore:
RUN curl https://api.nuget.org/v3/index.json -k
RUN dotnet restore "AuthAPI/AuthAPI.csproj"
Build image
Remove curl command from dockerfile
Build image again

Categories

Resources