如何访问我的docker asp.net核心应用程序?

时间:2019-04-17 20:11:08

标签: asp.net docker asp.net-core docker-compose

我有一个由ASP.NET Core 2.2./PostgreSQL组成的docker解决方案,该解决方案在容器之间似乎运行良好。

我遵循了官方Quickstart: Compose and ASP.NET Core with SQL ServerQuickstart: Compose and Django的混合体,提出了将node.js添加到混合中的解决方案。

为了简洁起见,我在这篇文章中省略了实际的Dockerfile Dockerfile应该很标准:

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

RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs

RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]
EXPOSE 80/tcp
EXPOSE 5001/tcp
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh

其中entrypoint.sh实际上会启动Web服务器并像这样迁移EF迁移:

#!/bin/bash

set -e
run_cmd="dotnet run"

until dotnet ef database update; do
>&2 echo "PostgreSQL Server is starting up"
sleep 1
done

>&2 echo "PostgreSQL Server is up - executing command"
exec $run_cmd

最后我的docker-compose.yml看起来像这样:

version: '3.4'

networks:
  aspnetpostgresql:
    driver: bridge

services:
  db:
    image: postgres
    restart: always
    networks: 
      - aspnetpostgresql
  web:
    build: .
    ports:
        - "5001:5001"
        - "5000:5000"
    depends_on:
        - db
    networks: 
      - aspnetpostgresql

ASP.NET Core映像基于mcr.microsoft.com/dotnet/core/sdk:2.2。可以说,我有两个容器,my_web_app_db_1(PostgreSQL)和my_web_app_web_1(ASP.NET Core 2.2。React Web App)。我已登录my_web_app_web_1容器,并确定Web服务器正在https://localhost:5001上运行:

root@0123456780:/app# wget -p --no-check-certificate https://localhost:5001
--2019-04-17 19:46:29--  https://localhost:5001/
Resolving localhost (localhost)... 127.0.0.1, ::1
Connecting to localhost (localhost)|127.0.0.1|:5001... connected.
WARNING: The certificate of 'localhost' is not trusted.
WARNING: The certificate of 'localhost' hasn't got a known issuer.
HTTP request sent, awaiting response... 200 OK
Length: 1673 (1.6K) [text/html]
Saving to: 'localhost:5001/index.html'

localhost:5001/index.html                100%[==================================================================================>]   1.63K  --.-KB/s    in 0s      

# .... yada yada yada

FINISHED --2019-04-17 19:46:30--
Total wall clock time: 0.4s
Downloaded: 4 files, 3.2M in 0.1s (23.3 MB/s)

换句话说,在Web服务器端一切似乎都很好。

ABCDE-MacBook-Pro:MyWebApp MY_USER$ docker-compose ps
      Name                    Command               State                           Ports                         
------------------------------------------------------------------------------------------------------------------
my_web_app_db_1    docker-entrypoint.sh postgres    Up      5432/tcp                                              
my_web_app_web_1   /bin/sh -c /bin/bash ./ent ...   Up      0.0.0.0:5000->5000/tcp, 0.0.0.0:5001->5001/tcp, 80/tcp

请注意: 5001端口也向主机公开,如向右滚动可以看到。

但是,所有与https://localhost:5001连接的尝试均未成功。例如。从主机运行相同的wget操作

ABCDE-MacBook-Pro:MyWebApp MY_USER$ wget -p --no-check-certificate https://localhost:5001
--2019-04-17 23:00:05--  https://localhost:5001/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:5001... connected.
Unable to establish SSL connection.

执行curl操作同样不成功:

ABCDE-MacBook-Pro:MyWebApp MY_USER$ curl -k https://localhost:5001curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:5001 

最后,我知道aspnetcore-image本质上绑定到端口80:

  

此图像将ASPNETCORE_URLS环境变量设置为http://+:80,这意味着,如果您尚未通过Program.cs中的app.UseUrl在应用程序中显式设置URL,则您的应用程序将在容器内的80号端口。

但是,对于我来说,情况似乎并非如此,因为我能够wget到端口5001。要么这不适用于SDK映像,要么文档不是最新的。

问题是,要从主机访问https://localhost:5001,我该怎么办?

更新

我的设置目标是根据db实体框架迁移来创建和播种数据库。所结果的 Docker设置旨在供早期测试人员使用,因此不考虑大规模生产。

对于确切的数据库启动机制,我还是一无所知,但是只要开发还远远没有完成,我的要求 一直以来,该架构易于维护,并且易于部署迁移。因此,如果我可以运行

dotnet ef database update

命令,因为这与我在开发环境中所做的完全相同。在进一步研究了对项目进行docker化的最初尝试之后,我决定退后一步并从ASP.NET Core项目本身运行实际的迁移。这也许不是最干净的解决方案,但它解决了我的实际情况:即使用docker安装程序复制我的开发环境。对于那些感兴趣的人,我的Dockerfile现在看起来像这样:

FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app

RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=build-env /app/out . 

EXPOSE 80/tcp

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

DbContext看起来像这样:

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
        Database.Migrate();
    }

    // ...

}

docker-compose.yml与以前几乎相同(尽管我已经删除了网络配置)。现在,我可以从主机访问http://localhost:8000

1 个答案:

答案 0 :(得分:0)

由于您没有发布Dockerfile,所以我不确定,但是您的Dockerfile中是否有“ EXPOSE 5001”?也许,如果缺少它,则会导致此行为。