How to run Rails migrations and seeding in Amazon Elastic Beanstalk single container Docker environment

后端 未结 4 1859
野趣味
野趣味 2020-12-29 12:50

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 mi

4条回答
  •  失恋的感觉
    2020-12-29 13:36

    Update: This solution, though seemingly correct, doesn't work as intended (it seemed it was at first though). For reasons best explained in nmott's answer below. Will leave it here for posterity.


    I was able to get this working using container_commands via the .ebextensions directory config files. Learn more about container commands here. And I quote ...

    The commands in container_commands are processed in alphabetical order by name. They run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed. They also have access to environment variables such as your AWS security credentials. Additionally, you can use leader_only. One instance is chosen to be the leader in an Auto Scaling group. If the leader_only value is set to true, the command runs only on the instance that is marked as the leader.

    So, applying that knowledge ... the container_commands.config will be ...

    # .ebextensions/container_commands.config
    container_commands:
      01_migrate_db:
        command: docker exec `docker ps -l -q -f 'status=running'` rake db:migrate RAILS_ENV=production
        leader_only: true
        ignoreErrors: false
      02_seed_db:
        command: docker exec `docker ps -l -q -f 'status=running'` rake db:seed RAILS_ENV=production
        leader_only: true
        ignoreErrors: false
    

    That runs the migrations first and then seeds the database. We use docker exec [OPTIONS] CONTAINER_ID COMMAND [ARG...] which runs the appended COMMAND [ARG...] in the context of the existing container (not the host). And we get CONTAINER_ID by running docker ps -q.

提交回复
热议问题