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.
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
build
orpublish
step in yourdockerfile
. What is the Build.docker?App.Dockerfile
doesn't build anything, try the build withBuild.Dockerfile
.