I have below type of folder structure
project-folder/
├── Dockerfolder/
│ └── Dockerfile
└── pg/
├── folder1/
│ ├── 01-create-db.sql
│ ├── 02-create-tables.sql
│ └── 03-seed-data.sql
└── folder2/
├── 01-create-db.sql
├── 02-create-tables.sql
└── 03-seed-data.sql
I want to build a docker image based on postgres:latest
base image where when the container is started I want to run all the .sql
files on the server created
if I put the .sql
file in same directory (Dockerfolder) as Dockerfile
then I am able to copy them in docker-entrypoint-initdb.d
directory using COPY pg /docker-entrypoint-initdb.d/
line but I want to have the files only .sql
inside the docker-entrypoint-initidb.d
when I have the above folder structure
I have tried to push the entier structure (with folder and files) inside docker-entrypoint-initidb.d
but then it do not run those .sql file when i create a container out of it. this makes me believe that docker-entrypoint-initidb.d
need .sql file in its root.
I have tried the below 2 version of docker file
V1
FROM postgres:latest as builder
ENV POSTGRES_USER=postgresuser
POSTGRES_PASSWORD=mysecretpassword
POSTGRES_HOST=postgreshost
# Copy all the init.sql files from the subdirectories to the Docker container
COPY pg /docker-entrypoint-initdb.d/
# RUN docker-entrypoint.sh postgres -D /var/lib/postgresql/data
V2
# Use the official PostgreSQL image as the base image
FROM postgres:latest
# Create the directory in the image
# RUN mkdir -p /docker-entrypoint-initdb.d
# Copy .sql files from the host to the image
COPY ../pg /pg
# Move all .sql files from init-scripts to docker-entrypoint-initdb.d
RUN find /pg -name "*.sql" -exec cp {} /docker-entrypoint-initdb.d/ ;
Can you help me how can i do that? In short I want to roam around all the directory, subdirectory, sibling directories and find all .sql file and copy those files only in docker-entrypoint-initidb.d
P.S. if you have time do you have any other brand new approach that
you can suggest to achieve the result. Essentially I want to have a
container which is when spinned will be ready with my databases, my
tables inside those and my master data inside those tables.(not all
tables have master data)