Running your applications on Qovery is pretty straightforward, but there are many cases where you will need to run some tasks before your application starts, like running database migrations, and it might not be obvious how to do it.
Before you begin, this guide assumes the following:
- Your app is running in Dockerfile mode.
This tutorial will cover how to run tasks when your application is starting. We'll use the case of a database migration with Ruby on Rails, but the same principle can be applied for any framework or command you need to run.
Add an entrypoint script
The first thing to do is to add an entrypoint script. We'll add it to a docker directory at the project's root, but you can put it anywhere you want.
Let's create the docker/entrypoint.sh with the following content:#! /bin/shbundle exec rails db:migrateif [[ $? -eq 0 ]] ; thenecho -e "\n== Failed to migrate. Running setup first. ==\n"bundle exec rails db:setupfi# Execute the given or default command:exec "[email protected]"
This script will execute the Rails database migration script. If it fails because the database doesn't exist, it will run the setup command instead.
The last line is necessary to execute the main command of your Dockerfile.
Give execution permission to the entrypoint script
You can give execution permission to this file with the following command:
chmod +x docker/entrypoint.sh
Add the entrypoint to your Dockerfile
You will now need to add an ENTRYPOINT instruction to your Dockerfile. (Make sure the entrypoint.sh file is copied to the image somewhere)# ...# Dockerfile content omitted for brevity# ...ENTRYPOINT ["./docker/entrypoint.sh"]EXPOSE 3000CMD ["rails", "server", "-p", "3000", "-b", "0.0.0.0"]
You can learn more about Docker entrypoints here: https://docs.docker.com/engine/reference/builder/#entrypoint
Deploy your application
You can now commit and push your changes to your Git repository. The instructions you specified in the entrypoint.sh file will be executed before the application starts.