6

Let's suppose I have 2 containers: composer and php, which share the same code base, i.e. the same volume. And I want to manage them with a single docker-compose command like docker-compose up.

So the question is How can I start these containers one by one, not at the same time? I mean, start my php container only after composer one is exited, i.e. composer container should install all vendors for my php container and exit, then and only then php container should start a built-in PHP web server with already installed vendors.

P.S. Now I get the behavior that both containers start at the same time. So my php container is trying to start web server without vendors, while composer container is trying to install these vendors.

Is there a good solution for this case?

Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
  • 2
    you can write a script, checking what `docker ps --filter "name=composer"` or the shorter `docker ps -q --filter "name=composer"`returns. If you do not get a string of numbers and letters (for the last one), it means it exited. You can also listen to `docker events` such as `docker events --filter 'container=7805c1d35632'` and look for "container die" or such. I think your best bet is something like `docker events --filter 'container=7805c1d35632' --filter 'event=stop'` check the doc https://docs.docker.com/engine/reference/commandline/events/ – user2915097 Dec 15 '16 at 12:10
  • Hey @user2915097 , thanks a lot for this information. It really makes sense, however, the only problem I see that I should run this filtering script on my host machine, not inside the container. But can I do the similar check inside a container? Then I can run an infinite loop which checks every 2 seconds if `composer` container has died and then will finish the infinite loop and run built-in web server. Do you have any thoughts about it? – Victor Bocharsky Dec 15 '16 at 13:18
  • 1
    Yes you can, you need, when you launch your container, to mount the docker socket, like `docker run -v /var/run/docker.sock:/var/run/docker.sock...` Now inside your container, you can use the docker commands – user2915097 Dec 15 '16 at 13:35
  • you can also use signals, see http://blog.dixo.net/2015/02/sending-signals-from-one-docker-container-to-another/ I do not like the idea of checking every 2 seconds, I think there should be better options – user2915097 Dec 15 '16 at 13:37
  • See also the idea of AmanicA http://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y and wait-for-it https://github.com/vishnubob/wait-for-it – user2915097 Dec 15 '16 at 13:40
  • Ah, that's great! Thank you very much for these links, I think I need some time to handle them ) – Victor Bocharsky Dec 15 '16 at 14:01
  • How about proper CI pipeline in Jenkins? – rkosegi Oct 31 '18 at 10:35

1 Answers1

6

There are couple of things can help you;

  • depends_on: it does not wait for another service to finish, it only waits until the other services starts. see: depends_on
  • health_check: it does not start the service until the command returns success. see: healthcheck
  • create a shell script to order of initialisation of your containers/services. see: startup order
İsmail Atkurt
  • 1,360
  • 1
  • 12
  • 17