cannot access webapp running in docker - asp.net

I followed a tutorial about creating a web-app using Docker. My dockerfile exposes port 5000:
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 5000
#EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR .
COPY ["CustomerApi/CustomerApi.csproj", "CustomerApi/"]
COPY ["CustomerApi.Domain/CustomerApi.Domain.csproj", "CustomerApi.Domain/"]
COPY ["CustomerApi.Service/CustomerApi.Service.csproj", "CustomerApi.Service/"]
COPY ["CustomerApi.Data/CustomerApi.Data.csproj", "CustomerApi.Data/"]
RUN dotnet restore "CustomerApi/CustomerApi.csproj"
COPY . .
WORKDIR "./CustomerApi"
RUN dotnet build "CustomerApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CustomerApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CustomerApi.dll"]
when I run the image locally using
docker run myrepo/demo:latest -p 5000:5000 -p 5001:5001 -e ASPNETCORE_HTTP_PORT=https://+:5001 -e ASPNETCORE_URLS=http://+:5000
and open my browser on https://localhost:5000 I get ERR_EMPTY_RESPONSE (I'm not sure why I need 5001 within my docker run-command at all, as it's not exposed within my dockerfile).
When I inspect the image within docker desktop I see this:
In particular it shows ASPNETCORE_URLS=http://+80, although I overwrote that above using -e ASPENTCORE_URLS=https://5000.

The options on docker run are split up in 2 parts:
Options before the image name are docker options
Options after the
image name override any CMD and is sent to the container
So your command should look like this
docker run -p 5000:5000 -p 5001:5001 -e ASPNETCORE_HTTP_PORT=https://+:5001 -e ASPNETCORE_URLS=http://+:5000 myrepo/demo:latest
EXPOSE doesn't actually do anything. It's mostly documentation about what ports you think the container uses. It can be wrong and if it's wrong you don't get any errors or warnings.

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

Docker errror: dotnet-file.dll does not exist

I have successfully builded my app and there is image listed my-app:2.7
But when I try to run it error says:
Any idea where is wrong?
docker run -it --rm -p 5000:80 --name my-app:2.7 uploadcore Unable to find image 'uploadcore:latest' locally docker: Error response from daemon: pull access denied for uploadcore, repository does not exist or may require 'docker login': denied: requested access to the resource is denied. See 'docker run --help'.
docker run -p 5000:80 my-app:2.7 Could not execute because the specified command or file was not found. Possible reasons for this include: You misspelled a built-in dotnet command. You intended to execute a .NET program, but dotnet-uploadcore.dll does not exist. You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Dockerfile:
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0 AS base
WORKDIR /app
EXPOSE 59518
EXPOSE 44364
FROM mcr.microsoft.com/dotnet/nightly/sdk:6.0 AS build
WORKDIR /src
COPY UploadCore/UploadCore.csproj UploadCore/
RUN dotnet restore UploadCore/UploadCore.csproj
COPY . .
WORKDIR /src/UploadCore
RUN dotnet build UploadCore.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish UploadCore.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "uploadcore.dll"]
Files structure
Context
Controllers
Migrations
Models
Properties
Service
Views
wwwroot
Program.cs
Startup.cs
UploadCore.csproj
appsettings.Development.json
appsettings.json
libman.json
Linux is case sensitive, so your ENTRYPOINT has to be
ENTRYPOINT ["dotnet", "UploadCore.dll"]
Your second docker run command works. You can add a container name with the --name option like this
docker run -p 5000:80 --name mycontainer my-app:2.7
The image name has to be the first parameter that isn't an option. In your first docker run command, the first one is uploadcore, so docker looks for an image called that and it can't find it.

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.

How to copy a directory from docker container to host using Dockerfile?

I'm using the following Dockerfile to build an asp.net project using docker containers.
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY . .
WORKDIR "/src/IdentityANSP/"
RUN dotnet build "IdentityANSP.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "IdentityANSP.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "IdentityANSP.dll"]
Here, the artifacts of the build will be placed in a new docker image. Instead of that, I want the artifacts to copied to a directory in Docker host machine (localhost). To get that done, how should I change the Dockerfile?
PS: I'm new to Docker as well as stackoverflow. So, please ignore my mistakes in the question. Thanks
Long story short (see this answer for details), you cannot make Dockefile to extract build artefacts out. However, you can copy files out of a running container:
# Build the image
docker build -t my_image .
# Spin up a container from the image
docker run --rm -d --name my_temporary_container my_image
# Copy file
docker cp my_temporary_container:/path/in/container /host/path
# Stop the container
docker stop my_temporary_container
If the image in question requires a lot of time to start or some external files/variables/etc, you can override the default command so that it will be gracefully doing nothing instead:
# Start a container that endlessly reads its own stdout stream
docker run --rm -d --name my_temporary_container --entrypoint="" my_image /bin/cat /dev/stdout

Resources