From 779674991491b773f13a738d10ba5953b5a2cfb6 Mon Sep 17 00:00:00 2001 From: alireza Date: Sat, 17 Oct 2026 16:56:50 +0330 Subject: [PATCH] build(datacopy): add Dockerfile, compose and env template Adds a self-contained way to run the SqlServer to Postgres copy anywhere: a multi-stage Dockerfile (net9.0 SDK build, ASP.NET runtime because the project references Microsoft.AspNetCore.App), a compose file that joins the shared greenNetwork so the target is reachable as postgres, an env template for the two connection strings, and a src/.dockerignore so the build context excludes node_modules and bin/obj. The image restores explicitly from NuGet.org because the repo's nuget.config declares an empty packageSources, which would leave a clean container with no feed at all. --- src/.dockerignore | 9 +++++++ src/GreenHome.DataCopy/.gitignore | 2 ++ src/GreenHome.DataCopy/Dockerfile | 30 +++++++++++++++++++++ src/GreenHome.DataCopy/datacopy.env.example | 9 +++++++ src/GreenHome.DataCopy/docker-compose.yml | 30 +++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 src/.dockerignore create mode 100644 src/GreenHome.DataCopy/.gitignore create mode 100644 src/GreenHome.DataCopy/Dockerfile create mode 100644 src/GreenHome.DataCopy/datacopy.env.example create mode 100644 src/GreenHome.DataCopy/docker-compose.yml diff --git a/src/.dockerignore b/src/.dockerignore new file mode 100644 index 0000000..955c94c --- /dev/null +++ b/src/.dockerignore @@ -0,0 +1,9 @@ +# Applies to builds whose context is this folder (src/) — currently the DataCopy image. +# Build output and local-only folders, so the context stays small and never poisons a build. +**/bin +**/obj +**/node_modules +**/dist +.vs +**/.git +debug.log diff --git a/src/GreenHome.DataCopy/.gitignore b/src/GreenHome.DataCopy/.gitignore new file mode 100644 index 0000000..91403b8 --- /dev/null +++ b/src/GreenHome.DataCopy/.gitignore @@ -0,0 +1,2 @@ +# real connection strings — never commit +datacopy.env diff --git a/src/GreenHome.DataCopy/Dockerfile b/src/GreenHome.DataCopy/Dockerfile new file mode 100644 index 0000000..556a90e --- /dev/null +++ b/src/GreenHome.DataCopy/Dockerfile @@ -0,0 +1,30 @@ +# DataCopy — copies all data from SqlServer into the Postgres target. +# The build context is the repo's src/ folder; see docker-compose.yml next to this file. + +FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build +WORKDIR /src + +# Only the project files first, so a source-only change reuses the restore layer. +# NOTE: the repo's nuget.config declares an empty , which would leave a +# clean container with no feed at all. It is therefore deliberately not copied before +# restore, so packages come from the default NuGet.org feed. +COPY GreenHome.DataCopy/GreenHome.DataCopy.csproj GreenHome.DataCopy/ +COPY GreenHome.Infrastructure/GreenHome.Infrastructure.csproj GreenHome.Infrastructure/ +COPY GreenHome.Application/GreenHome.Application.csproj GreenHome.Application/ +COPY GreenHome.Domain/GreenHome.Domain.csproj GreenHome.Domain/ +COPY GreenHome.Sms.Ippanel/GreenHome.Sms.Ippanel.csproj GreenHome.Sms.Ippanel/ +COPY GreenHome.VoiceCall.Avanak/GreenHome.VoiceCall.Avanak.csproj GreenHome.VoiceCall.Avanak/ +COPY GreenHome.AI.DeepSeek/GreenHome.AI.DeepSeek.csproj GreenHome.AI.DeepSeek/ +RUN dotnet restore GreenHome.DataCopy/GreenHome.DataCopy.csproj \ + --source https://api.nuget.org/v3/index.json + +COPY . . +RUN dotnet publish GreenHome.DataCopy/GreenHome.DataCopy.csproj -c Release -o /app --no-restore + +# Infrastructure references Microsoft.AspNetCore.App, so the ASP.NET runtime is required. +FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS runtime +WORKDIR /app +COPY --from=build /app . + +# Extra arguments are appended, e.g. `docker compose run --rm datacopy --reset`. +ENTRYPOINT ["dotnet", "GreenHome.DataCopy.dll"] diff --git a/src/GreenHome.DataCopy/datacopy.env.example b/src/GreenHome.DataCopy/datacopy.env.example new file mode 100644 index 0000000..a54a823 --- /dev/null +++ b/src/GreenHome.DataCopy/datacopy.env.example @@ -0,0 +1,9 @@ +# Copy to datacopy.env and fill in. datacopy.env is gitignored — never commit real credentials. +# Values are taken literally: if a password contains '#' or starts/ends with a space, wrap the +# whole value in double quotes. + +# SqlServer source (read-only). Use host.docker.internal when SqlServer runs on the Docker host. +ConnectionStrings__SqlServer=Server=host.docker.internal,1433;Database=GreenHomeDb;User Id=sa;Password=CHANGE_ME;TrustServerCertificate=True;Encrypt=False + +# Postgres target — the infra container's database. +ConnectionStrings__Postgres=Host=postgres;Port=5432;Database=greenhome;Username=greenhome;Password=CHANGE_ME diff --git a/src/GreenHome.DataCopy/docker-compose.yml b/src/GreenHome.DataCopy/docker-compose.yml new file mode 100644 index 0000000..95d96d4 --- /dev/null +++ b/src/GreenHome.DataCopy/docker-compose.yml @@ -0,0 +1,30 @@ +# DataCopy — copies data from SqlServer into the Postgres target. +# +# cp datacopy.env.example datacopy.env # fill in both connection strings +# docker compose build +# docker compose run --rm datacopy # copies only missing rows (resumable) +# docker compose run --rm datacopy --reset # wipes the target tables first, then copies +# +# Run it from this folder. The target is the shared infra Postgres, reached by the +# name "postgres" over the external greenNetwork (see infra/docker-compose.yml). +# The tool ends with a per-table row-count check and exits 1 on any mismatch. + +services: + datacopy: + image: greenhome-datacopy:latest + build: + context: .. + dockerfile: GreenHome.DataCopy/Dockerfile + env_file: + - datacopy.env + # lets the container reach a SqlServer that runs on the Docker host itself + extra_hosts: + - "host.docker.internal:host-gateway" + networks: + - greenNetwork + # one-shot job: nothing to restart, and it must not be left running + restart: "no" + +networks: + greenNetwork: + external: true