ASP.NET 5: docker build with multi-projects solution

24,583

Solution 1

Updated: Feb 14, 2018

My previous answer doesn't take in account the best practices for writing Dockerfile. It also doesn't work for those solutions which have multiple web projects where each application must run in a separate Docker container. The better way would be to build and publish ASP.NET Core application in development environment and run the application in Docker container which is optimized for production use.

  1. Create Dockerfile in the web application folder
  2. Put the following content into it

    FROM microsoft/aspnetcore
    
    COPY ./publish /publish
    WORKDIR /publish
    EXPOSE 5000/tcp
    ENTRYPOINT ["dotnet", "QuizzCorrector.dll"]
    
  3. Build and publish the project by running dotnet publish -c Release -o publish
  4. Build Docker image by running docker build -t QuizzCorrector

Note that microsoft/aspnetcore Docker image is the runtime-only image which doesn't provide .NET Core SDK what makes it lighter and better for production use.


It might be late, but I'm going to answer this question for those who are still trying to get it working. You need to create a Dockerfile on solution level. Then you need to update your working folder to use the one where you web project is located. Here is below the final Dockerfile taking in account that kpm and k utilites were replaced with one dotnet tool.

FROM microsoft/dotnet:latest

COPY . /app
WORKDIR /app/src/QuizzCorrector
RUN ["dotnet", "restore"]

EXPOSE 5004
ENTRYPOINT ["dotnet", "run"]

Solution 2

For Future Reference & anyone having the same issue. Microsoft released A Sample dotnet core application with docker containers dotnet-architecture/eShopOnContainers Project here

You can refer to the link or find the Example below

Example :

FROM microsoft/aspnetcore:2.0.3 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY . .
RUN dotnet restore -nowarn:msb3202,nu1503
WORKDIR /src/src/Services/Basket/Basket.API
RUN dotnet build --no-restore -c Release -o /app

FROM build AS publish
RUN dotnet publish --no-restore -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Basket.API.dll"]

Solution 3

To be able to build and run multiple xprojects in DockerHub with ASP.NET CORE 1.0 I got a similar problem to this. This led me here and helped me. My solution was this:

Solution structure

```

Root
│   AuthenticationService.sln
│   Dockerfile    
│
└───src
│   │
│   └───AuthenticationService
│   └───DataAccess
│   └───EntityDataModels
│   └───Services
.

Dockerfile:

FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app/src/AuthenticationService
RUN ["dotnet", "restore"]

WORKDIR /app/src/DataAccess
RUN ["dotnet", "restore"]

WORKDIR /app/src/EntityDataModels
RUN ["dotnet", "restore"]

WORKDIR /app/src/Services
RUN ["dotnet", "restore"]

WORKDIR /app/src/AuthenticationService
RUN ["dotnet", "build"]

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS http://*:5000

ENTRYPOINT ["dotnet", "run"]
Share:
24,583

Related videos on Youtube

Jérôme MEVEL
Author by

Jérôme MEVEL

Passionate developer. Currently starting to learn Rust on my free time.

Updated on July 09, 2022

Comments

  • Jérôme MEVEL
    Jérôme MEVEL almost 2 years

    I'm trying to create an image of my ASP.NET 5 solution which is composed of 4 projects. Here is the structure:

    • FlashTools (ASP.NET 5 class library)
    • Models (ASP.NET 5 class library)
    • QuizzCorrector (ASP.NET 5 Web Application)
    • QuizzService (ASP.NET 5 class library)

    I've a simple Dockerfile which looks like this:

    FROM microsoft/aspnet
    
    COPY . /app
    WORKDIR /app
    RUN ["kpm", "restore"]
    
    EXPOSE 5004
    ENTRYPOINT ["k", "kestrel"]
    

    But not sure where to put it. In the root folder of my solution where a global.json is or in my Web Application folder where my project.json is? Of course I've modified it depending of where this file were located.

    Anyway both seems to work because it download all the libraries I need when I run the command

    docker build -t quizzcorrector .

    My problem is at a moment docker tells me

    Unable to locate Models >= 1.0.0

    Unable to locate FlashTools >= 1.0.0

    Unable to locate QuizzService >= 1.0.0

    I've seen on this thread https://github.com/aspnet/aspnet-docker/issues/19 that in a multi-projects solution we should run the command "kpm pack" to pack my app into a deployable and runnable form.

    I could not find any examples of Dockerfiles with the kpm pack command, only the documentation: https://github.com/aspnet/Home/wiki/Package-Manager

    I've also tried of course to use ADD or COPY commands in my Dockerfile to copy the contents of my projects into the filesystem of the container, but still the same error.

    Thank's for helping me

  • Jérôme MEVEL
    Jérôme MEVEL about 8 years
    9 months later, I finally tested this method and it works great. Thanks :-)
  • zaidorx
    zaidorx about 8 years
    Thanks. It took 2 days to find this solution.
  • Reft
    Reft about 6 years
    What if you have two projects in your solution and you want them published as two different web apps. Can you make it work if your dockerfile is NOT on solution level? I need them to be in their seperate projects...
  • matt_lethargic
    matt_lethargic about 5 years
    If you look at th eDockerfile in the eShopOnContainers project they are in the porject folder, but then run with the root filesystem context so paths to other projects are relative to that context. Doing it the new way here means that you have to build the project then run docker-compose build then run docker-compose up. I used to do it the updated way here, but then changed as it got annoying.
  • Alexey Andrushkevich
    Alexey Andrushkevich about 5 years
    @matt_lethargic you are right. The Dockerfile generated by Visual Studio for a project not only runs but also builds, but there are some drawbacks too. The Dockerfile should get updated every time a new project is added to solution. Also if there are additional steps that should be ran such as npm install - you should manually update the Dockerfile to include node image to do it for you.