无法在docker linux中运行Asp.Net Core

时间:2019-05-20 14:05:44

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

我正在尝试使用docker-compose在docker内部运行ASP.Net Core。以下是dockerfile的内容:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443


FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["SchoolContact/SchoolContact.csproj", "SchoolContact/"]
RUN dotnet restore "SchoolContact/SchoolContact.csproj"
COPY . .
WORKDIR "/src/SchoolContact"
RUN dotnet build "SchoolContact.csproj" -c Release -o /app

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

FROM base AS final
WORKDIR /app
ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000 80
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SchoolContact.dll"]

这是docker-compose.yml的内容:

version: '3'

services:
  api:
        image: schoolcontact
        build:
          context: .
          dockerfile: SchoolContact/Dockerfile
        links:
          - db:db
        ports:
          - '5000:5000'
        restart: always
        networks:
          - school
  db:
      image: mysql:5.7
      volumes:
        - ./db:/var/lib/mysql
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 123456
        MYSQL_DATABASE: Test
        MYSQL_USER: admin
        MYSQL_PASSWORD: 123456
      networks:
        - school

  phpmyadmin:
        depends_on:
          - db
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
          - '8080:80'
        environment:
          PMA_HOST: db
          MYSQL_ROOT_PASSWORD: 123456 
        networks:
          - school

  reverse-proxy:
        container_name: proxy
        image: nginx
        depends_on:
          - api
        ports:
          - "9090:8080"
        volumes:
          - ./proxy.conf:/etc/nginx/conf.d/default.conf
        networks:
          - school

networks:
  school:
volumes:
    db:
       driver: "local"

proxy.conf

server {
    listen 8080;

    location / {
      proxy_pass http://api:5000;
    }
}

在这里发布并寻求帮助之前,我已经尝试了太多次了,我尝试在没有nginx的情况下运行,是否公开端口,等等。但是我仍然无法访问该应用程序。我的最终目的是将应用程序托管在docker中,并可以通过URL http://localhost:5000访问它。我不知道我在哪里错了或者错过了其他东西。我真的需要帮助。感谢您的阅读。

1 个答案:

答案 0 :(得分:0)

这是我的工作方式,经过几周的研究(针对那些问题相同的人),将这些行添加到 Main 方法中。另外, dockerfile docker-compose.yml 应该如下所示:

public static void Main(string[] args)
{
    //CreateWebHostBuilder(args).Build().Run();
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseUrls("http://*:9001")
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

docker-compose.yml

services:
  api:
        image: schoolcontact
        build:
          context: .
          dockerfile: SchoolContact/Dockerfile
        links:
          - db:db
        ports:
          - 9021:9001
        restart: always
        networks:
          - school

dockerfile

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
#EXPOSE 443


FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["SchoolContact/SchoolContact.csproj", "SchoolContact/"]
RUN dotnet restore "SchoolContact/SchoolContact.csproj"
COPY . .
WORKDIR "/src/SchoolContact"
RUN dotnet build "SchoolContact.csproj" -c Release -o /app

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

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