C# app runs fine on VM, but not on docker - asp.net

Have a small ftp server with http in a app, which runs fine in a vm.
I'm trying to dockerize it, which when I do, works ok If I ssh in the container (I can telnet both localhost and the container VM), but I can't access from the machine.
Here is my dockerfile:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["proj.csproj", "proj"]
RUN dotnet restore "proj/proj.csproj"
COPY . .
WORKDIR "/src/proj"
RUN dotnet build "proj.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "proj.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
EXPOSE 21
EXPOSE 80
ENV ASPNETCORE_ENVIRONMENT=development
ENTRYPOINT ["dotnet", "proj.dll", "--urls", "http://0.0.0.0:80"]
Docker inspect:
"Ports": {
"21/tcp": null,
"80/tcp": null
PS: I changed the paths manually to not expose my project, so may have some mistakes with folders name

Related

Unable to run ASP.NET Core migration in docker container

I have created Dockerfile for the backend api. I want to update my database before the application starts to run.
Below is the docker file
# Build Stage
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /source
COPY ./orh-backend ./project
COPY ./function-script ./function-script
RUN dotnet restore "./project/md.api/md.api.csproj" --disable-parallel
RUN dotnet publish "./project/md.api/md.api.csproj" -c release -o /app --no-restore
WORKDIR /source/project/md.ef
RUN dotnet tool install --global dotnet-ef
RUN dotnet ef database update --context=TransactionDbContext
# Serve stage
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal
WORKDIR /app
COPY --from=build /app ./
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
ENV ASPNETCORE_ENVIRONMENT=Production
# ENTRYPOINT ["dotnet", "orch.api.dll"]
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
When I run docker compose build below error comes
I was expecting tables to be created.
I have tried the below commands as well.
CMD ["dotnet", "ef", " database update --context=TransactionDbContext" ]
ENV PATH $PATH:/root/.dotnet/tools

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"]

cannot access webapp running in docker

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.

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.

Docker Compose copies one folder but not another

I am still learning Docker and this situation has me stumped. Docker will copy one folder to the image/container but the other folder it skips and I can't figure out why.
Docker desktop 4.X Ubuntu (Linux) container
.NET 5.0 Blazor application
So in the application build the following two folders are output:
In the container though only one folder is there
My first thought would be caching but I am using docker-compose build --no-cache and docker-compose up -d --force-recreate.
I don't think the Dockerfile is the problem, or else I would not see any folders.
Update
Added Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 465
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["./Email.Shared/Email.Shared.csproj", "./Email.Shared/"]
COPY ["./Email.API/Email.API.csproj", "./"]
RUN dotnet restore "Email.API.csproj"
COPY . .
WORKDIR "/src/Email.API"
RUN dotnet build "Email.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Email.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Email.API.dll", "--urls", "http://+:5150"]

Resources