Rails Docker Image Deployed on Kubernetes Cluster. Safe to run rake:db:migrate on every Dockerfile build?

1/5/2018

I'm trying to determine where to put the rake db:migrate in our deployment. I have it currently in the Dockerfile.

The basic setup we have now is.

Dev works on Rails App -> Push to Repo -> Build/Scan/Push Image to Docker Registry -> Deploy to K8s cluster.

Currently in this workflow, every time it Builds it will do a rake db:migrate.

Is this reasonable?

-- David West
docker
kubernetes
ruby-on-rails

1 Answer

1/7/2018

I think this is reasonable if you use a RollingUpdate strategy for your kubernetes deployment.

In a case where you are even running multiple replicas of your rails app, you should be fine.

For example, if you are running 5 replicas, and use RollingUpdate, when you deploy a new image of your rails app, all 5 replica deployments will run rake db:migrate, but only the first run will actually apply the migration. The other runs will do nothing.

As long as you use RollingUpdate, this will allow some time to pass between replica deployments, so you won’t run into database locking issues etc, that you might experience deploying all replicas at the same time.

-- grizzthedj
Source: StackOverflow