无法通过HTTPS访问Docker容器

时间:2019-10-15 20:38:36

标签: c# docker docker-compose dockerfile docker-machine

我有一个用ASP.NET Core 3.0编写的小应用程序。我正在使用docker和Kestrel在Ubuntu 18.04计算机上发布它。当我尝试访问它时,返回“无法访问此站点”。...

我已经创建了已经自签名的证书,并在docker配置中进行了描述

这是我的docker-compose.yml文件:

version: "3"
services:
    web:
        working_dir: /app
        build: .
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
          - ASPNETCORE_URLS=https://+;http://+
          - ASPNETCORE_HTTPS_PORT=8001
          - ASPNETCORE_Kestrel__Certificates__Default__Password=<My-password>
          - ASPNETCORE_Kestrel__Certificates__Default__Path=/https/<my-certificate>.pfx
        ports:
            - 80:80
            - 433:433
        volumes:
            - ${HOME}/.aspnet/https:/app/https/
        depends_on:
            - db
    db:
        image: "mcr.microsoft.com/mssql/server"
        environment:
            SA_PASSWORD: "<My-passowrd>"
            ACCEPT_EULA: "Y"

我的Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["MyApp.csproj", ""]
RUN dotnet restore "./MyApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyApp.csproj" -c Release -o /app/publish

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

还有我的applicationsettings.json文件:

...
"AllowedHosts": "*",
    "Kestrel":{
        "Certificates":{
            "Default":{
                "Path":     "/app/https/<My-certificate>t.pfx",
                "Password": "<My-password>"
            }
        }
    }
}

我检查了一些容器的错误,但没有错误。我也尝试访问http://mysitecom:8000https://mysitecom:8001,但是没有任何运气。 无法找出问题所在,没有引发任何错误。

1 个答案:

答案 0 :(得分:0)

您正在设置环境变量,以在此行的docker-compose.yml中通过端口8001托管https:

<div>
  {users.map(user => {
    return (user !== undefined && user !== null)
    ? <AddUser user={user} key={user.id} classes={classes} state={fleetCustomerState} dispatch={dispatch} />
    : null
  })}
</div>

但是,您还从撰写文件的端口443:443映射。

- ASPNETCORE_HTTPS_PORT=8001

您可能需要将其更改为ports: - 80:80 - 433:433 。如果您想改为通过端口8001连接到https,请尝试在端口设置下的443:8001