简体   繁体   English

asp.net core 2.0 - 多项目解决方案 docker 文件

[英]asp.net core 2.0 - multiple projects solution docker file

[asp.net core 2.0 and docker for linux] [asp.net core 2.0 和 docker for linux]

I am completely new to docker and trying to figure out how to use docker in a case where i have a solution with 2 projects.我对 docker 完全陌生,并试图弄清楚如何在我有 2 个项目的解决方案的情况下使用 docker。 All tutorials i've seen show with single project.我见过的所有教程都显示了单个项目。

So, if someone could show step by step solution i would really appreciate it.因此,如果有人可以逐步展示解决方案,我将不胜感激。

i have a solution structure like:我有一个解决方案结构,如:

Solution.sln
|______WebAPIProject.csproj
|______ClassLibraryProject.csproj

In Visual studio, i've added docker support to solution and got these files:在 Visual Studio 中,我为解决方案添加了 docker 支持并获得了这些文件:

Under WebAPIProject, it created this Docker file :在 WebAPIProject 下,它创建了这个Docker 文件

FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "WebAPIProject.dll"]

then, under separate docker-compose 'project' i've got:然后,在单独的 docker-compose“项目”下,我得到了:

docker-compose.ci.build.yml docker-compose.ci.build.yml

version: '3'

services:
  ci-build:
    image: microsoft/aspnetcore-build:1.0-2.0
    volumes:
      - .:/src
    working_dir: /src
    command: /bin/bash -c "dotnet restore ./Solution.sln && dotnet publish ./Solution.sln -c Release -o ./obj/Docker/publish"

and docker.compose.yml filedocker.compose.yml文件

version: '3'

services:
  WebAPIProject:
    image: WebAPIProject
    build:
      context: ./WebAPIProject
      dockerfile: Dockerfile

I am sure its something trivial with paths but i am just pretty lost with it all so if someone could shed a bit of a light on it?我敢肯定它的路径微不足道,但我对这一切都感到很迷茫,所以如果有人能对它有所了解吗?

I finally found a way how to built solutions with docker. 我终于找到了一种如何使用docker构建解决方案的方法。

IMPORTANT: For this to work, you have to put the Dockerfile file into the same location where the solution file is 重要信息:为此,您必须将Dockerfile文件放在解决方案文件所在的位置

I've just created a docker file with the following content: 我刚刚创建了一个包含以下内容的docker文件:

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

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY Solution.sln ./
COPY ClassLibraryProject/*.csproj ./ClassLibraryProject/
COPY WebAPIProject/*.csproj ./WebAPIProject/

RUN dotnet restore
COPY . .
WORKDIR /src/ClassLibraryProject
RUN dotnet build -c Release -o /app

WORKDIR /src/WebAPIProject
RUN dotnet build -c Release -o /app

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

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

Note that I think you may have to respect project build dependencies but I don't know really. 请注意,我认为您可能必须尊重项目构建依赖项,但我真的不知道。

Sample call to build: 示例调用构建:

sudo docker build --no-cache -t webapi:dev .

Sample call to run: 要运行的示例调用:

sudo docker run -d=false -p 8080:80 --name webapi webapi:dev

Hope that helps. 希望有所帮助。

While I understand that dotnet core isn't specific to Windows and not everyone is going to be using Visual Studio, there's this neat feature that Microsoft has included inside Visual Studio (Tried it in 2017). 虽然我知道dotnet核心不是特定于Windows并且不是每个人都会使用Visual Studio,但微软已将这一功能包含在Visual Studio中(2017年试用)。

  1. Right click on the Web Project. 右键单击Web项目。
  2. Select Add. 选择添加。
  3. Then "Docker Support". 然后“Docker支持”。 This will automatically fetch multiple projects into the container. 这将自动将多个项目提取到容器中。

If you have multiple Web Projects, repeat the step individually for each. 如果您有多个Web项目,请分别为每个项目重复该步骤。

Here's the reference: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/visual-studio-tools-for-docker?view=aspnetcore-2.1#existing-app 以下是参考: https//docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/visual-studio-tools-for-docker?view=aspnetcore-2.1#existing-app

If you happen to run with any issues when running the Docker container, try deselecting Hyper-V Services in "Windows Features" (Search for Windows Features in Start Menu), selecting again, and then restarting your computer. 如果您在运行Docker容器时碰巧遇到任何问题,请尝试在“Windows功能”(在“开始”菜单中搜索Windows Features中取消选择Hyper-V Services ,再次选择,然后重新启动计算机。 Check here . 点击这里

在此输入图像描述

This links to an amazing post about how to tackle this issue. 这链接到一篇关于如何解决这个问题的惊人帖子

He mentions putting all your code in a src directory. 他提到将所有代码放在src目录中。 I didn't so that, and this is the file i came up with: (The key is the for file part. I'm sure it's not the best dockerfile otherwise; tips are welcome.) 我不是这样,这是我提出的文件:(关键是for file部分。我确定它不是最好的dockerfile否则;提示是受欢迎的。)

FROM microsoft/dotnet:2.2-aspnetcore-runtime-stretch-slim AS base
WORKDIR /app
EXPOSE 5000

FROM microsoft/dotnet:2.2-sdk-stretch AS build
WORKDIR /src

COPY ./*.sln ./

COPY */*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*} && mv $file ${file%.*}; done
RUN dotnet restore

COPY . ./
RUN dotnet build -c Release -o /app

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app .

ENV ASPNETCORE_URLS="http://*:5000"
ENTRYPOINT ["dotnet", "PersonalSiteApi.dll"]

I had the same problem and all other solutions didn't fit my so I worked out my own. 我有同样的问题,所有其他解决方案都不适合我,所以我自己制定了。 I feel that bellow steps are legible, but if you're new to Docker, I explained it line by line in my blog post dedicated to new Docker users (so you could understand precisely what is going on in this Dockerfile ). 我觉得下面的步骤很清晰,但是如果你是Docker的新手,我会在专门讨论新Docker用户的博客文章中逐行解释它(这样你就可以准确理解这个Dockerfile中发生了什么)。

  1. Keep Dockerfile in project directory (which is, in my point of view, better than keeping it next to solution file, because you can have more than one Docker image per solution). Dockerfile保存在项目目录中 (在我看来,这比将其保留在解决方案文件旁边要好,因为每个解决方案可以有多个Docker镜像)。

  2. Configure Dockerfile as follows: 配置Dockerfile如下:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app

COPY . ./
RUN dotnet publish PROJECT_NAME -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/PROJECT_NAME/out .

ENTRYPOINT ["dotnet", "PROJECT_NAME.dll"]
  1. Move .dockerignore file to solution directory - that's required, because Docker CLI takes .dockerignore file only from root directory of the build context (as documentation says ), but it's also convenient, because you have one, common set of ignore rules for all projects (similar to .gitignore file), which is easier to maintain. .dockerignore文件移动到解决方案目录 - 这是必需的,因为Docker CLI 仅从构建上下文的根目录获取.dockerignore文件(如文档所述 ),但它也很方便,因为您对所有项目都有一组通用的忽略规则(类似于.gitignore文件),更容易维护。

  2. Run build command from solution directory , pointing to Dockerfile in project directory, and setting current directory ( . ) as build context (to have access to all projects): 从解决方案目录运行build命令 ,指向项目目录中的Dockerfile ,并将当前目录( )设置为构建上下文(以访问所有项目):

docker build -f PROJECT_DIRECTORY/Dockerfile -t IMAGE_NAME .
  1. Run container as usual, for example: 像往常一样运行容器,例如:
docker run -d -p 8080:80 --name my-app-name IMAGE_NAME
FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

RUN mkdir /output

# Copy project and publish

COPY . /app

WORKDIR /app/YourProjectName
RUN dotnet publish --configuration Debug --output /output

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime

ENV ASPNETCORE_URLS http://*:5001

WORKDIR /app

COPY --from=build-env /output .
EXPOSE 5001

ENTRYPOINT ["dotnet", "YourProjectName.dll"]

This is a Docker configuration that worked for me. 这是一个适合我的Docker配置。 This was an ASP.NET Core 2 WebApi project with many child project references. 这是一个ASP.NET Core 2 WebApi项目,包含许多子项目引用。

Then run it using  docker run -d -p 8080:5001 --name some-name yourpojectname

Hope this helpes someone. 希望这有助于某人。

As of 2022 docker-compose 3.9截至 2022 年 docker-compose 3.9

After few attempts, I managed to do it this way:经过几次尝试,我设法这样做:

Project hierarchy:项目层次:

MoulaTracker/
|- MoulaTracker.Core/    # <- PROJECT 1
|- MoulaTracker.Api/     # <- PROJECT 2
|- Dockerfile.Core
|- Dockerfile.Api
|- docker-compose.yml
|- MoulaTracker.sln

The project MoulaTracker.Api is dependent of MoulaTracker.Core MoulaTracker.Api项目依赖于MoulaTracker.Core

docker-compose.yml : docker-compose.yml

version: "3.9"

services:
  moula-tracker-core:
    build:
      context: .
      dockerfile: ./Dockerfile.Core
  moula-tracker-api:
    build:
      context: .
      dockerfile: ./Dockerfile.Api

Dockerfile.Api : Dockerfile.Api

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.409 AS build-env

# Copy csproj and restore as distinct layers
WORKDIR /MoulaTracker.Api
COPY ./MoulaTracker.Api .
COPY ./MoulaTracker.Core ../MoulaTracker.Core
RUN dotnet restore

RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.0 AS runtime

WORKDIR /MoulaTracker.Api
COPY --from=build-env /MoulaTracker.Api/ ./

ENTRYPOINT ["dotnet", "out/MoulaTracker.Api.dll"]
  1. Install docker desktop as administrator以管理员身份安装docker桌面
  2. Run Docker desktop as administrator以管理员身份运行Docker桌面
  3. Move your sln file to projects folders (inside Code folder)将您的 sln 文件移动到项目文件夹(在代码文件夹内)
  4. right click to project go to Add option -> Docker support and select linux as OS and click OK右键单击项目 go 添加选项 -> Docker 支持和 select linux 作为操作系统,然后单击确定
  5. it will generated DockerFile它将生成 DockerFile
  6. move DockerFile to projects folder(inside Code folder) included sln file level将 DockerFile 移动到项目文件夹(在代码文件夹内)包括 sln 文件级别
  7. open cmd as administrator and go to DockerFile path level以管理员身份打开 cmd 和 go 到 DockerFile 路径级别
  8. run this --> docker build -t imagename.运行这个 --> docker build -t imagename.
  9. after it creates the image file创建图像文件后
  10. run this --> docker run -d -p 8080:83 --name dockercontainername imagename运行这个 --> docker run -d -p 8080:83 --name dockercontainername imagename
  11. image running in container在容器中运行的镜像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM