Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit f597484

Browse files
feat: Docker Database-migration and DbContext creation (#8)
1 parent f57c6f7 commit f597484

File tree

6 files changed

+63
-68
lines changed

6 files changed

+63
-68
lines changed

compose.yaml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ services:
5656
environment:
5757
ACCEPT_EULA: "Y"
5858
MSSQL_SA_PASSWORD: "${DATABASE_PASSWORD}"
59-
MSSQL_PID: "Developer"
6059
ports:
6160
- "1433:1433"
6261
user: "root"
@@ -71,17 +70,20 @@ services:
7170
networks:
7271
- backend
7372

74-
db-setup:
75-
container_name: "db-setup"
73+
db-migrations:
74+
container_name: db-migrations
7675
build:
77-
context: ./database
78-
dockerfile: Dockerfile
79-
restart: "no"
80-
environment:
81-
DATABASE_PASSWORD: "${DATABASE_PASSWORD}"
76+
context: .
77+
dockerfile: src/ServiceLayer.Mesh/Dockerfile.migrations
78+
args:
79+
DatabaseConnectionString: "${DatabaseConnectionString}"
8280
depends_on:
8381
db:
8482
condition: service_healthy
83+
environment:
84+
DATABASE_NAME: "${DATABASE_NAME}"
85+
DATABASE_USER: "${DATABASE_USER}"
86+
DATABASE_PASSWORD: "${DATABASE_PASSWORD}"
8587
networks:
8688
- backend
8789

database/Dockerfile

Lines changed: 0 additions & 16 deletions
This file was deleted.

database/create_database_statement.sql

Lines changed: 0 additions & 23 deletions
This file was deleted.

database/db-setup-entrypoint.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Drop and recreate database for fresh start
5+
echo "Checking if database exists..."
6+
/opt/mssql-tools/bin/sqlcmd -S db -U "$DATABASE_USER" -P "$DATABASE_PASSWORD" -Q "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'$DATABASE_NAME') CREATE DATABASE $DATABASE_NAME"
7+
8+
# Run migrations
9+
echo "Running migrations..."
10+
/opt/mssql-tools/bin/sqlcmd -S db -d "$DATABASE_NAME" -U "$DATABASE_USER" -P "$DATABASE_PASSWORD" -i /database/migration.sql
11+
12+
echo "Migration completed successfully!"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Stage 1: Build & Generate EF Migration Script
2+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build-env
3+
4+
WORKDIR /src
5+
6+
RUN apt-get update && apt-get install -y git
7+
8+
COPY . .
9+
10+
RUN git submodule update --init --recursive
11+
12+
ARG DatabaseConnectionString
13+
ENV DatabaseConnectionString=${DatabaseConnectionString}
14+
15+
RUN dotnet restore ./src/ServiceLayer.Mesh/ServiceLayer.Mesh.csproj && \
16+
dotnet build ./src/ServiceLayer.Mesh/ServiceLayer.Mesh.csproj && \
17+
dotnet tool install dotnet-ef --tool-path /tools
18+
19+
ENV PATH="/tools:$PATH" \
20+
DOTNET_CLI_HOME=/tmp \
21+
DOTNET_SKIP_FIRST_TIME_EXPERIENCE=true \
22+
DOTNET_NOLOGO=true
23+
24+
RUN mkdir -p /database && \
25+
dotnet ef migrations script \
26+
-o /database/migration.sql \
27+
--project ./src/ServiceLayer.Mesh/
28+
29+
# Stage 2: Runtime - Apply Migration and Seed Data
30+
FROM mcr.microsoft.com/mssql-tools:v1 AS migration-env
31+
32+
COPY --from=build-env /database/migration.sql /database/migration.sql
33+
COPY scripts/database/migrate-and-seed.sh /scripts/database/migrate-and-seed.sh
34+
35+
RUN addgroup --system appgroup && \
36+
adduser --system --ingroup appgroup appuser && \
37+
chmod +x /scripts/database/migrate-and-seed.sh
38+
39+
USER appuser
40+
41+
ENTRYPOINT ["/scripts/database/migrate-and-seed.sh"]

0 commit comments

Comments
 (0)