如何在Amazon Elastic Beanstalk单容器Docker环境中运行Rails迁移和播种 [英] How to run Rails migrations and seeding in Amazon Elastic Beanstalk single container Docker environment

查看:161
本文介绍了如何在Amazon Elastic Beanstalk单容器Docker环境中运行Rails迁移和播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Docker部署Rails应用程序到Elastic Beanstalk,到目前为止,一切都已经解决了。我正在应用程序需要运行数据库的迁移和播种,我无法确定我需要如何进行。看来, /。ebextensions 文件夹中的任何命令都在主机上下文中运行,而不是docker容器。这是正确的吗?

I'm working on deploying a Rails application to Elastic Beanstalk using docker and so far everything has worked out. I'm at the point where the application needs to run migrations and seeding of the database, and I'm having trouble figuring out exactly how I need to proceed. It appears that any commands in the /.ebextensions folder run in the context of the host machine and not the docker container. Is that correct?

启动后,运行命令执行docker容器内的迁移,但如何确保迁移只能运行在单一实例?有没有一个环境变量或其他一些方法,我可以从码头集装箱内知道什么机器是领导者?

I'm fine with running a command to execute migrations inside of the docker container after startup, but how do I ensure that the migrations only run on a single instance? Is there an environment variable or some other way I can tell what machine is the leader from within the docker container?

更新:我发布了亚马逊弹性Beanstalk论坛中的一个问题6月/ 8月/ 15日如何运行Docker主机上容器上的命令。你可以跟随那里的对话以及它们是有用的。

Update: I posted a question in the Amazon Elastic Beanstalk forums asking how to run "commands from Docker host on the container" on the 6th/Aug/15'. You can follow the conversations there as well as they are useful.

推荐答案

我不知道你提出的解决方案是上班。看起来当前处理EB Docker的部署运行容器命令之前新的docker容器运行,这意味着你不能使用 docker exec 。我怀疑你的命令会对旧的容器执行,这个旧容器还没有停止运行。

I'm not sure the solution you have proposed is going to work. It appears that the current process for EB Docker deployment runs container commands before the new docker container is running, which means that you can't use docker exec on it. I suspect that your commands will execute against the old container which is not yet taken out of service.

经过多次的试用和错误,我通过使用容器命令shell脚本。

After much trial and error I got this working through using container commands with a shell script.

container_commands:
  01_migrate_db:
    command: ".ebextensions/scripts/migrate_db.sh"
    leader_only: true

脚本:

if [ "${PROCESS}" = "WEB" ]; then

  . /opt/elasticbeanstalk/hooks/common.sh

  EB_SUPPORT_FILES=$(/opt/elasticbeanstalk/bin/get-config container -k support_files_dir)

  EB_CONFIG_DOCKER_ENV_ARGS=()

  while read -r ENV_VAR; do
    EB_CONFIG_DOCKER_ENV_ARGS+=(--env "$ENV_VAR")
  done < <($EB_SUPPORT_FILES/generate_env)

  echo "Running migrations for aws_beanstalk/staging-app"
  docker run --rm "${EB_CONFIG_DOCKER_ENV_ARGS[@]}" aws_beanstalk/staging-app bundle exec rake db:migrate || echo "The Migrations failed to run."
fi
true

我将整个脚本包装在支票中,以确保迁移不会在后台工作人员上运行。

I wrap the whole script in a check to ensure that migrations don't run on background workers.

然后,我们以完全一样的方式,在EB启动新容器时使用ENV来构建ENV,以便正确的环境到位对于迁移。

I then build the ENV in exactly the same way that EB does when starting the new container so that the correct environment is in place for the migrations.

最后,我针对已创建但尚未运行的新容器运行命令 - aws_beanstalk / staging-app 。它在迁移结束时退出,而 - rm 会自动删除该容器。

Finally I run the command against the new container which has been created but is not yet running - aws_beanstalk/staging-app. It exits at the end of the migration and the --rm removes the container automatically.

这篇关于如何在Amazon Elastic Beanstalk单容器Docker环境中运行Rails迁移和播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆