0

I'm trying to configure a database container (PostgreSQL) and I want to configure some database settings by running SQL commands against the started instance. The RUN command in a dockerfile runs BEFORE the database launches.

I guess I could create a script to first stand up the docker instances then push out the configuration, but that's neither portable nor elegant.

Is there a way to somehow configure RUN commands or similar to run AFTER the container instance starts?

Thanks in advance for your help.

1 Answer 1

0

One solution might be:

  1. Create a bash script containing the startup commands you want to run inside your container

  2. Copy that script into the docker image with a COPY command

  3. Change the RUN command in the Dockerfile to run your script on startup, instead of calling postgres directly

  4. Ensure your script calls postgres (like the original RUN command did) before your configuration commands

You might need your script to run postgres in the background:

# Run postgres, but in the background, so we can execute more commands
postgres ... & pg_pid="$!"

# Your configuration commands here
...

# Tell bash not to exit until postgres does
wait $pg_pid

However, I don't know whether docker will shut down postgres gently in this case. (It might just kill the shell.)


You can also override the default RUN command by specifying your startup command with docker run ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .