Execute data migrations on container deployment in ECS

Hi all,

I am trying to organize the entire deployment from into Amazon ECS. One thing that I am still not clear about is data migrations on deployment (or, in fact, any one-time execution that I need to run on deployment, such as cache clear etc…)
Is there a way that I can setup docker image to execute a one-time command on startup and then proceed to the main task execution?
The only way I can think about doing it, is by embedding this command into the main execution of a task. However, there are two problems:

  1. ECS will try to restart the container if it crashes for whatever reason.
  2. I am using ‘supervisord’ as a daemon to make sure that all of my services are running. So supervisord is the main task of a container and this is the only this I need to be running as a top level service in container.

Given the two above, if I, for example, build a shell script that will do what I need to once and then start supervisord daemon, every time that container or supervisord crashes the script will be executed again, even though it is not new version deployment and the only thing that needs to be done is restart supervisord daemon.

I am missing something?


1 Like


You could implement an ENTRYPOINT script that could run one-shot commands at startup. You would have to make it idempotent so that it doesn’t run things that have already been run. The ENTRYPOINT is handy for making sanity checks in the environment, and doing stuff like running DB migrations. The last thing that an ENTRYPOINT does is call the CMD of the container.

Here’s an example entrypoint script from the wordpress official image: https://github.com/docker-library/wordpress/blob/master/docker-entrypoint.sh

It will make sure that the DB credentials passed in to the container work, create the wordpress database if needed, and install wordpress on first launch. Finally, it actually runs wordpress.



1 Like

That is what I assumed. Thank you sir.

@shurikag Know this is later but here’s another answer to help others in the future. I actually think you should run a one off ECS Task for this. The ufo automates this exact use case: http://ufoships.com/docs/migrations/ Though you do not have to use ufo, conception you simply want to run a 1 off task and specify the command as the migration command.