Articles .NET Core 3.1, Docker, PostgreSQL, Swagger, C# by Paul Kukiel

emailx45

Местный
Регистрация
5 Май 2008
Сообщения
3,571
Реакции
2,439
Credits
574
.NET Core 3.1, Docker, PostgreSQL, Swagger, C# by Paul Kukiel
27/May/2020
[SHOWTOGROUPS=4,20]
.NET Core 3.1, Docker, postgres, swagger quick start

sources: https://dumpz.ws/resources/net-core-3-1-docker-postgresql-swagger-c.114/

Required
Recommended
Overview and Introduction
Have you been tempted by .NET Core, but not sure where to start?

Well, you’re not the only one, in fact it's actually difficult to find a comprehensive story on "Your first .NET Core API" let alone connect it to Postgres.

This quick start aims to provide you a template that can literally be run with 2 commands and provides the opportunity to continue to build out a local application.

The QuickStart provides an API that includes:
  • GETs
  • POSTs
  • Connectivity to PostgreSQL
  • Swagger integration
This demo app will run completely in docker. Simply build the container:
Код:
docker-compose build

Build will compile the application and move out entrypoint shell script ready for execution. This step is important, we want the DB migrations to run when the container starts but before the applications tries to connect. The migrations will create the tables in the database.

Docker file:
Код:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
COPY . /app
WORKDIR /app
RUN dotnet tool install --global dotnet-ef
RUN dotnet restore
RUN dotnet build
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
entrypoint.sh

Hide   Copy Code
#!/bin/bash

set -e
run_cmd="dotnet run --no-build --urls http://0.0.0.0:5000 -v d"

export PATH="$PATH:/root/.dotnet/tools"

until dotnet ef database update; do
    >&2 echo "Migrations executing"
    sleep 1
done

>&2 echo "DB Migrations complete, starting app."
>&2 echo "Running': $run_cmd"
exec $run_cmd

Then start:
Код:
docker-compose up

This will start the two containers. PostgreSQL is completely standard, we simply pass in an environment value which is the password for the database (postgres), the small piece to add here is that init.sql script is also mounted between the host (local machine) and the container. This script is copied to a location that is executed on the start-up of the database container. It is very simple. It will drop the "Posts" database if it exists and then create it again, so we start fresh. This configuration is located in the "docker-compose.yml" file.
Код:
docker-compose.yml:
version: '3'
services:
  web:
    container_name: dotnetCore31
    build: .
    ports:
        - "5005:5000"
    depends_on:
        - database
  database:
    container_name: database
    image: postgres:latest
    ports: 
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=password
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql

Once the app starts, you can interact at Для просмотра ссылки Войди или Зарегистрируйся to test out the API.

You can connect to the database as well by connecting to: localhost port 5432 with your favourite tool. I recommend and use DBBeaver: settings are:
Код:
host:  localhost
port: 5432
database: postgres
user: postgres
password: postgres

pg-1.png


pg-2.png


If you wish to run the app locally, add a lost host entry. The containers internal DNS knows to resolve database to the postgres (name defined in docker-compse.yml file line 10) container so adding this alias to your hosts file will allow your project in VSCode or Visual Studio to execute and connect to the database.

Or change the connection string in appsettings.Development.json to localhost (from database).

Hide Copy Code
#windows: c:\windows\system32\drivers\etc\hosts
Linux/MacOS: /etc/hosts

127.0.0.1 database
I prefer to use Visual Studio. To run the application locally, simply double click the pgapp.sln file and click Run. Your app will compile and run locally and be alliable on port 5000.

Local url: Для просмотра ссылки Войди или Зарегистрируйся

overview.png

License
This article, along with any associated source code and files, is licensed under The MIT License


[/SHOWTOGROUPS]