From a3c17e95399a108256377a88e073ff7ba0e5d1aa Mon Sep 17 00:00:00 2001 From: raczu Date: Sun, 31 Mar 2024 20:36:28 +0200 Subject: [PATCH] ci!: improve development environment 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. --- .env.sample | 7 ++- Database/init-users.sh | 9 ++-- Server/.dockerignore | 32 +++++++++++++ Server/ReasnAPI/ReasnAPI/appsettings.json | 2 +- Server/server.Dockerfile | 17 +++++++ compose.override.yml | 15 +++++++ compose.yml | 55 +++++++++++++++++++++++ docker-compose.override.yaml | 9 ---- docker-compose.yaml | 35 --------------- 9 files changed, 130 insertions(+), 51 deletions(-) create mode 100644 Server/.dockerignore create mode 100644 Server/server.Dockerfile create mode 100644 compose.override.yml create mode 100644 compose.yml delete mode 100644 docker-compose.override.yaml delete mode 100644 docker-compose.yaml diff --git a/.env.sample b/.env.sample index e91febe1..065303c6 100644 --- a/.env.sample +++ b/.env.sample @@ -1,3 +1,8 @@ +# SERVER +ASPNETCORE_ENVIRONMENT=Development + +SERVER_LISTEN_PORT=5272 + # POSTGRES POSTGRES_USER=dba POSTGRES_PASSWORD=sql @@ -12,4 +17,4 @@ POSTGRES_CLIENT_PASSWORD=sql # PGADMIN PGADMIN_LISTEN_PORT=5050 PGADMIN_DEFAULT_EMAIL=dba@reasn.io -PGADMIN_DEFAULT_PASSWORD=sql \ No newline at end of file +PGADMIN_DEFAULT_PASSWORD=sql diff --git a/Database/init-users.sh b/Database/init-users.sh index 874cc273..3da9c9e2 100644 --- a/Database/init-users.sh +++ b/Database/init-users.sh @@ -1,5 +1,4 @@ #!/bin/bash - set -e psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL @@ -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 diff --git a/Server/.dockerignore b/Server/.dockerignore new file mode 100644 index 00000000..e5c60aba --- /dev/null +++ b/Server/.dockerignore @@ -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 diff --git a/Server/ReasnAPI/ReasnAPI/appsettings.json b/Server/ReasnAPI/ReasnAPI/appsettings.json index de86dfdc..826f8080 100644 --- a/Server/ReasnAPI/ReasnAPI/appsettings.json +++ b/Server/ReasnAPI/ReasnAPI/appsettings.json @@ -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": "*" } diff --git a/Server/server.Dockerfile b/Server/server.Dockerfile new file mode 100644 index 00000000..a22d5148 --- /dev/null +++ b/Server/server.Dockerfile @@ -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"] diff --git a/compose.override.yml b/compose.override.yml new file mode 100644 index 00000000..17989357 --- /dev/null +++ b/compose.override.yml @@ -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 + \ No newline at end of file diff --git a/compose.yml b/compose.yml new file mode 100644 index 00000000..c8eb3e17 --- /dev/null +++ b/compose.yml @@ -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: diff --git a/docker-compose.override.yaml b/docker-compose.override.yaml deleted file mode 100644 index 7b1722a5..00000000 --- a/docker-compose.override.yaml +++ /dev/null @@ -1,9 +0,0 @@ -version: "3.8" - -services: - postgres: - ports: - - "5432:5432" - - volumes: - - ./Database/init-dev-data.sql:/docker-entrypoint-initdb.d/04_init-dev-data.sql \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 62754961..00000000 --- a/docker-compose.yaml +++ /dev/null @@ -1,35 +0,0 @@ -version: "3.8" - -services: - postgres: - image: postgres:16 - 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/03_init-users.sh - env_file: - - .env - - pgadmin: - image: dpage/pgadmin4:8.0 - networks: - - reasn-network - ports: - - "${PGADMIN_LISTEN_PORT:-5050}:${PGADMIN_LISTEN_PORT:-5050}" - volumes: - - pgadmin-data:/var/lib/pgadmin - env_file: - - .env - depends_on: - - postgres - -networks: - reasn-network: - driver: bridge - -volumes: - postgres-data: - pgadmin-data: