How to access ASP.NET Swagger API published in Docker? - asp.net

I have made an ASP.NET API with scaffolding. I have generated a docker file in Visual Studio 2019:
#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:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["CoinAPI.csproj", "CoinAPI/"]
RUN dotnet restore "CoinAPI/CoinAPI.csproj"
WORKDIR "/src/CoinAPI"
COPY . .
RUN dotnet build "CoinAPI.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CoinAPI.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoinAPI.dll"]
I am building the project with command:
docker build -t coinapi .
And running it with:
docker run -dp 12344:12344 coinapi
The container is running in docker desktop in Windows, but when I access localhost:12344 I get an error 404. After adding this line of code webBuilder.UseUrls("http://localhost:12344"); in Program.cs file, I am getting an ERR_EMPTY_RESPONSE when accessing the URL. What can I do to successfully access my Swagger API run in a docker container?

The application listens to ports 80 and 443 by default, try this
docker run -dp 12344:80 coinapi
And you should access the web with http://localhost:12344

The thing that did the trick was to change webBuilder.UseUrls("http://localhost:12344"); to webBuilder.UseUrls("http://+:12344"); and now the API works and I can connect to it via the browser with http://localhost:12344/

Related

Adding ASPNETCORE_URL in Dockerfile having no effect

I created a template ASP.Net docker project and made the following changes:
Change it to Asp.Net 7
Only EXPOSE 5000 in the Dockerfile
Add ENV ASPNETCORE_URLS=http://+:5000 to the Dockerfile
Now when I run the Docker locally it tries and fails to go to https://localhost:0/. Changing the port in the url to 5000 and changing to http doesn't help.
Visual studio is telling me the Container Port is 5000:
However the ASPNETCORE_URLS remain +443 and +80 in the Environment tab:
Shouldn't they be swapped out for my rules? I just want to be able to go to localhost:5000 and see the application. What am I missing?
My Dockerfile is:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["IANSW.Web/IANSW.Web.csproj", "IANSW.Web/"]
RUN dotnet restore "IANSW.Web/IANSW.Web.csproj"
COPY . .
WORKDIR "/src/IANSW.Web"
RUN dotnet build "IANSW.Web.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "IANSW.Web.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "IANSW.Web.dll"]

How to run an ASP .NET 6 API in a docker container on a raspberry pi / armv7

If I deploy my API directly I can start it via dotnet <app.dll>.
I want to deploy it in a container tho. However, the container always exits with an error code 132 or 130 depending on the Dockerfile used. I will post it here:
FROM mcr.microsoft.com/dotnet/aspnet:3.1-bullseye-slim-arm32v7 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build -r linux-arm
FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish -r linux-arm
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]
The first line of this Dockerfile is what I have been changing up in order to "find" the correct runtime. I tried many containers listed here at the Linux arm32 Tags section. All containers I tried exit with an error immediately:
I would greatly appreciate any help on how to resolve this issue :)
EDIT:
I use a raspberry pi 4 Model B with 8GB of RAM

This site can’t be reached - Docker

I have created the Containers / Apps
But when I run it, I got this error message: This site can’t be reached. localhost unexpectedly closed the connection.
But when I run from Visual Studio, I can run it.
Extra troubleshooting steps is below
This is docker file
FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# 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:5.0-focal AS build
WORKDIR /src
COPY ["WebApplication3/WebApplication3.csproj", "WebApplication3/"]
RUN dotnet restore "WebApplication3/WebApplication3.csproj"
COPY . .
WORKDIR "/src/WebApplication3"
RUN dotnet build "WebApplication3.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApplication3.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication3.dll"]
By default, Swagger is only available when the app runs in development mode. Docker containers, by default, don't run in development mode.
Try accessing the API directly on http://localhost:49155/WeatherForecast
You can run the container in development mode by setting the environment variable ASPNETCORE_ENVIRONMENT to Development by adding the option -e ASPNETCORE_ENVIRONMENT=Development to your docker run command. Then Swagger should be available.

ASP.NET Core app for Raspberry Pi using Docker in VS2017

I want to deploy my Asp.Net Core 2.1 app to my Raspberry Pi 3.
Here is the scenario:
I developed the application in VS 2017 as a docker wrapped app
Raspberry Pi is running Raspbian with docker installed.
The application is running fine on development machine, but when I deploy the image to Raspberry and try to run it there I'm getting the error:
standard_init_linux.go:190: exec user process caused "exec format error"
I assume that this is happening because my image has wrong architecture (intel instead of arm), but I don't know how to fix that. Please advise.
This is my docker file:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["WebApplication4/WebApplication4.csproj", "WebApplication4/"]
RUN dotnet restore "WebApplication4/WebApplication4.csproj"
COPY . .
WORKDIR "/src/WebApplication4"
RUN dotnet build "WebApplication4.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebApplication4.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication4.dll"]
Have you tried this?
# This Dockerfile uses nightly preview builds for .NET Core
# It will be updated to .NET Core 2.2 shortly
FROM microsoft/dotnet-nightly:2.1-sdk AS build
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore
# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet-nightly:2.1-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/out ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
This is suggested at the official dockerhub. There are also some images tagged with arm32.

In Docker for windows, how do i access a webapi running on my dev machine from the container i aslo have on my machine?

I am currently running docker for windows with a container using the asp.net core 2.0 image.
I also have a web api application running on iis on the development machine (the same machine i have docker installed with the container)
I need to be able to make an API request to the api on my dev machine.
I cant seem to get this to work.
Any help would be appreciated
Update:
My docker file
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY mysol.sln ./
COPY mysol.Web/mysol.Web.csproj mysol.Web/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/CraOrchestrator.Web
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "mysol.Web.dll"]
I am running the container with
docker run -dit -p 1253:80 -v c:/data:/data --name sol mysol:latest
There should not be any problem for your containerised app to reach an app running on the host or even a different container. The problem may be in the .dockerfile.
You only need the executables in your image to correctly run your application. Your .dockerfile may look like:
# use microsoft dotnet core as base image - including ASP.NET Core
FROM microsoft/dotnet:2.0.5-runtime
# set the working directory
WORKDIR /app
# copy executables
ADD . /app
# make port 5050 available - should not matter much since this is a client app
EXPOSE 5050
# run app
CMD ["dotnet", "mysol.Web.dll"]
To create the image:
docker build -t app-image .
To run the container (and so your application):
docker run --name myapp -p 4000:5050 app-image
You may not require the port setup since the application you run is the one performing requests to the Web API running in IIS.

Resources