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
0
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
답변함 일 년 전
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.

답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠