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.
Related
Im trying to make an ASP.NET API (.NET 6.0) that runs in docker.
I made the project with build in docker support from visual studio and set it to linux.
Also I included an custom assembly with functions i want to use abroad multiple applications.
Whenever I build the project in debug mode it works fine and starts up my project.
The build command:
docker build -f "C:\Users\USER\source\repos\PROJECT\PROJECT\Dockerfile" --force-rm -t PROJECT:dev --target base --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=PROJECT" "C:\Users\USER\source\repos\PROJECT"
But whenever i start it in release mode it gets rid of the "--target base" flag in the docker build command and gives me the error "The type or namespace name could not be found" of that assembly i try to include.
Also when i try a simple:
docker build -t /Dockerfile .
Gives me the same error.
My Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 443
ENV ASPNETCORE_URLS=http://+:443
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY Project* .
RUN dotnet restore "Project.csproj"
COPY . .
RUN dotnet build "Project.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Project.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.dll"]
I normally use Rider for that but for reproduction purposes I will describe the process by using the terminal
I created a .Net 5 Web Api and want to add Docker support for it. I tried to get into it by playing around with a sample app. So with the terminal I create a new Web Api
mkdir project
cd project
dotnet new sln
dotnet new webapi -o Api
dotnet sln add ./Api
I check that everything is fine with
cd Api
dotnet run
call https://localhost:5001/weatherforecast in the browser
close with ctrl + c
Inside the Api project I create a Dockerfile with this content
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet
WORKDIR /app
EXPOSE 80
COPY --from=build /app/out .
ENTRYPOINT [ "dotnet", "Rest.dll" ]
I also create a .dockerignore file with this content
.git
Dockerfile
bin/
obj/
I'm building the image with
docker build -t api .
When I now run
docker images
this image has a size of 209MB. I would like to know if this is "the best/right way to go" or if there is something I can improve in the Dockerfile or .dockerignore file.
Thanks in advance
You can base your Dockerfile off a smaller image. Searching the catalogue of available images in the Microsoft container registry (MCR) here, the Linux alpine (5.0-alpine) is probably the most lightweight. Note that for the other distros, there may be a regular and slim variant, where the regular is the SDK environment and the slim is the production environment.
Then you can structure your Dockerfile as follows to move the artefacts from the build output in the SDK image to the final lightweight deployment image:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app
# Main build (SDK environment)
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
RUN dotnet restore "webapi.csproj"
COPY . .
WORKDIR "/src/webapi"
RUN dotnet build "webapi.csproj" -c Release -o /app/build
# Publish build outputs to /app dir
FROM build AS publish
RUN dotnet publish "webapi.csproj" -c Release -o /app/publish
# Final deployment image
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "api.dll"]
In my example, I've used the Debian buster SDK image for the build stage and the buster slim for deployment.
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:
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.
I create my application and it successfully creates the docker container. When I try and run it
docker run -it --name myapp myapp:latest
I get the following error:
The specified framework 'Microsoft.AspNetCore.All', version '2.1.2' was not found.
...
- The following versions are installed:
2.1.1 at [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
When I log into the container, I can see that, in the /usr/share/dotnet/shared/Microsoft.AspNetCore.All folder, there is one subfolder called 2.1.2.
I can't figure out why it's saying that 2.1.1 is installed and 2.1.2 is not??
The following is my Dockerfile:
FROM microsoft/dotnet:2.1.400-sdk-stretch AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/dotnet:2.1.400-sdk-stretch AS build
WORKDIR /src
COPY source/application/MyApp/MyApp.csproj source/application/MyApp/
COPY source/application/MyApp.DependentProject/MyApp.DependentProject.csproj source/application/MyApp/
RUN dotnet restore source/application/MyApp/MyApp.csproj
COPY . .
WORKDIR /src/source/application/MyApp
RUN dotnet build MyApp.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish MyApp.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]
You could try changing:
FROM microsoft/dotnet:2.1.400-sdk-stretch AS base
to
FROM microsoft/dotnet AS base
or
FROM microsoft/dotnet:2.1.2-sdk AS base
See the docker docs and Microsoft docs on dotnet core and docker for further examples