super.t
super.t

Reputation: 2746

Docker - extend the parent's ENTRYPOINT

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.

Upvotes: 11

Views: 11560

Answers (2)

stef77
stef77

Reputation: 1000

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

Upvotes: 1

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

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

Upvotes: 9

Related Questions