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.
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
- Django loading data from fixture after backward migration / loaddata is using model schema not database schema
- Saving Django ModelForm with a ForeignKey
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'"