Run Migrations Using Docker in Node.js and PostgreSQL

Nowadays, Docker is becoming the essential tool to create any type of web app. It isolates your application which you can run anywhere you need to.

If you are not familiar with the docker, I would recommend the following links where you could get started.

You can find tons of other resources as well on the docker and it is something you should be knowing. I will be assuming you have at least beginner knowledge in docker and docker-compose, and you can get started with this article.

Before we begin

There is something you should understand, when running any container there is a certain period where the container will take its time to build. Any process which is dependent on that one needs to be kept on hold till that.

In our case, we should wait till the database is up and volume is attached then only we will be able to run our migrations, right?

If I talk specifically, then in PostgreSQL, when we up the database container, the following are some operations it performs.

image

You can skip the content, I just wanted you to know even if the database is starting it does not mean it will be available after the start so you might need to wait for some time to begin.

Bdw, you can get the source code from here if you want to skip the theory.

Let's get started

I will be getting straight to the point, You will have a command which will execute your migration. In my case, I have a package.json file and have a command named npm run migrate and it will run all my migrations.

Now Let's create a file named Dockerfile and put some content there.

DockerfileDockerfile

Now, let's understand what additional we wrote apart from the traditional Dockerfile. You will see only one line which is

RUN git clone https://github.com/vishnubob/wait-for-it.git

This will make a clone of the wait-for-it repo and use its command-line methods to get what we want.

You should visit and see the wait-for-it in action on github repo

Above that, we also installed one more dependency externally which is db-migrate-pg which is optional, It was packages need to install so be in that way.

Now Let's create one more file named docker-compose.yml which will be handling our up, down, pull, and build operations. To begin with, let understand the structure.

We will be having 3 apps:

  • database

  • web (any other depending upon your need)

  • migration (will run the migration to your project)

Here both, web and migration will depend on the database app and after it runs, they will start their execution.

docker-compose.ymldocker-compose.yml

Db service

We are getting values from env and attaching a column to it and exposing a port 5432 from it, this is cruel as we need to be taking care of port.

Web service

Let's build this from the context of Dockerfile and link volume and attach port to it. If you notice, we are exposing HOST as db application name which depends on db service.

Migration

This service will be similar to web service but if you see the command it is a bit different which is waiting for db to start on port 5432 same as exposing it on db service and then running the migration command.

Finalizing

To test if everything works correctly or not, let write command docker-compose up this will first build the image and then perform all the operations. Following is the output you can see on the terminal.

outputoutput

Great! This seems to work fabulously now. You can find the source code here.

I hope you learnt something new today; if you like my content, do consider subscribing to my newsletter here.

Thanks!

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics