How can I troubleshoot 'required file not found' error when running Postgres docker-compose with initialization scripts?
Asked Answered
D

1

5

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.

Dampen answered 19/5, 2023 at 18:38 Comment(0)
D
16

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.

Dampen answered 21/5, 2023 at 3:6 Comment(5)
Hi Alexandre, can you expand on your solution here?Dawkins
@Kieran, Hi! You must convert all your .sh files from CRLF (Windows) to LF (Linux) end of line sequence (character). You can use VS Code or any editor you like (with this conversion). And don't forget to save your files after conversion ;)Malcolmmalcom
I'd been struggling with this for so long and wondered why my init script was flaky. I can't believe this is why. It should be documented. Thanks @MalcolmmalcomDay
I had the same problem but for me it wasn't the line endings. It was strange but I had to RUN ./myfile.sh NOT RUN myfile.shTangent
On Linux, find /path/to/local/workspace -type f -print0 | xargs -0 dos2unix -- solved the issue for me.Sid

© 2022 - 2024 — McMap. All rights reserved.