Can't access an ASP.NET application started in a docker container - c#

I started a docker container with an ASP.NET app inside. The logs say that the application started successfully and is alive, but when I try to access the home page of the app I get the ERR_EMPTY_RESPONSE and the error page of the browser.
Here is my dockerfile:
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
COPY /bin/Debug/net5.0/ App/
WORKDIR /App
ENTRYPOINT ["dotnet", "TritaniumLocal.Web.Host.dll"]
Here is my docker build command: docker build -t "tritaniumlocal" .
Here is my docker run command: docker run -d -p 44311:44311 tritaniumlocal aspnetapp2.
The app starts from Visual Studio on port 44311. I tried also without port remap, but nothing helped
UPD: Here is a part of a log which can help:
warn: Microsoft.AspNetCore.Server.Kestrel[0] Unable to bind to http://localhost:44311 on the IPv6 loopback interface: 'Cannot assign requested address'. Hosting environment: Production Content root path: /App Now listening on: http://localhost:44311 Application started. Press Ctrl+C to shut down.

Related

C# WebApi deployed in docker is not exposing port to local machine

Currently, I have app (.Net Core 3.1) which is listening on port 40003. Now I'm trying to put it into a container. I created dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 40003
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["myProject/myProject/myProject.csproj", "myProject/myProject/"]
RUN dotnet restore "myProject/myProject/myProject.csproj" --configfile ./NuGet.Config
COPY . .
WORKDIR "/src/myProject/myProject"
RUN dotnet build "myProject.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "myProject.csproj" -c Release -o /app/publish
FROM base AS final
ENV ASPNETCORE_URLS=http://*:40003
ENV ASPNETCORE_ENVIRONMENT="development"
EXPOSE 40003
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "myProject.dll"]
As you see I exposed port 40003 and set variable ASPNETCORE_URLS.
I run it:
docker run -it --rm --name myTag -p 40003:40003 myTag
When I log into container:
docker exec -it myTag bash
and executing:
wget http://localhost:40003
I got the expected result. So I believe that my component inside the container is alive and listening
However when I am trying to go to
http://localhost:40003
from my local browser, I'm receiving ERR_EMPTY_RESPONSE.
Can you give some advice? Thank you in advance.
Edit:===================
I posted a complete docker file. (added first 3 lines to docker file which defines base).
Yes I'm sure that I posted to HTTP (not https).
http://localhost:40003 returns correct response which is a custom "selftest"
On not development env this endpoint is blocked. Something like:
[HttpGet]
[HttpPost]
public JsonResult<DiagnosticsResult> ServerStatus()
The response: I really got no response. No 200, 404 etc. simple nothing.
Also I'm logging now that result of self tests. Above when I'm inside the docker
INFORMATION 2021-11-30 09:15:48,738 [Omdc.SelfHost.Base.Tests.MinimumDiskSpaceTest.Run] MinimumDiskSpaceTest starting.
INFORMATION 2021-11-30 09:15:48,741 [Omdc.SelfHost.Base.Tests.MinimumDiskSpaceTest.Run] Drive: /, Volume label: /, Total available space: 82%
But when I'm requesting from a local browser there are neither console logs nor HTTP responses.
So my request to not reach the endpoint (?)

Run console application in google cloud>kubernetes

My Code:
public static void Start()
{
tcpListener = new TcpListener(IPAddress.Any, 26950);
tcpListener.Start();
tcpListener.BeginAcceptTcpClient(TCPConnectCallback, null);
while(true){Thread.Sleep(500);}
}
private static void TCPConnectCallback(IAsyncResult _result)
{
TcpClient _client = tcpListener.EndAcceptTcpClient(_result);
//Assign _client to local server
}
My Dockerfile to create and upload an Image:
FROM mcr.microsoft.com/dotnet/sdk
COPY . /app
WORKDIR /app
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "Test.dll"]
Command in Cloud Shell:
gcloud builds submit --tag gcr.io/t****/i****
After this, I deploy it on Kubernetes Engine>Workloads
Error: CrashLoopBackOff
Solution:
FROM mcr.microsoft.com/dotnet/sdk:3.1
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "run"]
The error CrashLoopBackOff indicates that a container is repeatedly crashing after restarting. A container might crash for many reasons
For example:
The application inside the container keeps crashing
Some type of parameters of the pod or container have been configured incorrectly
An error has been made when deploying Kubernetes
You can get more info about your error the CrashLoopBackOff error to know why your Pod's container is crashing
To see all Pods running in your cluster, run the following command:
kubectl get pods
And to get the Pod's logs, run the following command:
kubectl logs [POD_NAME]
You can also pass in the -p flag to get the logs for the previous instance of a Pod's container, if it exists.
Check "Exit Code" of the crashed container
You can find the exit code by performing the following tasks:
Run the following command:
kubectl describe pod [POD_NAME]
Review the value in the containers: CONTAINER_NAME: last state: exit code
Additionally, check if it has restarts.
For example you can use the command
watch -n1 kubectl get pods POD_NAME
To see if the pod got some of the following states:
creatingContainer --> running --> completed --> crashloopbackoff
You could see more information about it in the following link
Also you can check this article that can help you with this issue What is a CrashLoopBackOff? How to alert, debug / troubleshoot, and fix Kubernetes CrashLoopBackOff events.
Finally, if you provide more information like the logs and more details, the community could help you more.
Edit 1
Looks like your problem come from the FROM mcr.microsoft.com/dotnet/sdk
I have found the following documentation: Create a Dockerfile for an ASP.NET Core application
You could try to use something like:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
According to the official documentation : microsoft-dotnet-sdk these are the feature tags:
5.0 (Current)
docker pull mcr.microsoft.com/dotnet/sdk:5.0
3.1 (LTS)
docker pull mcr.microsoft.com/dotnet/sdk:3.1

Cannot access API run via docker on Port 80 in Postman

I have a simple API which I am running in Docker
When I run the command to run my image I get the output below
docker run newapi:latest mee
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://[::]:80
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /api
This all looks fine
However, I get connection refused when using Postman
http://localhost/weatherforecast
Any idea what is wrong?
My docker file is:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /api
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
EXPOSE 80
# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /api
COPY --from=build-env /api/out .
ENTRYPOINT ["dotnet", "api.dll"]
As you can see I am exposing port 80
I can use http://localhost:8080 but I just want to be able to use http://localhost
When you run the docker try mapping the port
docker run -p 80:80 newapi:latest mee

ASP.NET docker-compose app not coming up on assigned PORT

working my way through tutorials on dockerized the API WeatherForecast web template from ASP.NET core:
https://code.visualstudio.com/docs/containers/quickstart-aspnet-core
https://code.visualstudio.com/docs/containers/docker-compose
I had to start from here, because I wasn't getting a new image to build using the tutorial here: https://docs.docker.com/compose/aspnet-mssql-compose/
"1" works, which is great. However, "2" will not work on the localhost:5000/WeatherForecast port as advertised, and I'm having some trouble debugging why after many reviews of the available docs.
I should make a note that in creating the templated app from the command line, I did choose the --no-https option.
I then used docker ps to bring up the PORTS . The web app is using 5000/tcp, 0.0.0.0:32779->80/tcp . When I substitute 5000 for 32779 , I get the API string returned instead!
I know I'm missing something within docker-compose and could use some extra eyes on it. Thank you!
EDIT: For reference, the files below were generated by my VSCode editor.
1. I ran dotnet new webapi --no-https
2. I then brought up the VSCode "command pallete" and ran Docker: Add Dockerfiles to Workspace and selected 'yes' for the inclusion of docker-compose.yml file and Linux. I also choose to use port 5000. I use Fedora 30.
4. I run dotnet build from the project root in the terminal.
5. If I run from docker commands and make the ports explicit it will work as advertised, but if I run docker-compose -f <yml-file> up -d- --build, it will not.
I just re-read this and find it annoying that I'm stuck within VSCode to fix the issue (according to the docs)
By default Docker will assign a randomly chosen host port to a port exposed by a container (the container port). In this case the exposed (container) port is 5000, but it will be exposed on the host via a random port, such as 32737.
You can use specific port on the host by changing the Docker run options used by docker-run: debug task (defined in .vscode/tasks.json file). For example, if you want to use the same port (5000) to expose the service, the docker-run: debug task definition would look like this:
a. Dockerfile
# Please refer https://aka.ms/HTTPSinContainer on how to setup an
https developer certificate for your ASP .NET Core service.
version: '3.4'
services:
aspdotnetdocker2:
image: aspdotnetdocker2
build:
context: .
dockerfile: Dockerfile
ports:
- 5000
b. docker-compose.yml
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 5000
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["aspdotnet_docker2.csproj", "./"]
RUN dotnet restore "./aspdotnet_docker2.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "aspdotnet_docker2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "aspdotnet_docker2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "aspdotnet_docker2.dll"]
Have a look at docker-compose docs:
SHORT SYNTAX
Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).
So try:
ports:
- "5000:<port in ASPNETCORE_URLS>"
# e.g.
# - "5000:80"
# - "44388:443"

Accessing Docker container from another machine in an internal network

I'm attempting to run a asp.net core application from a raspberry pi using docker, I think I have the main parts down. I have a repository on github that is a simplistic asp.net core project. I have setup an account on docker cloud which build everytime I push to my github repo.
I have docker pulled my repository onto my pi:
I run the command:
docker run -d -p 8080:80 joro550/radiusnet --network=host
and I can see that it is running:
But when I go to my pi's ip address on port 8080 then I get this:
When I've been searching around for this people have suggested adding these flags (which I have tried and come up with the same results:
adding --network=host to the docker run command
adding -it to the docker run command
Adding Expose 80 into the docker file
I think at this point I'm at a bit of a lose as to how to access this thing.
The docker documentation does suggest running
`docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" myapp`
If I'm using windows 10 Nano containers, which I don't believe I'm using but when I run this command I get a resounding <no value>
Cutting it back to docker inspect -f "{{ .NetworkSettings.IPAddress }}" myapp gives me a different ip address to my ips internal ip address, which I've tried on port 8080 and get the same result
Doing a curl on both addresses gives me the same result of connection refused:
Here's my docker file for anyone interested:
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# copy csproj and restore as distinct layers
COPY /src ./
RUN dotnet restore
# copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out -r linux-arm
# build runtime image
FROM microsoft/dotnet:2.0.0-runtime-stretch-arm32v7
WORKDIR /app
COPY --from=build-env /app/src/RadiusNet.Web/out .
EXPOSE 80
ENTRYPOINT ["dotnet", "RadiusNet.Web.dll"]
If any more information is needed please ask, I'm pretty new to Docker so I just did a bit of a knowledge dump of my current situation.
Link to github project (if it's needed): https://github.com/joro550/RadiusNet
Any help - at this point will be greatly appreciated
Cutting it back to docker inspect -f "{{ .NetworkSettings.IPAddress }}" myapp gives me a different ip address to my ips internal ip address, which I've tried on port 8080 and get the same result
Try curling the port 5000 and 80 with this IP address from inside the raspbery pi rather than 8080. Plus, are you sure you are exposing the right port? You have "expose 80" but the port 8080 is mapped to 5000, and docker ps shows no mapping for 80

Categories

Resources