Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a database migration command when deploying a Docker container to AWS

Please bear with me. Pretty new to Docker.

I'm deploying Docker containers (detached) to an AWS EC2 registry using CodeDeploy. On deploy, the following command is run after setting some environmental variables etc:

exec docker run -d ${PORTS} -v cache-${CACHE_VOLUME} --env-file $(dirname $0)/docker.env --tty "${IMAGE}:${TAG}"

The container runs an image located and tagged in EC2 Container Service. No problems so far.

Since this is a PHP application (specifically a Symfony2 application) I would normally need to issue the following command to execute database migrations on deployment:

 php app/console doctrine:migrations:migrate --no-interaction

Now, is there any to run this command during "docker run..." while keeping the container running, or do I need to run another container specifically for this command?

Many thanks!

like image 580
Fredrik Avatar asked Jan 11 '17 13:01

Fredrik


People also ask

Can I deploy a Docker container to AWS?

AWS and Docker have collaborated to make a simplified developer experience that enables you to deploy and manage containers on Amazon ECS directly using Docker tools. You can now build and test your containers locally using Docker Desktop and Docker Compose, and then deploy them to Amazon ECS on Fargate.

What is are the AWS cloud service options for deploying a Docker image?

AWS services such as AWS Fargate, Amazon ECS, Amazon EKS, and AWS Batch make it easy to run and manage Docker containers at scale.


3 Answers

Just leaving this here for the next one that searches for this... ;-)

When using a recent version of Doctrine, there is a pretty handy parameter for this:

php bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration

The "allow-no-migration" parameter instructs doctrine not to throw an exception, when there is nothing to do...

like image 111
Alex Regenbogen Avatar answered Oct 19 '22 03:10

Alex Regenbogen


You need create entrypoint. This script runs at the container startup.

entrypoint.sh file:

#create or update db
./waitforit.sh <DB_HOST>:<DP_PORT> -t 30
php app/console doctrine:migrations:execute 

# start apache
apache2-foreground

wait for it it is a script waited when database is started up

like image 22
Bukharov Sergey Avatar answered Oct 19 '22 02:10

Bukharov Sergey


I do as follows:

docker-compose exec [containerID] ./app/console migrations:migrate --no-interaction
like image 1
enno.void Avatar answered Oct 19 '22 01:10

enno.void