diff --git a/Dockers/postgres-shadow/Dockerfile b/Dockers/postgres-shadow/Dockerfile new file mode 100644 index 0000000..3f81ea3 --- /dev/null +++ b/Dockers/postgres-shadow/Dockerfile @@ -0,0 +1,20 @@ +FROM postgres:15 + +# Install additional tools +RUN apt-get update && apt-get install -y \ + postgresql-client \ + && rm -rf /var/lib/apt/lists/* + +# Create directory for scripts +RUN mkdir -p /docker-entrypoint-initdb.d + +# Copy initialization script +COPY init.sh /docker-entrypoint-initdb.d/ +RUN chmod +x /docker-entrypoint-initdb.d/init.sh + +# Environment variables +ENV SOURCE_DB_URL="" +ENV TARGET_DB_NAME="shadow_db" + +# Expose PostgreSQL port +EXPOSE 5432 diff --git a/Dockers/postgres-shadow/init.sh b/Dockers/postgres-shadow/init.sh new file mode 100644 index 0000000..dd5c13b --- /dev/null +++ b/Dockers/postgres-shadow/init.sh @@ -0,0 +1,25 @@ +#!/bin/bash +set -e + +# Wait for PostgreSQL to be ready +until pg_isready; do + echo "Waiting for PostgreSQL to be ready..." + sleep 2 +done + +# Create the target database +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE DATABASE $TARGET_DB_NAME; +EOSQL + +# Copy the database if SOURCE_DB_URL is provided +if [ ! -z "$SOURCE_DB_URL" ]; then + echo "Copying database from $SOURCE_DB_URL to $TARGET_DB_NAME..." + + # Dump the source database + pg_dump "$SOURCE_DB_URL" | psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$TARGET_DB_NAME" + + echo "Database copy completed successfully" +else + echo "No SOURCE_DB_URL provided, skipping database copy" +fi \ No newline at end of file