从多项目点网核心解决方案构建 Docker 镜像 [英] Building a Docker image from a multi project dot net core solution

查看:23
本文介绍了从多项目点网核心解决方案构建 Docker 镜像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从包含两个项目的 Visual Studio 解决方案构建 docker 映像:

I am trying to build a docker image from a visual studio solution that consists of two projects:

  • 一个项目包含通用代码(http 方法、日志记录等)
  • 另一个是业务逻辑和应用

我可以在 Visual Studio 中构建和运行,它会生成一个 Docker 映像,我可以将其部署到我们的测试环境中.但是,在测试环境中发送到 API 的任何 POST 都会返回 500 错误.我可以在错误响应中看到类似的引用:'/Users/Tim/Documents/Dotnet Code/dotnetcore-app/App/MyClass.cs',这让我怀疑有什么问题图片 - 该路径在我的笔记本电脑上.

I can build and run in Visual Studio, which produces a Docker image that I am able to deploy to our test environment. However, any POSTs sent to the API in the test environment return a 500 error. I can see references like: '/Users/Tim/Documents/Dotnet Code/dotnetcore-app/App/MyClass.cs' in the error response, which leads me to suspect there is something up with the image - that path is on my laptop.

希望其他人可能遇到过类似的事情,或者可以提供一些关于将这样的多项目解决方案构建到 Docker 容器中的指示.

Hoping someone else may have come across something similar, or could provide some pointers about building a multi project solution like this into a Docker container.

免责声明:这是我第一次使用 dotnet core 和 Visual Studio - 我有一些使用 docker 的经验.

Disclaimer: this is my first time working with dotnet core and visual studio - I have some experience with docker.

推荐答案

如果您使用 Visual Studio 中的标准"Dockerfile 模板,则在本地 PC(可能是 Windows)上构建通用"项目时可能会遇到问题以及在 docker 容器(可能是 Linux)中构建的主"项目.主"dll 引用了 Windows 构建的 dll,因此存在问题.

If you use "standard" Dockerfile template from Visual Studio, you might have issue with having your "common" project being built on your local PC (probably windows) and the "main" project being built in docker container (probably Linux). the "main" dll is referencing windows-built dll, hence the issues.

确保您不仅复制主项目,还复制公共项目,这样 dotnet 也可以在 docker 中构建它.

Make sure you are copying not only main project, but also the common project, so dotnet can build it in docker also.

试试这个 dockerfile:

Try out this dockerfile:

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ASPNETCORE_URLS http://*:5000
EXPOSE 5000

FROM microsoft/aspnetcore-build:2.0 AS builder
ARG Configuration=Release
WORKDIR /src
COPY *.sln ./
COPY Project.Main/Project.Main.csproj Project.Main/
COPY Project.Common/Project.Common.csproj Project.Common/
RUN dotnet restore
COPY . .
WORKDIR /src/ProjectMain
RUN dotnet build -c $Configuration -o /app

FROM builder AS publish
ARG Configuration=Release
RUN dotnet publish -c $Configuration -o /app

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

虽然是构建Main"项目,但它是在sln级别执行的.

Although is to build the "Main" project, it's executed on the sln level.

这篇关于从多项目点网核心解决方案构建 Docker 镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆