I'm tryng to use a docker-compose file to mount a posgres container and run two scripts after that. i"m using 'docker-entrypoint-initdb.d' for that.
Here is my docker-compose file:
version: '3.4' services: postgres-compose: image: postgres:11.20-alpine container_name: postgres11-20 environment: POSTGRES_DB: "postgres" POSTGRES_PASSWORD: "123" ports: - "5432:5432" volumes: - "C:/Docker/postgreslvolume:/var/lib/postgresql/data" - "./scripts/:/docker-entrypoint-initdb.d/"`
And my scripts and dump file in directory ./scripts are: init-a.sh, init-b.sh, backup.dump.
init-a.sh is like this:
#!/bin/sh psql -c "CREATE ROLE usertest PASSWORD 'password' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;" psql -c "CREATE DATABASE databasetest;" psql --username postgres -c "grant all privileges on databasetest to usertest"
init-b.sh is like this:
#!/bin/sh pg_restore -p 5432 -U usertest -d databasetest -v "/docker-entrypoint-initdb.d/backup.dump" exit
My dump file was created on pgadmin.
But its its not working. And this is what docker log show me:
/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/backup.dump 2023-05-19 15:05:25 2023-05-19 15:05:25 /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init-a.sh 2023-05-19 15:05:25 /usr/local/bin/docker-entrypoint.sh: line 170: /docker-entrypoint-initdb.d/init-a.sh: cannot execute: required file not found
The first file sets the database and user and the second restore the database. But it tells me that file ins't there. I'm following the script documentation of docker hub for postgres.
I'm expecting to make this dump automatically. I've tryed to change directories and files names. But it's not working at all.
1 Answer
I found the solution after doing a lot of research. There's to many users with problems like these. But no one exactly like this one. So, I found that the problem was I used Windows notepad to change the scripts. That made some changes in file format, and at initialization docker couldn't read the file normally. It even made the script files copies at /docker-entrypoint-initdb.d/ but the copied file ended with 0 bytes inside the container and the volume.
2ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobm9qaWaEcX%2BOoaawZZOWu261jK2pqK2SobK0tM6oq2aqlabCqr7EnWSfoZyaeq%2B702adqK2emXqmvtGoqWavmJq7br7Up6WippdivbC%2F06Cpnqtdmbyk