tonysneed / Demo.AspNetCore-Nginx-Ssl

How to set up ASP.NET Core Web API using Nginx as reverse proxy for SSL termination
59 stars 28 forks source link

Build with 2 projects #1

Open lolik20 opened 2 years ago

lolik20 commented 2 years ago

How i can build container with 2 projects?

FROM mcr.microsoft.com/dotnet/core/aspnet:6.0-alpine AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:6.0-alpine AS build
WORKDIR /src
COPY ["RentServiceApp.csproj", "./"]
COPY ["RentServiceCommon.csproj", "./"]

RUN dotnet restore "./RentServiceApp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "RentServiceApp.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT ["dotnet", "RentServiceApp.csproj"]
Building api
Step 1/17 : FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
 ---> e3f3ae957ae9
Step 2/17 : WORKDIR /app
 ---> Using cache
 ---> e075be1c0883

Step 3/17 : FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
 ---> 1f349786b71f
Step 4/17 : WORKDIR /src
 ---> Using cache
 ---> 8c4e004fccc2
Step 5/17 : COPY ["RentServiceApp.csproj", "./"]
 ---> Using cache
 ---> e44973d1a797
Step 6/17 : COPY ["RentServiceCommon.csproj", "./"]
ERROR: Service 'api' failed to build: COPY failed: file not found in build context or excluded by .dockerignore: stat RentServiceCommon.csproj: file does not exist

image

lolik20 commented 2 years ago
version: "3.7"

services:
  reverseproxy:
    image: lolik20/proxy
    build:
      context: ./Nginx
      dockerfile: Dockerfile
    ports:
      - "80:80"
      - "443:443"
    restart: always

  api:
    image: lolik20/api
    depends_on:
      - reverseproxy
    build:
      context: ./RentServiceApp
      dockerfile: Dockerfile
    expose:
      - "5000"
    restart: always
soondook commented 1 year ago

Hi guys! maybe my solution will help someone. I still did not understand what the reason was, but by experiment, I got a working configuration. My problem was that the assembly of two images (via compose) worked correctly for me only when I launched the solution through Visual Studio. When I tried to build via "docker-compose -f docker-compose.yml up -d", the reverse proxy did not work correctly. Below I give examples of my working configs: P.S. You also need to take into account that the basic images (which are referenced in the example) have changed a long time ago: for build: microsoft.com/dotnet/core/sdk:3.1-buster AS build

soondook commented 1 year ago

nginx.conf

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream web-api {
        server helloaspnetcore3.api:80;
    }

    server {
        listen 80;
        server_name localhost;

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 443 ssl;
        server_name localhost;

        ssl_certificate /etc/ssl/certs/localhost.crt;
        ssl_certificate_key /etc/ssl/private/localhost.key;

        location / {
            proxy_pass         http://web-api;
            proxy_redirect     off;
            proxy_http_version 1.1;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}
soondook commented 1 year ago

docker-compose.yml

version: '3.4'

services:

  helloaspnetcore3.api:
    depends_on:
      - proxy
    image: ${DOCKER_REGISTRY-}helloaspnetcore3api
    build:
      context: .
      dockerfile: HelloAspNetCore3.Api/Dockerfile
    ports:
      - 5000:80

  proxy:
    build:
      context: ./Nginx
      dockerfile: Nginx.Dockerfile
    ports:
      - "80:80"
      - "443:443"
    restart: always

Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

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

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