Configuring Docker to create a container for an Angular App - c#

I am trying to get a default Angular App working inside a Docker container. Here are the steps I have followed:
1) Open Visual Studio 2017 and select: File/New/Project.
2) Select ASP.NET Core Web Application. Click OK.
3) Select .NET Core 2.2 and Angular. I notice that 'Enable Docker Support' is greyed out. Not sure why. Tried the first three steps in Visual Studio 2019 and it is the same. Untick Configure for HTTPS. Click OK.
4) Right click on the project and select Add/Container Orchestration Support. Select the Container Orchestration as Docker Compose and the target OS as Linux.
5) Rebuild the app
When I launch the app I see this:
My Docker File looks like this:
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["Web/WebSPA/WebSPA.csproj", "Web/WebSPA/"]
RUN dotnet restore "Web/WebSPA/WebSPA.csproj"
COPY . .
WORKDIR "/src/Web/WebSPA"
RUN dotnet build "WebSPA.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "WebSPA.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebSPA.dll"]
I saw another question, which suggested changing the DockerFile to something like this:
ARG NODE_IMAGE=node:8.11
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS base
WORKDIR /app
EXPOSE 80
FROM ${NODE_IMAGE} as node-build
WORKDIR /web
COPY src/Web/WebSPA .
RUN npm install
RUN npm run build:prod
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /src
COPY scripts scripts/
COPY src/Web/*/*.csproj /src/csproj-files/
COPY . .
COPY --from=node-build /web/wwwroot /src/src/Web/WebSPA/wwwroot/
WORKDIR /src/src/Web/WebSPA
RUN dotnet publish -c Release -o /app
FROM build AS publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebSPA.dll"]
However, it made no difference.

Related

mcr.microsoft.com/dotnet/aspnet:6.0-jammy-chiseled: not found

I have created the default WeatherForecast web api project with Docker support.
By default, the dockerfile is using aspnet: 6.0
But I would like to upgrade it to 6.0-jammy-chiseled. This is my dockerfile after I have updated it to 6.0-jammy-chiseled
FROM mcr.microsoft.com/dotnet/aspnet:6.0-jammy-chiseled AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0-jammy-chiseled AS build
WORKDIR /src
COPY ["Weather2.csproj", "."]
RUN dotnet restore "./Weather2.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Weather2.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Weather2.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Weather2.dll"]
When I run it, I got an error:
mcr.microsoft.com/dotnet/aspnet:6.0-jammy-chiseled: not found
I try to pull the image manually with docker pull mcr.microsoft.com/dotnet/nightly/aspnet:6.0-jammy-chiseled and it pull without issue.
Based on the Docker file of the official sample app, I think that your Docker file should be revised as
FROM mcr.microsoft.com/dotnet/nightly/aspnet:6.0-jammy-chiseled AS base (add nightly in the image path)
and
FROM mcr.microsoft.com/dotnet/sdk:6.0-jammy AS build (remove -chiseled as SDK has no need to be chiseled and Microsoft will not publish chiseled SDK but only chiseled runtime.

how to avoid manually copying csproj files in docker file

I have multiple dotnet projects with multi level nesting and I am specifying each csproj with their path structure below for each and every application's docker file.
Whenever there is a addition and deletion of projects all docker file have to be update.
Can I avoid it and make some generic docker file?
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY My.sln .
COPY ["Core/Events.csproj", "Core/"]
COPY ["Core/Infra/Services/Services.csproj", "Core/Infra/Services/"]
COPY ["Features/Feature.A.API/Feature.A.API.csproj", "Features/Feature.A.API/"]
RUN dotnet restore
COPY . .
You could store all needed projects in a root dir lets call it src then you just have to use the main project for restore/build/publish (the one with your asp .net core webapi), all the other sources (dependencies) needed during build would then be present (in the src dir), with correct paths.
For example:
src/Core/....
src/Features/...
src/Features/Feature.A.API/Dockerfile
The docker file would then look like this (aspnet 5.0):
with place holders:
[API_DIR] in your case: Features/Feature.A.API/
[API_PROJECT] in your case: Feature.A.API
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["src/[API_DIR]/[API_PROJECT].csproj", "[API_DIR]/"]
RUN dotnet restore "[API_DIR]/[API_PROJECT].csproj"
COPY ["src/", "."]
WORKDIR "src/[API_DIR]"
RUN dotnet build "[API_PROJECT].csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "[API_PROJECT].csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "[API_PROJECT].dll"]
Run it from Folder which contains the src dir (this is your build context), with:
docker build src/Features/Feature.A.API/Dockerfile

Dotnet Core Docker Environment Variables

I ran into some problems with dotnet core 3.1 and the visual Studio generated Dockerfile.
I try to put a password in the environment variables of docker via the Dockerfile but when I get read the varibale with Environment.GetEnvironmentVariable("HETZNER_PASSWORD") the variable is null.
I found someone who suggested it the way I tried here
My Dockerfile looks like this:
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["HetznerStorageboxStat/HetznerStorageboxStat.csproj", "HetznerStorageboxStat/"]
RUN dotnet restore "HetznerStorageboxStat/HetznerStorageboxStat.csproj"
COPY . .
WORKDIR "/src/HetznerStorageboxStat"
RUN dotnet build "HetznerStorageboxStat.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "HetznerStorageboxStat.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV HETZNER_USERNAME=USERNAME
ENV HETZNER_PASSWORD=PASSWORD
ENV INFLUX_URL=http://10.20.10.1:8086/write?db=hetzner
ENTRYPOINT ["dotnet", "HetznerStorageboxStat.dll"]
What am I doing wrong? All the other tutorials are years old or use ASP.Net
Thanks in advance
You can set the environment variable for your service in the docker-compose.override.yml file under environment:

Missing files when running ASP.NET Core application in Docker: How to display files when the container is running?

I have a simple ASP.NET Core solution that I dockerize and seems that some files that are supposedly copied to the output as always are not present when my application is running via Docker.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
ENV RUNNING_IN_DOCKER=true
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS source
WORKDIR /src
COPY ["SecretProject.Core/SecretProject.Core.csproj", "SecretProject.Core/"]
COPY ["SecretProject.DAL/SecretProject.DAL.csproj", "SecretProject.DAL/"]
COPY ["SecretProject.Utils/SecretProject.Utils.csproj", "SecretProject.Utils/"]
COPY ["SecretProject.Externals/SecretProject.Externals.csproj", "SecretProject.Externals/"]
COPY ["NuGet.Config", "/"]
RUN dotnet restore "SecretProject.Core/SecretProject.Core.csproj"
FROM source AS build
COPY . .
WORKDIR "/src/SecretProject.Core"
RUN dotnet build "SecretProject.Core.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "SecretProject.Core.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SecretProject.Core.dll"]
Missing files are the ones there: SecretProject.Core/Views/EmailTemplates/*.*
I would like to see the files when the container is running, I am indeed suspecting that not everything is copied.
Instead of having all these lines
COPY ["SecretProject.Core/SecretProject.Core.csproj", "SecretProject.Core/"]
COPY ["SecretProject.DAL/SecretProject.DAL.csproj", "SecretProject.DAL/"]
COPY ["SecretProject.Utils/SecretProject.Utils.csproj", "SecretProject.Utils/"]
COPY ["SecretProject.Externals/SecretProject.Externals.csproj", "SecretProject.Externals/"]
COPY ["NuGet.Config", "/"]
Replace for
COPY . .

docker container does not allocates the port

I'm trying to place my simple REST Api (built on asp.net core) into docker container.
I have the following 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 ["/", "SimpleApp.Api/"]
RUN dotnet restore "SimpleApp.Api/SimpleApp.Api.csproj"
COPY . .
WORKDIR "/src/SimpleApp.Api"
RUN dotnet build "SimpleApp.Api.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "SimpleApp.Api.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SimpleApp.Api.dll"]
I can successfully build an image from this file, but when I run the container I can't, for example, call none of the URLs + I have swagger on top of my API, so it should show me a page but it doesn't.
docker run -d --name "mySimpleContainer" "MySimpleApp:latest" --ports 5000:80
The url like localhost:5000 doesn't work so firstly I checked the logs:
........
2019-02-02 13:37:17.3635||DEBUG|Microsoft.AspNetCore.Hosting.Internal.WebHost|Loaded hosting startup assembly SimpleApp.Api
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
looks good to me. Secondary, I checked if 5000 is busy or not... and suddenly it's not!!!
to check the port I used netstat -an | find ":5000"
Does anyone know where I failed?

Categories