简体   繁体   English

如何在VS2017中为dotnet核心Web API应用设置docker项目(dcproj)

[英]How to setup a docker project (dcproj) for a dotnet core Web API app in VS2017

I'm using dotnetcore to develop applications and one thing I noticed when creating a aspnetcore (2.1) web api was the option to "Enable Docker" on the project. 我使用dotnetcore开发应用程序,创建aspnetcore(2.1)Web api时注意到的一件事是该项目上的“启用Docker”选项。 See below: 见下文:

在此处输入图片说明

This was really great for allowing me to run my web application against my local docker for debugging! 对于允许我在本地docker上运行Web应用程序进行调试,这确实非常棒! :-DI see that I have a new .dcproj file for the docker project, a docker file in my web api and a yml file in the docker project. :-DI看到我为docker项目有一个新的.dcproj文件,在我的Web api中有一个docker文件,在docker项目中有一个yml文件。 Everything I needed to run in debug. 我需要在调试中运行的所有内容。

Now I'm creating a dotnetcore console application (again core 2.1 is the version I'm using). 现在,我正在创建一个dotnetcore控制台应用程序(同样,我正在使用core 2.1版本)。 Unfortunately, I dont have an "Enable Docker Support" option for console apps and would like to run my console app in my local docker instance. 不幸的是,我没有控制台应用程序的“启用Docker支持”选项,并且想在本地Docker实例中运行控制台应用程序。 Visual studio version I'm running is as follows: 我正在运行的Visual Studio版本如下:

在此处输入图片说明

I had a look for and can't seem to find the docker project as a project type template. 我寻找了并且似乎找不到docker项目作为项目类型模板。 So I resorted to copying over the docker project from my web app and modifying it to run my console app... needless to say I'm not able to get this running. 因此,我诉诸于从Web应用程序复制docker项目并对其进行修改以运行我的控制台应用程序...不用说,我无法使其运行。 I get the following error when I try to launch the docker compose: 尝试启动docker compose时出现以下错误:

Failed to launch debug adapter 无法启动调试适配器

在此处输入图片说明

My docker file is identical to the one from the web project (that works), other than my project and dll names (these are set correctly). 除了我的项目和dll名称(正确设置)之外,我的docker文件与网络项目(有效)中的文件相同。

My yml file is the same also, other than the names being changed also. 我的yml文件也一样,除了名称也被更改了。 Not sure what I'm doing wrong - has anyone managed to achieve this? 不知道我在做什么错-是否有人设法做到这一点? I ultimately want to debug my console application when it's running in docker through Visual Studio debug. 我最终想通过Visual Studio调试在控制台应用程序在docker中运行时对其进行调试。

Thanks for any pointers in advance!! 感谢您提前提出任何建议!

For verbosity, here's my Dockerfile : 详细来说,这是我的Dockerfile

FROM microsoft/dotnet:2.1.2-runtime-alpine3.7 AS base
WORKDIR /app
EXPOSE 52649
EXPOSE 44331

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY AI.EmitterJob.csproj AI.EmitterJob/
RUN dotnet restore AI.EmitterJob/AI.EmitterJob.csproj
COPY . .
WORKDIR /src/AI.EmitterJob
RUN dotnet build AI.EmitterJob.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish AI.EmitterJob.csproj -c Release -o /app

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

And docker-compose.yml file: docker-compose.yml文件:

version: '3.4'

services:
  ai.emitterjob:
    image: ${DOCKER_REGISTRY}ai_emitter
    build:
      context: .
      dockerfile: ../AI.EmitterJob/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
      - ASPNETCORE_HTTPS_PORT=44331
    ports:
      - "52649:80"
      - "44331:443"
    volumes:
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro

Note - I know I dont need to expose ports or use volumes here (or set env vars) for the console app, but I wanted to keep changes from the web docker file that works, to a minimum. 注-我知道我不需要在此处为​​控制台应用程序公开端口或使用卷(或设置env vars),但我想将对Web docker文件的更改保持在最小范围内。

Just in case anyone else wants to try this, I finally got it working! 以防万一其他人想尝试一下,我终于让它工作了!

When I copied over the docker file, the only line I changed was from this: 当我在docker文件上复制时,我唯一更改的行是:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base

To this: 对此:

FROM microsoft/dotnet:2.1.2-runtime-alpine3.7 AS base

Because I'd used that alpine build before as a base image. 因为我以前曾使用该高山建筑作为基本图像。 Reverting back to the aspnetcore base image made this work fine. 返回到aspnetcore基本映像可使此工作正常进行。

Obviously, this isn't ideal for proper deployments and I need to find a sufficient replacement for the aspnetcore base image that will have the dotnetcore runtime installed. 显然,这对于正确的部署不是理想的,我需要找到一个足够的替代品来安装将安装dotnetcore运行时的aspnetcore基本映像。

I'm happy I can debug my console app through docker now! 我很高兴现在可以通过docker调试控制台应用程序! :-D :-D

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

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