I created a basic Rest API using ASP.NET Core 5 i want to make run with docker. The application works fine on IIS Express.
https://learn.microsoft.com/fr-fr/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio
I also want to create a docker container in order to launch the application.
In the project folder i created a Docker folder with several files. Here is my App.dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
ARG WEBAPP_VERSION=0.0.1
LABEL maintainer=anymail@email_server.com \
Name=webapp \
Version=${WEBAPP_VERSION}
ARG URL_PORT
WORKDIR /app
ENV NUGET_XMLDOC_MODE skip
ENV ASPNETCORE_URLS http://*:${URL_PORT}
ENTRYPOINT [ "dotnet", "WebApplication.dll" ]
I also have a Build.docker file:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
## Can be Debug or Release.
ARG BUILD_CONFIG=Debug
ARG BUILDER_VERSION=0.0.1
LABEL maintainer=some_email@email_server.com \
Name=webapp-build-${BUILD_CONFIG} \
Version=${BUILDER_VERSION}
## Will be the path mapped to the external volume.
ARG BUILD_LOCATION=/app/out
ENV NUGET_XMLDOC_MODE skip
WORKDIR /app
COPY *.csproj .
RUN dotnet restore
COPY . /app
RUN dotnet publish --output ${BUILD_LOCATION} --configuration ${BUILD_CONFIG}
Finally I have a docker-compose.yml
version: '3'
services:
webapp:
container_name: webapp.test
image: webapp:${WEBAPP_VERSION}
build:
context: ../
dockerfile: ./Docker/App.dockerfile
args:
WEBAPP_VERSION: ${WEBAPP_VERSION}
URL_PORT: ${URL_PORT}
ports:
- "5000:${URL_PORT}"
volumes:
- appbuild:/app
links:
- mysql
environment:
MYSQL_SERVER_NAME: ${MYSQL_SERVER_NAME}
env_file:
- secrets.env
depends_on:
- builder
builder:
container_name: builder
image: webapp:${BUILDER_VERSION}.${BUILD_CONFIG}
build:
context: ../
dockerfile: ./Docker/Build.dockerfile
args:
BUILDER_VERSION: ${BUILDER_VERSION}
BUILD_CONFIG: ${BUILD_CONFIG}
BUILD_LOCATION: ${BUILD_LOCATION}
volumes:
- appbuild:${BUILD_LOCATION}
mysql:
container_name: ${MYSQL_SERVER_NAME}
image: mysql/mysql-server:8.0.23
restart: always
volumes:
- dbvol:/var/lib/mysql
environment:
MYSQL_RANDOM_ROOT_PASSWORD: root
env_file:
- secrets.env
volumes:
appbuild:
dbvol:
Finally, I launch the following command line :
docker build -f Docker/App.dockerfile -t webapp:Debug --build-arg URL_PORT=7909 .
the process is rather quick. I also launch this command line :
docker run --name webapp.test -p 5000:7909 -it webapp:Debug
I unfortunatelly obtain an error message.
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
* You intended to execute a .NET program:
The application 'WebApplication.dll' does not exist.
* You intended to execute a .NET SDK command:
It was not possible to find any installed .NET SDKs.
Install a .NET SDK from:
https://aka.ms/dotnet-download
Could not execute because the application was not found or a compatible .NET SDK is not installed. Possible reasons for this include: * You intended to execute a .NET program: The application 'FileProcessSvcPOC.dll' does not exist.
If the executable file isn't found, you'll see a message similar to the following: 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-xyz does not exist.
If the expected runtime isn't found, they follow normal .NET runtime roll-forward rules such as: An application rolls forward to the highest patch release of the specified major and minor version.
When a .NET tool fails to run, most likely you ran into one of the following issues: The executable file for the tool wasn't found. The correct version of the .NET runtime wasn't found. If the executable file isn't found, you'll see a message similar to the following:
Your build Dockerfile uses the ASP.NET Core runtime container image:
FROM mcr.microsoft.com/dotnet/aspnet:5.0
This container doesn't have an SDK, just the runtime. When you build it, it fails:
RUN dotnet restore
Could not execute because the application was not found or a compatible .NET SDK is not installed. and The application 'restore' does not exist
Because the dotnet restore
command is an SDK command. It's not available if you are just using the runtime.
You should use the SDK container for your build Dockerfile (not your runtime Dockerfile, that's fine):
FROM mcr.microsoft.com/dotnet/sdk:5.0
If you already installed the SDK and you have x64 machine;
Delete the following item from System Variables
> Path
C:\Program Files (x86)\dotnet
In my case dot net SDKs were installed in different locations. A simple
where.exe dotnet
on powershell returned the following
There were 2 entries in system environment variables i.e. The one with x86 was above so it was taking precedence, so I swapped both of them and hurray it started working. Special thanks to https://www.hanselman.com/blog/dotnet-could-not-execute-because-the-application-was-not-found-or-a-compatible-net-sdk-is-not-installed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With