Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker With a .Net Core API

Okay I am trying to get a docker-compose build to work with a .net core api. the folder structure is as follows:

  • backend
    • db
    • services
      • src
        • api
          • api.csproj
      • test
      • services.sln
      • Dockerfile
  • client
  • docker-compose.yml

My root docker-compose.yml file is as follows:

version: '3.3'

services:
  plan-analytics-postgres:
    container_name: plan-analytics-postgres
    restart: always
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
      PGPASSWORD: admin
    build:
      context: ./backend/db
    ports:
      - '5432:5432'
    networks:
      - 'backend-net'

  plan-analytics-services:
    container_name: plan-analytics-services
    build:
      context: ./backend/services
    ports:
      - '9001:9001'
    volumes:
      - './backend/services/api:/services'
    environment:
      NODE_ENV: 'dev'
      port: '9001'
      BABEL_DISABLE_CACHE: 1
      DATABASE_URL: 'postgres://admin:admin@plan-analytics-postgres:5432/plan-analytics'
      DATABASE_TEST_URL: 'postgres://admin:admin@plan-analytics-postgres:5432/plan-analytics-integration'
    networks:
      - 'backend-net'
      - 'frontend-net'

  plan-analytics-web:
    container_name: plan-analytics-web
    build:
      context: ./client
    ports:
      - '3000:3000'
      - '35729:35729'
    environment:
      NODE_ENV: 'development'
      CHOKIDAR_USEPOLLING: 1
    volumes:
      - './client:/src/app'
      - '/src/app/node_modules'
    depends_on:
      - 'plan-analytics-postgres'
      - 'plan-analytics-services'
    networks:
      - 'frontend-net'

networks:
  frontend-net:
    driver: bridge
  backend-net:
    driver: bridge
  message-net:
    driver: bridge

The Dockerfiles for the client and the db work as expected, but I haven't had any luck getting my api up and running. I have a similar setup with a node api and it works wonderfully. I would love some help figuring out what is wrong with the Dockerfile that is to run the C# API. Here is its content:

FROM microsoft/dotnet:2.1-sdk AS builder

RUN mkdir -p src/services
WORKDIR /services

# Add ./NuGet.config
COPY ./*.sln  ./



COPY src/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p src/${file%.*}/ && mv $file src/${file%.*}/; done

# COPY test/*/*.csproj ./
# RUN for file in $(ls *.csproj); do mkdir -p test/${file%.*}/ && mv $file test/${file%.*}/; done

RUN dotnet restore

COPY ./src/api ./src/api
# COPY ./test ./test
RUN dotnet build -c Release --no-restore


# FROM build as publish
# WORKDIR /src/services/api
# RUN dotnet publish -c Release -o out

# Do the following when we add NuGet packages
# RUN dotnet build -c Release --no-restore
RUN dotnet publish "./src/api/api.csproj" -c Release -o "../../dist" --no-restore


# Build runtime image
FROM microsoft/dotnet:2.1.0
WORKDIR /services
COPY --from=build /services/dist .
ENTRYPOINT ["dotnet", "api.dll"]

This is the error that I am getting:

Step 9/14 : RUN dotnet build -c Release --no-restore
 ---> Running in 47ffd4456e9d
Microsoft (R) Build Engine version 15.8.166+gd4e8d81a88 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/share/dotnet/sdk/2.1.402/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1004: Assets file '/Users/bryce/Documents/stacks/react-redux-to-net-core-postgres/backend/services/src/api/obj/project.assets.json' not found. Run a NuGet package restore to generate this file. [/services/src/api/api.csproj]

Build FAILED.

/usr/share/dotnet/sdk/2.1.402/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1004: Assets file '/Users/bryce/Documents/stacks/react-redux-to-net-core-postgres/backend/services/src/api/obj/project.assets.json' not found. Run a NuGet package restore to generate this file. [/services/src/api/api.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.54
ERROR: Service 'plan-analytics-services' failed to build: The command '/bin/sh -c dotnet build -c Release --no-restore' returned a non-zero code: 1
like image 984
BryceHayden Avatar asked Nov 08 '22 01:11

BryceHayden


1 Answers

So I was able to get the above working...it partly had to do with the --no-restore, and there's a bug associated with it. To get it to work I did the following:

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /api

COPY ./*.sln ./
# COPY ./NuGet.config /root/.nuget/NuGet/

COPY src/*/*.csproj ./
RUN for file in $(ls *.csproj); do mkdir -p src/${file%.*}/ && mv $file src/${file%.*}/; done

# COPY test/*/*.csproj ./
# RUN for file in $(ls *.csproj); do mkdir -p test/${file%.*}/ && mv $file test/${file%.*}/; done

RUN dotnet restore

COPY . .

RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
WORKDIR /api
COPY --from=build /api/src/api/out .
ENTRYPOINT ["dotnet", "api.dll"]

I left the comments in the above code, in case you need an example for tests.

like image 111
BryceHayden Avatar answered Nov 14 '22 22:11

BryceHayden