[Solved]-Deploy-time commands inside Docker on Elastic Beanstalk

3๐Ÿ‘

You could add a post deploy script in your .ebextensions like so:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/10_migrate.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      if [ -f /tmp/leader_only ]; then
        echo "Running Migrations"
        container_id=`docker ps -q --no-trunc --filter label="com.amazonaws.ecs.container-name=MY_CONTAINER_NAME" | head -n 1`
        docker inspect $container_id
        docker exec $container_id /entrypoint.sh python manage.py migrate --noinput
      fi
  "/opt/elasticbeanstalk/hooks/appdeploy/post/90_rm_leader_only.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      if [ -f /tmp/leader_only ]; then
        echo "Deleting leader_only file"
        rm /tmp/leader_only
      fi
container_commands:
  01_touch_the_leader:
    command: |
      #!/usr/bin/env bash
      touch /tmp/leader_only
    leader_only: true

I have included the leader only bit in case you might be using a load balanced environment, it wonโ€™t run migrations on multiple servers at once. This can also be used for the collectstatic command.

๐Ÿ‘คA. J. Parr

0๐Ÿ‘

I think using ENTRYPOINT in Dockerfile might solve your problem.

Either you can have a script (which takes care of database migrations, starting a webserver and other sanity checks) baked within your container-image and call that script in Dockerfile ENTRYPOINT.

ADD run_all /opt/bin/
ENTRYPOINT ["/opt/bin/run_all"]

This way the script is executed only when you create a container of that image.

Or run the both the commands (Python manage.py migrate && httpd start) using ENTRYPOINT

๐Ÿ‘คVDR

0๐Ÿ‘

Know this is an old post, but this also works for me, similar approach to what you mentioned as tacky:

    container_commands:
      01-init-elb:
        command: "docker exec docker_js_api_1 bash -c 'php artisan optimize:clear && php artisan migrate --force'"
๐Ÿ‘คyogibear

Leave a comment