14

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!

Fredrik
  • 1,741
  • 4
  • 24
  • 40

3 Answers3

17

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

entrypoint.sh file:

# create or update db
./wait-for-it.sh <DB_HOST>:<DB_PORT> -t 30
php app/console doctrine:migrations:execute 

# start apache
apache2-foreground

wait-for-it is a script that waits until the database is started.

migu
  • 1,371
  • 1
  • 14
  • 23
Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
8

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...

Alex Regenbogen
  • 568
  • 4
  • 5
1

I do as follows:

docker-compose exec [containerID] ./app/console migrations:migrate --no-interaction
enno.void
  • 6,242
  • 4
  • 25
  • 42
  • But how can the deployment script know the [containerId]? Or are you running it manually? – Fredrik Jan 11 '17 at 15:41
  • 1
    I do it manually. But i think u could do a "docker ps" in your deployment-script and grep the containerId you would apply to... – enno.void Jan 11 '17 at 15:56
  • 1
    This mixes two possible solutions. `docker-compose run [service]` would spin up a new docker container. You can also use `docker ps` to find out the container id of the running php container and use `docker exec [containerID] [migrationCommand]` to run the command inside the running container instead of spinning up a new one. – eawenden Jan 11 '17 at 16:07
  • @mr.void but you can't do using Swarm mode – yuklia Apr 18 '18 at 15:50