English
A local database in Docker
Step 1. Describe the database container
Why. Chatex stores its data in PostgreSQL 18. We could install PostgreSQL into the system, but a container is better for development:
- it starts and stops with one command and does not run as a system service all the time;
- every developer gets exactly the same version and settings;
- it can be deleted together with its data and recreated in seconds when you want a clean start;
- the tests (topic 6) use the same image, so development and tests behave the same.
Docker Compose describes containers in a YAML file, so the settings live in the repository instead of in a long docker run command that everyone types differently.
Do. Create compose.yaml in the repository root:
yaml
# Local PostgreSQL for development: docker compose up -d
# Matches the connection string in Source/Chatex.Api/appsettings.Development.json and the dotnet-ef default.
name: chatex
services:
postgres:
image: postgres:18-alpine
environment:
POSTGRES_DB: chatex
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- postgres-data:/var/lib/postgresql
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d chatex"]
interval: 5s
timeout: 3s
retries: 10
restart: unless-stopped
volumes:
postgres-data:Line by line:
name: chatex— the project name; Docker prefixes the container and the volume with it (chatex-postgres-1,chatex_postgres-data).image: postgres:18-alpine— the official PostgreSQL 18 image built on the small Alpine Linux.environment— on the first start the image creates the databasechatexand the userpostgreswith the passwordpostgres. Such a password is acceptable only because the container is reachable from your computer alone; the production server (topic 23) gets real secrets.ports: "5432:5432"— the port 5432 of your computer leads to the port 5432 of the container, the standard PostgreSQL port.volumes— the data is kept in a named Docker volumepostgres-data, outside the container. Stopping or even deleting the container keeps the data; only deleting the volume removes it.healthcheck— every 5 seconds Docker runspg_isreadyinside the container; the container counts as healthy once PostgreSQL accepts connections.restart: unless-stopped— after a computer restart Docker starts the container again, unless you stopped it yourself.
Step 2. Start PostgreSQL
Do. Make sure Docker Desktop is running, then start the container in the background:
powershell
docker compose up --detach --wait--detach returns the terminal to you, --wait waits until the healthcheck reports healthy. The first start downloads the image (about 100 MB).
Check
powershell
docker compose psThe list shows the service postgres with the status Up … (healthy) and the port 0.0.0.0:5432->5432/tcp. Now ask PostgreSQL for its version with psql, the command-line client inside the container:
powershell
docker compose exec postgres psql -U postgres -d chatex -c "select version();"The answer starts with PostgreSQL 18..
The database is still empty: the tables appear in topic 4, when EF Core creates them. To look into the database later, you can use psql as above, the database window of Rider or Visual Studio, or a separate client such as DBeaver or pgAdmin with the connection settings host localhost, port 5432, database chatex, user and password postgres.
Useful commands for later:
| Command | What it does |
|---|---|
docker compose stop | stops the container, the data stays |
docker compose up --detach --wait | starts it again |
docker compose logs postgres | shows the PostgreSQL log |
docker compose down --volumes | deletes the container and the data, for a clean start |
Step 3. The EF Core command-line tool
Why. In topic 4 we create the database tables with EF Core migrations, and migrations are managed by the command-line tool dotnet ef. A tool can be installed globally on the computer, but then every developer may have a different version. A local tool is pinned in the file dotnet-tools.json of the repository, and dotnet tool restore installs exactly that version.
Do. Create the tool manifest and install the tool with the version that matches the EF Core packages:
powershell
dotnet new tool-manifestpowershell
dotnet tool install dotnet-ef --version 10.0.14The manifest in the repository root now lists the tool:
json
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "10.0.14",
"commands": [
"dotnet-ef"
],
"rollForward": false
}
}
}After cloning the repository on another computer, one command installs the tools of the manifest:
powershell
dotnet tool restoreCheck
powershell
dotnet ef --versionThe output ends with 10.0.14.