Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a docker container based on condition

Problem-

I have a docker-compose.yml with 6 services. When i execute docker-compose up, all 6 containers gets started but i need 2 containers to start its work initially and rest 4 containers based on conditions.

Description-

6 services in compose(2 services for all users & 4 services for 2 users):-

-2 service for all users

  • I'm mounting login required pages in first container

  • I maintained database for login accounts in 2nd container

-2 services for each users

  • Based on the user, i redirect from login page to different applications
  • at this point, i mounted his/her application in one container and database in another.

Is there a way, when i run docker-compose up -it should start only 2 services common to all users and based on the user login i need to start other 2 services where his/her application mounted ???

like image 243
Shashank G Avatar asked Aug 25 '16 06:08

Shashank G


2 Answers

Doing this based upon user alone would require logic outside of Docker Compose, but you may be able to configure .

Extending Services

You might be able to use two Docker Compose files to accomplish similar results.

  • 2 services for all users - docker-compose.yml
  • 4 services for 2 users - docker-compose.admin.yml

In docker-compose.admin.yml would compliment or override settings in the original compose file, as well as additional containers. You can use overrides to change anything including ENV vars or volumes sent to the containers.

When running docker-compose up the two services for all users will start.

You can run all six containers by running

docker-compose -f docker-compose.yml -f docker-compose.admin.yml up

Aliases

Setting up an alias in your .bashrc would make this much easier to use on a daily basis.

# For regular users
alias comp="docker-compose"

# For two special users
alias comp="docker-compose -f docker-compose.yml -f docker-compose.admin.yml"

With this setup all users can use comp up or comp down and get the appropriate set of containers.

Docs

See details in the Docker Docs:

Extending Service in Compose

like image 105
DevOps Dan Avatar answered Sep 20 '22 06:09

DevOps Dan


This can now be solved neatly, with a recent addition to docker-compose, compose profiles (at least for the service part, not sure for the volumes). See documentation here.

like image 23
CharlesB Avatar answered Sep 21 '22 06:09

CharlesB