Starting a container only after the previous container is Running

2/25/2017

Lets say I have 3 containers X,Y and Z. I want container Y to start only after container X is running, and container Z to start only after Y is running.

How do I do this?

-- Kunal Sehegal
docker
kubernetes

2 Answers

2/26/2017

You can use docker-compose file with the depends_on option to control container start order

Here is how the docker-compose.yml for your example would look like:

version: '3'
services:
  X:
    image: X_image

  Y:
    image: Y_image
    depends_on: X

  Z:
    image: Z_image
    depends_on: Y

Note: depends_on will not wait for X to be “ready” before starting Y - only until it's running. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

-- A.A.
Source: StackOverflow

2/25/2017

You should consider to redesign your application parts if they have such heavy dependencies, then they should maybe not be split to different containers or you should use some decoupeling to avoid

What you could do is to:

  1. Have one ore more init containers
  2. Have a entrypoint script within each of the containers which blocks until the dependency is fulfilled
  3. Implement something outside the cluster which controls which pods are started.
  4. Write a custom controller
-- pagid
Source: StackOverflow