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:
GET
s POST
s - 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
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 http://localhost:5005/swagger 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
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).
#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: http://localhost:5000/swagger