Running SQL Migrations Before Booting Docker Compose Services

Running SQL Migrations Before Booting Docker Compose Services

In this article, I’ll cover your local experience, which easily extends to CI, and I’ll also touch how to do this in production. Having a great local development experience is critical to happy engineers.

Developers are happy to come into a code base and start hacking away at problems, as opposed to dreading picking up their laptops. As services become more and more separated into separate “micro-services,” new problems arise. When I look back on my short career, one problem that has caused me a lot of pain is SQL migrations and how they should work in Docker Compose. Ideally, they should run before your web apps boot so they have access to a well-structured database. Your strategy to make this happen may be different for each environment. In this article, I’ll cover your local experience, which easily extends to CI, and I’ll also touch how to do this in production.

A Bit of Background

I ran into this problem when we started to adopt Apollo Federation into our stack. One gateway service lives in front of our other GraphQL services (which have their own databases). This gateway is what the client-side app (“Web Browser” in diagram) queries to get its data. For clients to start using the gateway API, every dependent service must be up and healthy.

diagram showing relationship of components of Apollo Federation Gateway

Our service diagram

My goal is to start the entire set of services in a deterministic manner, to have each service wait until its dependent service is running and healthy. I’ll focus on the database and migrations in this article, but the concepts here extend to any service having dependencies on another service.

A typical Docker Compose file which runs the products service with its migrations will look like this:

There are a few issues with this setup, focusing on depends_on:

  1. The products-run-migrations script will run right when postgres starts to run, not when it's healthy and ready to accept connections. There is a 99% chance that the postgres container will not be healthy when the migrations begin to run, causing them to fail.
  2. Similar situation with the products service. It will run right when products-run-migrations starts to run. If you query the products service when it's healthy, there is a good chance the migrations have not yet completed.

software-development docker-compose sql docker devops

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Hire Dedicated DevOps Developers

Hire our Dedicated DevOps Developers who have in-depth skills and expertise to develop an interactive and secure web application. Get custom DevOps solutions for your project.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

Hire DevOps Developer

Looking to hire top DevOps developers at affordable prices? **[Hire DevOps Developer](https://hourlydeveloper.io/hire-dedicated-devops-developer/ "Hire DevOps Developer")** from **[HourlyDeveloper.io](https://hourlydeveloper.io/...