Wednesday, February 7, 2024

[SOLVED] Cannot send requests to a NET 8 WebApi backend that is on jenkins ci/cd pipeline

Issue

I created an item on jenkins.

src="https://i.stack.imgur.com/od8QK.png" alt="enter image description here" />

here is my execute shell configs

enter image description here

here is my dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY ["NobetciBorsa_API.csproj", "./"]
RUN dotnet restore "NobetciBorsa_API.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "NobetciBorsa_API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "NobetciBorsa_API.csproj" --no-restore -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "NobetciBorsa_API.dll"]

and here is console output on jenkins item

Started by user nobetciborsa
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/NobetciBorsa
The recommended git tool is: NONE
using credential JenkinsToken
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/NobetciBorsa/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/IMPORTRADER/BackEnd.git # timeout=10
Fetching upstream changes from https://github.com/IMPORTRADER/BackEnd.git
 > git --version # timeout=10
 > git --version # 'git version 2.39.2'
using GIT_ASKPASS to set credentials JenkinsToken
 > git fetch --tags --force --progress -- https://github.com/IMPORTRADER/BackEnd.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 236715bcad04306fea703188b4ad0b40d8c2d1b1 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 236715bcad04306fea703188b4ad0b40d8c2d1b1 # timeout=10
Commit message: "new port"
 > git rev-list --no-walk 236715bcad04306fea703188b4ad0b40d8c2d1b1 # timeout=10
[NobetciBorsa] $ /bin/sh -xe /tmp/jenkins6376362888161306814.sh
+ docker build -t nobetciborsa/jenkinswithdockerlimitless .
#0 building with "default" instance using docker driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 564B done
#1 DONE 0.0s

#2 [internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0
#2 DONE 0.1s

#3 [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:8.0
#3 DONE 0.1s

#4 [internal] load .dockerignore
#4 transferring context: 356B done
#4 DONE 0.0s

#5 [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:8e77ad6fb7c33c17f026424d3bef05ea2ee15d1621e28f312adeab4dc1005866
#5 DONE 0.0s

#6 [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:8.0@sha256:1d6ca86c35975c08bd63a066f190fb7bd68fbc2d675d70b2728d458921743a24
#6 DONE 0.0s

#7 [internal] load build context
#7 transferring context: 2.78kB done
#7 DONE 0.0s

#8 [build 5/7] COPY . .
#8 CACHED

#9 [publish 1/1] RUN dotnet publish "NobetciBorsa_API.csproj" --no-restore -c Release -o /app/publish
#9 CACHED

#10 [build 6/7] WORKDIR /src/
#10 CACHED

#11 [base 2/2] WORKDIR /app
#11 CACHED

#12 [build 2/7] WORKDIR /src
#12 CACHED

#13 [build 7/7] RUN dotnet build "NobetciBorsa_API.csproj" -c Release -o /app/build
#13 CACHED

#14 [final 1/2] WORKDIR /app
#14 CACHED

#15 [build 3/7] COPY [NobetciBorsa_API.csproj, ./]
#15 CACHED

#16 [build 4/7] RUN dotnet restore "NobetciBorsa_API.csproj"
#16 CACHED

#17 [final 2/2] COPY --from=publish /app/publish .
#17 CACHED

#18 exporting to image
#18 exporting layers done
#18 writing image sha256:bcbfc63a70dde0c7f61f3c520b4c19cf52ba5a0cb99f7eea40cbde0499a86531 done
#18 naming to docker.io/nobetciborsa/jenkinswithdockerlimitless done
#18 DONE 0.0s
+ docker run -d -p 1001:1001 -p 1000:1000/tcp -p 1000:1000/udp nobetciborsa/jenkinswithdockerlimitless
5379c3bf18f960d8d83499b3c8837112645050521dd91bb56a15781371d8bea0
Finished: SUCCESS

it is looking active at port 1001 with "docker ps" command

enter image description here

I have set the port to 1001 in program.cs

enter image description here

When I want to access swagger on server ip address this is the error I get. I tried a lot of method but I could not fix that. What should I do?

enter image description here


Solution

Haven't seen your code, but in the ASP.NET Core templates Swagger is only added when you run in Development environment, so my assumption would be it's the same for you here.

Here what the template usually looks like:

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

You are running your container in Production environment since you don't explicitly set it otherwise (Production is the default), therefore Swagger is not registered with DI and therefore you cannot access it.

You could adjust your docker run command to add an option -e ASPNETCORE_ENVIRONMENT=Development which sets the environment variable ASPNETCORE_ENVIRONMENT which is read by ASP.NET Core and sets the environment to Development.

For details please see the Microsoft Docs - Use multiple environments in ASP.NET Core.



Answered By - Mushroomator
Answer Checked By - Mary Flores (WPSolving Volunteer)