无法使我的Docker Desktop C# Web API环境与docker-compose一起运行

huangapple go评论72阅读模式
英文:

Can't get my Docker Desktop c# web api environment running with docker-compose

问题

我正在尝试使用Docker来更熟悉这项技术,并计划尝试使用Kubernetes。

对于我的实验,我创建了一个.NET 6 Web API项目,创建了一个名为"Holovacs.Kubernetes.Authentication.WebApi"的容器应用程序。它实际上是一个示例Web API项目。我勾选了创建容器选项,这样就生成了一个Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Holovacs.Kubernetes.Authentication.WebApi/Holovacs.Kubernetes.Authentication.WebApi.csproj", "Holovacs.Kubernetes.Authentication.WebApi/"]
RUN dotnet restore "Holovacs.Kubernetes.Authentication.WebApi/Holovacs.Kubernetes.Authentication.WebApi.csproj"
COPY . .
WORKDIR "/src/Holovacs.Kubernetes.Authentication.WebApi"
RUN dotnet build "Holovacs.Kubernetes.Authentication.WebApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Holovacs.Kubernetes.Authentication.WebApi.csproj" -c Release -o /app/publish /p:UseAppHost=false

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

我可以在Docker Desktop上以调试模式运行这个东西:无法使我的Docker Desktop C# Web API环境与docker-compose一起运行

...嘿!Swagger页面出现了,告诉我它正在按预期运行:无法使我的Docker Desktop C# Web API环境与docker-compose一起运行

接下来,我使用docker-compose构建了我的"开发环境"。我的docker-compose.yml文件如下:

version: '3'
name: devenv
services:
  authdbserver:
    image: postgres:latest
    restart: always
    ports:
      - 35432:5432
    environment:
      POSTGRES_DB: authdb
      POSTGRES_USER: authsvcuser
      POSTGRES_PASSWORD: X1Mz9YdFol0pw!t
  appdbserver:
    image: postgres:latest
    restart: always
    ports:
      - 35433:5432
    environment:
      POSTGRES_DB: appdb
      POSTGRES_USER: appsvcuser
      POSTGRES_PASSWORD: vgm3UsjF0UYxaI
  dbadmin:
    image: dpage/pgadmin4:latest
    restart: always
    ports:
      - 30080:80
    links:
      - authdbserver
      - appdbserver
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: secret
      PGADMIN_LISTEN_PORT: 80
  auditdb:
    image: mongo:latest
    restart: always
    ports:
     - 37017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: secret
  mongoadmin:
    image: mongo-express:latest
    restart: always
    ports:
      - 38081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: admin
      ME_CONFIG_MONGODB_ADMINPASSWORD: secret
      ME_CONFIG_MONGODB_URL: mongodb://admin:secret@auditdb:27017/   
  hkauthsvc:
    image: holovacskubernetesauthenticationwebapi:dev
    restart: always
    ports: 
      - 30081:80
    links:
      - authdbserver
      - appdbserver
      - auditdb
    environment:
      AUTH_SVC_CONN: Server=authdbserver;Database=authdb;User Id=authsvcuser;Password=eX1Mz9YdFol0pw!t;
      APP_SVC_CONN: Server=appdbserver;Database=appdb;User Id=appsvcuser;Password=vgm3UsjF0UYxaI;
  rabbitmq:
    image: rabbitmq:management
    ports: 
     - 35672:15672
    environment:
      RABBITMQ_DEFAULT_USER: admin@admin.com
      RABBITMQ_DEFAULT_PASS: secret

在命令行中,我进入了创建此yml文件的文件夹并运行了以下命令:

docker-compose up

请注意,devenv-hkauthsvc-1容器不断退出。我无法让它保持运行状态,并且它不会在端口30081上响应。这与它没有关联的日志。似乎没有任何错误。它只是运行,停止,然后再次运行。

我看到一些Stack Overflow文章建议我需要监听STDIN或以某种方式设置互动模式...是吗?这似乎是一个奇怪的限制,我在其他任何这些容器中都没有遇到过。我是否配置错误?这是C# / Web API、ASP.NET、Microsoft的怪癖吗?我做错了什么,或者有错误的假设吗?请提供建议。

注意:为了好玩,我尝试在Visual Studio中以Release配置运行它,并将我的镜像更改为holovacskubernetesauthenticationwebapi:latest(不确定开发和最新标签是从哪里来的),然后使用docker-compose up -d运行容器,现在当我运行容器时,它似乎不会停止...甚至有一些.NET容器的日志...但它仍然没有响应。

英文:

I'm experimenting with Docker to get more familiar with the technology, and plan on experimenting with Kubernetes as well.

For my experiment, I made a .NET 6 web api project that creates a container app named "Holovacs.Kubernetes.Authentication.WebApi". It's literally a cookie-cutter sample web api project. I checked the box to create it as a container and this makes a Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Holovacs.Kubernetes.Authentication.WebApi/Holovacs.Kubernetes.Authentication.WebApi.csproj", "Holovacs.Kubernetes.Authentication.WebApi/"]
RUN dotnet restore "Holovacs.Kubernetes.Authentication.WebApi/Holovacs.Kubernetes.Authentication.WebApi.csproj"
COPY . .
WORKDIR "/src/Holovacs.Kubernetes.Authentication.WebApi"
RUN dotnet build "Holovacs.Kubernetes.Authentication.WebApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Holovacs.Kubernetes.Authentication.WebApi.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Holovacs.Kubernetes.Authentication.WebApi.dll"]

I can run this thing in debug mode on Docker Desktop: 无法使我的Docker Desktop C# Web API环境与docker-compose一起运行

...and, hey! The Swagger page comes up, letting me know it's running as expected:
无法使我的Docker Desktop C# Web API环境与docker-compose一起运行

So next I build out my "development environment" using docker-compose. My docker-compose.yml file:

version: '3'
name: devenv
services:
authdbserver:
image: postgres:latest
restart: always
ports:
- 35432:5432
environment:
POSTGRES_DB: authdb
POSTGRES_USER: authsvcuser
POSTGRES_PASSWORD: X1Mz9YdFol0pw!t
appdbserver:
image: postgres:latest
restart: always
ports:
- 35433:5432
environment:
POSTGRES_DB: appdb
POSTGRES_USER: appsvcuser
POSTGRES_PASSWORD: vgm3UsjF0UYxaI
dbadmin:
image: dpage/pgadmin4:latest
restart: always
ports:
- 30080:80
links:
- authdbserver
- appdbserver
environment:
PGADMIN_DEFAULT_EMAIL: admin@admin.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
auditdb:
image: mongo:latest
restart: always
ports: 
- 37017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: secret
mongoadmin:
image: mongo-express:latest
restart: always
ports:
- 38081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: secret
ME_CONFIG_MONGODB_URL: mongodb://admin:secret@auditdb:27017/   
hkauthsvc:
image: holovacskubernetesauthenticationwebapi:dev
restart: always
ports: 
- 30081:80
links:
- authdbserver
- appdbserver
- auditdb
environment:
AUTH_SVC_CONN: Server=authdbserver;Database=authdb;User Id=authsvcuser;Password=eX1Mz9YdFol0pw!t;
APP_SVC_CONN: Server=appdbserver;Database=appdb;User Id=appsvcuser;Password=vgm3UsjF0UYxaI;
rabbitmq:
image: rabbitmq:management
ports: 
- 35672:15672
environment:
RABBITMQ_DEFAULT_USER: admin@admin.com
RABBITMQ_DEFAULT_PASS: secret

...on the command line, I navigate to the folder I created this yml file in and run it:

docker-compose up
[+] Running 7/0
✔ Container devenv-appdbserver-1   Created                                                                        0.0s
✔ Container devenv-auditdb-1       Created                                                                        0.0s
✔ Container devenv-rabbitmq-1      Created                                                                        0.0s
✔ Container devenv-mongoadmin-1    Created                                                                        0.0s
✔ Container devenv-authdbserver-1  Created                                                                        0.0s
✔ Container devenv-dbadmin-1       Created                                                                        0.0s
✔ Container devenv-hkauthsvc-1     Created                                                                        0.0s
(lots of startup logs)
devenv-hkauthsvc-1 exited with code 0
devenv-hkauthsvc-1 exited with code 0
devenv-hkauthsvc-1 exited with code 0
devenv-hkauthsvc-1 exited with code 0 (this keeps repeating...)

Note the devenv-hkauthsvc-1 container keeps exiting. I can't keep it running, and it won't respond on port 30081. There are no associated logs with this. There don't appear to be any errors. It just runs, stops, and runs again.

I've seen some SO articles suggesting that I need to be listening to STDIN or set this interactive somehow... do I? That seems to be an odd limitation that I haven't had to do with literally any of these other containers. Have I misconfigured something? Is this a quirk of c#/ web-api, asp.net, microsoft? Am I doing something wrong, or making some incorrect assumptions? Please advise.

NOTE: for giggles, I tried to run this in Visual Studio in Release configuration, and changed my image to holovacskubernetesauthenticationwebapi:latest (not sure where the dev and latest tags are coming from) and used docker-compose up -d and now when I run the container, it doesn't seem to stop... there's even some logs for the .net container... but it's still not responding.

答案1

得分: 0

我搞清楚了。镜像的开发版本与Docker Compose不兼容,我猜是因为它期望有一个调试器附加到它上面。当我将镜像构建为发布配置时,它不再立即退出... 但是对端点的调用仍然是空白的。我以为还有其他问题,但实际上一切都正常;在发布配置中,默认情况下禁用了重定向到Swagger(实际上是整个Swagger界面)。所以实际上在那一点上一切都正常,我只是需要访问一个真正的端点。在我与其他部分遇到的所有麻烦之后,我只是假设我搞砸了其他东西。

英文:

All right I figured this out. The dev version of the image does not play nice with the docker compose, I'm assuming because it's expecting a debugger to be attached to it. When I built the image as a release configuration, it no longer immediately exited... but calls to the endpoint were coming up blank. I thought something else was wrong, but it was actually fine; in Release config, the redirect to swagger (indeed, the entire swagger UI) is disabled by default. So it was actually working fine at that point, I just needed to hit a real endpoint. After all the headache I'd had with the other parts, I just assumed I'd screwed something else up.

huangapple
  • 本文由 发表于 2023年6月6日 06:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定