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.

已提問 4 年前檢視次數 4340 次
6 個答案
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
已回答 4 年前
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.

已回答 4 年前
0
已回答 4 年前
0
已回答 4 年前
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
已回答 1 年前
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.

已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南