I'm using a wait-for-it script to check if the database is up before I'm launching my other app.
until psql -h "$host" -U "postgres" -c '\l'; do
>&2 echo "Postgres is unavailable - sleeping"
>&2 echo "Postgres is up - executing command"
MySQL is up - executing command
COPY docker-entrypoint.sh /entrypoint.sh
COPY ./wait-for-it.sh wait-for-it.sh
CMD ["npm", "start"]
Let's clear up a couple of things first. When you use both
CMD in a Dockerfile, the
CMD value is passed to the
ENTRYPOINT as a parameter. So what you currently have in the file translates to
/entrypoint.sh npm start
This is executed when you start the container. Without knowing what's happening in
entrypoint.sh, it's hard to tell what impact this has.
You could make the following changes, please give this a try:
CMD to the following:
CMD /wait-for-it.sh localhost && /entrypoint.sh npm start
When doing that, please adjust the following:
wait-for-it.sh- please adjust to wherever you're copying the script file in the Dockerfile. I suggest you copy it to the same folder as
localhostargument for the
wait-for-it.shscript file, please replace with your database host.
What the above does, is run the
wait-for-it.sh script and then, once the database is up, it runs the previous command that you had in
CMD. It should be comparable to what you currently have.
As an alternative, you could also call the
wait-for-it.sh script from your
entrypoint.sh script and only run the additional steps (
npm start once the wait script has succeeded). Up to you...
If you are using Docker-Compose for starting up your containers, you can overwrite the command that is executed when the container starts using the
command attribute in your
docker-compose.yaml file, e.g.
command: > bash -c " /wait-for-it.sh localhost && /entrypoint.sh npm start "
Please note the use of
bash -cfor starting multiple commands using your shell of choice (Bash in this case)
Using this method, you can basically chain multiple commands to run after each other, combining them using the
BTW: I use this wait-for-it script for a similar purpose with good results in the same manner as described above. It's slightly more robust than you version of the wait script, and supports pretty much any host/port combination. I use it to wait for MySQL - your question is not clear whether it's about MySQL or PostgreSQL.