10

I've got a custom image based on the official postgres image and I want to extend the entrypoint of the parent image so that it would create new users and databases if they don't exist yet every time a container starts up. Is it possible? Like my image would execute all the commands from the standard entrypoint plus my own shell script.

I know about putting my own scripts into the /docker-entrypoint-initdb.d directory, but it seems that they get executed only when the volume is created the first time.

super.t
  • 2,526
  • 7
  • 32
  • 51

2 Answers2

9

What you need to do is something like below

setup_user.sh

sleep 10
echo "execute commands to setup user"

setup.sh

sh setup_user.sh &
./docker-entrypoint.sh postgres

And your image should use the ENTRYPOINT as

ENTRYPOINT ["/setup.sh"]

You need to start your setup script in background and let the origin entryscript do its works to start the database

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Seems to be a good one, thanks. I was also thinking of adding a script into the image which is supposed to be run manually, modifying it whenever I need new users/databases and executing it manually via the terminal attaching to the container. – super.t Sep 27 '17 at 19:57
0

In addition to the accepted answer Docker - extend the parent's ENTRYPOINT, instead of sleeping a specific time, you may want to consider executing your script similar to how ''docker-entrypoint.sh'' of the postgres docker image does it (docker-entrypoint.sh; to init the DB, they start the server, execute initialization commands, and shut it down again). Thus:

setup_user.sh

su - "$YOUR_PG_USER" -c '/usr/local/bin/pg_ctl -D /var/lib/postgresql/data -o "-c listen_addresses='localhost'" -w start'
psql -U "$YOUR_PG_USER" "$YOUR_PG_DATABASE" < "$YOUR_SQL_COMMANDS"
su - "$YOUR_PG_USER" -c '/usr/local/bin/pg_ctl -D /var/lib/postgresql/data -m fast -w stop'

setup.sh

./setup_user.sh && ./docker-entrypoint.sh postgres
stef77
  • 1,000
  • 5
  • 19