I'm trying to build and publish my .net core app with a docker file. My folder structure is as follows:
Parent
-DockerApp (Solution folder)
-DockerApp (project folder)
-Dockerfile
-.dockerignore
-Nginx
-Dockerfile
-nginx.config
Below is my docker file. For now, I am not even trying to do a docker-compose, I am simply trying to build a single image.
Below is the command I run. I run it from the parent (root) folder:
docker build -t dockerexample ./DockerApp/DockerApp
Below is the dockerfile:
# get the latest debian based x64 runtime image for the asp.net core runtime env
FROM mcr.microsoft.com/dotnet/core/aspnet:latest as run-env
# expose the 80 and 443 ports within the image
EXPOSE 80
EXPOSE 443
# set the base directory to /app
WORKDIR /app
# get the latest debian based x64 sdk image for the asp.net core build env
FROM mcr.microsoft.com/dotnet/core/sdk:latest as build-env
# set the working directory to /app/src
WORKDIR /app/src
# copy just the .csproj files for all projects into relevant project folders so that the nuget packages
# can be restored and the layer cached
COPY ["DockerApp.csproj", "DockerApp/"]
# set working directory to /app/src/DockerApp
WORKDIR /app/src/DockerApp
# restore nuget packages for just the API project (and as it references other projects)
# their nuget packages will also be restored
RUN dotnet restore "DockerApp.csproj"
# Copy everything else and build as a separate layer
COPY . .
RUN ls -l
# build the project in release mode and copy files into /app/src/DockerApp/app
RUN dotnet build "DockerApp.csproj" -c Release -o /app
# get the build-env image as the publish image (is this needed?)
FROM build-env AS publish
# publish (pack the app and its dependencies) the project into /app/src/DockerApp/out
RUN dotnet publish "DockerApp.csproj" -c Release -o /out
# set image back to runtime with final tag
FROM run-env AS final
# set working directory to /app/src/DockerApp/out
WORKDIR /app/src/DockerApp/out
# copy all files from the prior build stage of publish from the /app/src/DockerApp/out
# to /app/src/DockerApp/out in the destination image
COPY --from=publish /out .
# start the application
ENTRYPOINT ["dotnet", "DockerApp.dll"]
When I run the ls -l command, I see all my project files in the /app/src/DockerApp folder within the image so I'm unsure why its not working.
Below is the output from the ls -l command. If I manually run build on the project file within the project folder, it runs fine without any issues. What is wrong with my path as I'm not sure why it will not build.
drwxr-xr-x 2 root root 4096 Aug 24 20:58 Controllers
-rwxr-xr-x 1 root root 412 Aug 24 20:58 DockerApp.csproj
-rwxr-xr-x 1 root root 631 Aug 24 20:58 Program.cs
drwxr-xr-x 2 root root 4096 Aug 24 20:58 Properties
-rwxr-xr-x 1 root root 1600 Aug 25 05:44 Startup.cs
-rwxr-xr-x 1 root root 146 Aug 24 20:58 appsettings.Development.json
-rwxr-xr-x 1 root root 105 Aug 24 20:58 appsettings.json
drwxr-xr-x 2 root root 4096 Aug 25 23:20 obj
I noticed I was doing quite a bit of extra steps with some incorrect steps thrown in to boot. I have read some more documentation on this and fixed my docker file as follows:
# get the latest debian based x64 sdk image for the asp.net core build env
FROM mcr.microsoft.com/dotnet/core/sdk:latest as build-env
# set the base directory to /app
WORKDIR /app
# copy just the .csproj files for all projects into relevant project folders so that the nuget packages
# can be restored and the layer cached
COPY DockerApp.csproj ./
RUN dotnet restore DockerApp.csproj
# Copy everything else
COPY . ./
# publish (pack the app and its dependencies) the project
#RUN dotnet publish DockerApp.csproj -c Release -o out
RUN dotnet publish DockerApp.csproj -o out
# set image back to runtime with final tag
FROM mcr.microsoft.com/dotnet/core/aspnet:latest AS final
# copy all files from the prior build stage of publish
COPY --from=build-env /app/out .
EXPOSE 5005
ENV ASPNETCORE_URLS http://*:5005
ENV ASPNETCORE_ENVIRONMENT Development
# start the application
ENTRYPOINT ["dotnet", "DockerApp.dll"]
I also ended up exposing a different port so I can use nginx as a reverse proxy instead of directly utilizing kestrel even for local development (with a self signed cert!)