Skip to content

Commit

Permalink
ci!: improve development environment
Browse files Browse the repository at this point in the history
BREAKING CHANGE: renamed docker compose files to compose*.yml.

Simplify running up development environment. Now it requires
to use simple command: docker-compose watch

Introduce a watch mechanism in development environemnt to
rebuild dotnet container each time the server source code
is changed.

Add healtcheck mechanism to ensure the functionality of the
postgres before running up the rest of services.
  • Loading branch information
raczu committed Mar 31, 2024
1 parent b6395e8 commit a3c17e9
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 51 deletions.
7 changes: 6 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# SERVER
ASPNETCORE_ENVIRONMENT=Development

SERVER_LISTEN_PORT=5272

# POSTGRES
POSTGRES_USER=dba
POSTGRES_PASSWORD=sql
Expand All @@ -12,4 +17,4 @@ POSTGRES_CLIENT_PASSWORD=sql
# PGADMIN
PGADMIN_LISTEN_PORT=5050
PGADMIN_DEFAULT_EMAIL=[email protected]
PGADMIN_DEFAULT_PASSWORD=sql
PGADMIN_DEFAULT_PASSWORD=sql
9 changes: 4 additions & 5 deletions Database/init-users.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
Expand All @@ -14,10 +13,10 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E
GRANT CREATE ON SCHEMA common TO $POSTGRES_ADMIN_USER;
CREATE USER "$POSTGRES_CLIENT_USER" WITH LOGIN PASSWORD '$POSTGRES_CLIENT_PASSWORD';
GRANT CONNECT ON DATABASE $POSTGRES_DB TO $POSTGRES_CLIENT_USER;
GRANT SELECT ON ALL TABLES IN SCHEMA events TO $POSTGRES_CLIENT_USER;
GRANT CONNECT ON DATABASE $POSTGRES_DB TO $POSTGRES_CLIENT_USER;
GRANT SELECT ON ALL TABLES IN SCHEMA events TO $POSTGRES_CLIENT_USER;
GRANT SELECT ON ALL TABLES IN SCHEMA common TO $POSTGRES_CLIENT_USER;
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA users TO $POSTGRES_CLIENT_USER;
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA users TO $POSTGRES_CLIENT_USER;
GRANT UPDATE, INSERT ON events.event, events.tag, events.parameter, common.status, common.image TO $POSTGRES_CLIENT_USER;
EOSQL
32 changes: 32 additions & 0 deletions Server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/go/build-context-dockerignore/

**/.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
2 changes: 1 addition & 1 deletion Server/ReasnAPI/ReasnAPI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
]
},
"ConnectionStrings": {
"DefaultValue": "Server=localhost;Port=5432;Database=reasn;User Id=dba;Password=sql;"
"DefaultValue": "Server=postgres;Port=5432;Database=reasn;User Id=dba;Password=sql;"
},
"AllowedHosts": "*"
}
17 changes: 17 additions & 0 deletions Server/server.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
COPY . /source
WORKDIR /source/ReasnAPI/ReasnAPI
ARG TARGETARCH
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS development
COPY . /source
WORKDIR /source/ReasnAPI/ReasnAPI
CMD ["dotnet", "run", "--no-launch-profile"]

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS production
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["dotnet", "ReasnAPI.dll"]
15 changes: 15 additions & 0 deletions compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
server:
build:
target: development
develop:
watch:
- action: rebuild
path: ./Server/ReasnAPI/ReasnAPI

postgres:
expose:
- 5432
volumes:
- ./Database/init-dev-data.sql:/docker-entrypoint-initdb.d/03-init-dev-data.sql

55 changes: 55 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
services:
server:
build:
context: Server
dockerfile: server.Dockerfile
target: production
networks:
- reasn-network
ports:
- "${SERVER_LISTEN_PORT:-5272}:8080"
env_file:
- .env
depends_on:
postgres:
condition: service_healthy

postgres:
image: postgres:16
restart: unless-stopped
networks:
- reasn-network
volumes:
- postgres-data:/var/lib/postgresql/data
- ./Database/init-db.sql:/docker-entrypoint-initdb.d/01-init-db.sql
- ./Database/init-constraints.sql:/docker-entrypoint-initdb.d/02-init-constraints.sql
- ./Database/init-users.sh:/docker-entrypoint-initdb.d/99-init-users.sh
env_file:
- .env
healthcheck:
test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER:-postgres}", "-d", "${POSTGRES_DB:-postgres}"]
interval: 10s
timeout: 5s
retries: 5

pgadmin:
image: dpage/pgadmin4:8.0
networks:
- reasn-network
ports:
- "${PGADMIN_LISTEN_PORT:-5050}:${PGADMIN_LISTEN_PORT:-80}"
volumes:
- pgadmin-data:/var/lib/pgadmin
env_file:
- .env
depends_on:
postgres:
condition: service_healthy

networks:
reasn-network:
driver: bridge

volumes:
postgres-data:
pgadmin-data:
9 changes: 0 additions & 9 deletions docker-compose.override.yaml

This file was deleted.

35 changes: 0 additions & 35 deletions docker-compose.yaml

This file was deleted.

0 comments on commit a3c17e9

Please sign in to comment.