Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Wait until Your Dockerized Database Is Ready before Continuing

wait-until-your-dockerized-database-is-ready-before-continuing.jpg

In this video, we'll go over using a script to make sure your database is ready before running automated tests. Perfect for CI.

Quick Jump:

Without some type of logic to ensure PostgreSQL, MySQL or whatever database you use is initialized and ready to go, you’re very likely going to run into failing tests in a Continuous Integration (CI) environment when you run tests that depend on your DB being available.

Technically you can use this script to wait until any command is ready but for this video, let’s focus on the very common use case of waiting for a database to be ready that’s running in Docker as part of a Docker Compose project.

# Demo Video of the wait-until Script

Timestamped Table of Contents

  • 1:15 – Taking a look at an example CI pipeline
  • 2:28 – What happens when you don’t wait for your database
  • 2:57 – Fixing things by introducing the wait-until script
  • 4:43 – Seeing things work after calling the new wait-until script
  • 5:49 – We looked at a PostgreSQL example but MySQL works too
  • 6:08 – Investigating how we load in our environment variables
  • 7:31 – Checking out the README file for the wait-until script
  • 8:02 – Taking a quick look at the script itself
  • 9:10 – Why bother using this script when other tools exist?
  • 12:43 – You can install it anywhere, including your base Docker CI image
  • 14:33 – Copy / paste’able examples for using this with PostgreSQL and MySQL
  • 16:19 – Avoiding the input device is not a TTY error in a CI environment
  • 17:11 – You’re in full control of redirecting the output of your wait command
  • 18:46 – You can configure the timeout by passing in an optional 2nd argument
  • 19:41 – Feel free to open a pull request if you have any improvements

Reference Links

What do you do to ensure a Dockerized service is ready in CI? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments