Switch backend to wait for SQL Server and use direct INSERTs
Replace stored procedure calls with inline INSERT statements and SCOPE_IDENTITY() Add netcat for readiness check in new entrypoint.sh Introduce dedicated SQL Server and init-db services in docker-compose
This commit is contained in:
27
db/Dockerfile
Normal file
27
db/Dockerfile
Normal file
@@ -0,0 +1,27 @@
|
||||
# Dockerfile for DB init service
|
||||
|
||||
FROM mcr.microsoft.com/mssql/server:2022-latest
|
||||
|
||||
# Set SQL Server environment variables
|
||||
ENV SA_PASSWORD=Ou812@12!@ \
|
||||
ACCEPT_EULA=Y
|
||||
|
||||
# Set working directory inside the container
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Copy the initialization script and SQL file into the image
|
||||
COPY --chmod=0755 init.sh .
|
||||
COPY init.sql .
|
||||
|
||||
# Make the script executable
|
||||
|
||||
|
||||
# Start SQL Server, wait until it is ready, then run the init script
|
||||
CMD /opt/mssql/bin/sqlservr & \
|
||||
/bin/bash -c "\
|
||||
until /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P $$SA_PASSWORD -Q \"SELECT 1\" >/dev/null 2>&1; do \
|
||||
sleep 1; \
|
||||
done && \
|
||||
./init.sh && \
|
||||
tail -f /dev/null \
|
||||
"
|
||||
32
db/init.sh
Normal file
32
db/init.sh
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# init.sh - Initialize the SQL Server database with the schema and sample data.
|
||||
# This script is intended to be run inside the SQL Server container (or from the host
|
||||
# with sqlcmd reachable). It executes the init.sql script located in the same
|
||||
# directory.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Default connection settings – can be overridden via environment variables
|
||||
: "${DB_SERVER:=localhost}"
|
||||
: "${DB_USER:=sa}"
|
||||
: "${DB_PASSWORD:=YourStrong!Passw0rd}"
|
||||
: "${DB_NAME:=CustomerDB}"
|
||||
: "${SQLCMD_PATH:=sqlcmd}" # Path to the sqlcmd executable
|
||||
|
||||
# Path to the init script (relative to this script's location)
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
INIT_SQL="${SCRIPT_DIR}/init.sql"
|
||||
|
||||
if [[ ! -f "${INIT_SQL}" ]]; then
|
||||
echo "❌ init.sql not found at ${INIT_SQL}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "🚀 Running database initialization..."
|
||||
"${SQLCMD_PATH}" -S "${DB_SERVER}" -U "${DB_USER}" -P "${DB_PASSWORD}" -d "master" -Q "IF DB_ID(N'${DB_NAME}') IS NULL CREATE DATABASE ${DB_NAME};"
|
||||
|
||||
# Run the init script against the newly created database
|
||||
"${SQLCMD_PATH}" -S "${DB_SERVER}" -U "${DB_USER}" -P "${DB_PASSWORD}" -d "${DB_NAME}" -i "${INIT_SQL}"
|
||||
|
||||
echo "✅ Database ${DB_NAME} initialized successfully."
|
||||
Reference in New Issue
Block a user