RDS DB migrations using ECS and Fargate

0

I run my app with Docker and ECS. How can I handle database migrations? With fargate there doesn’t seem to be a way to SSH into the container to run any commands so I can’t run my RDS MySQL migrations by going into the container. I have some migrations to run at times, not for all deploys. Any advice?

I used to run my docker container in a EC2 box so I had the option to run a command inside the container once the container is up. I have migrated to ECS and Fargate now and I don't know how to handle it now.

asked 4 years ago4136 views
6 Answers
1

While it is technically possible to install sshd in your image so you can SSH into your task, that's not a terribly practical approach. It would be helpful to understand your scenario in a little bit more detail. What's driving doing the DB migrations? Are you deploying updated code? If that's the case, consider automating the DB migration as part of your deployment (e.g. validate the state of the DB when you're task starts up and if not what you expect, do the migration).

/Mats

profile pictureAWS
Mats
answered 4 years ago
0

Hi,
Were you able to solve this issue? I am also facing the same sort of issue. I have an application deployed on ECS (fargate) and I want to run migrations.

answered 4 years ago
0
answered 4 years ago
0
answered 4 years ago
0

You can do it in your Dockerfile as part of your ENTRYPOINT or CMD. In Rails app you would normally run the migrations before you start the server by running

bundle exec rails db:migrate
bundle exec rails server -p 3000
jedrek
answered a year ago
0

@Mats ideally that would be good, but the deployment process of an ECS task doesn't seem to provide a mechanism to execute one-off commands as part of the process (in the same way that you can use initContainers in Kubernetes deployments or lifecycle hooks in Helm charts).

@jedrek that's a non-ideal workaround as 1) you'd be running migrations (eg: start db connections and execute queries at least to check the migration version) every time a container starts (not just a deployment). If I have a service running 100 copies, migrations would be execute at least 100 times during the deploy process with potential race conditions if multiple containers can start around the same time. 2) migrations would also run whenever containers get moved around container instances, when instances get removed/readded and so on.

would be great if the ECS service would allow for something like init containers so that a check (in the form of a command running inside a container) could be performed before a deployment starts.

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions