Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Docker container only after another container is exited

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?

like image 688
Victor Bocharsky Avatar asked Dec 15 '16 11:12

Victor Bocharsky


People also ask

How do I keep docker containers running after exit?

Dockerfile Command to Keep the Container Running Method 1: You can use the -t (pseudo-tty) docker parameter to keep the container running. Method 2: You can run the container directly passing the tail command via arguments as shown below. Method 3: Another method is to execute a sleep command to infinity.

Can we start exited container in docker?

Alternatively, if the container just exited we can easily start it in a single step. For this, we use the command, Here `docker ps -q -l` replaces the container ID of the last created Docker container. Additionally, we can also use options like -i to start the container in interactive mode.

What is restart unless stopped docker?

This means that with the unless-stopped restart policy, if the container was running before the reboot, the container would be restarted once the system restarted. When an application within a Docker container exits, that container will be also halted.


1 Answers

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
like image 137
İsmail Atkurt Avatar answered Sep 24 '22 19:09

İsmail Atkurt