Start a docker container based on condition

那年仲夏 提交于 2020-08-04 23:24:22

问题


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


回答1:


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




回答2:


I often have some services that are no-ops based on the environment.

If you add this you will pass through the external user to the container:

    environment:
      - USER=${USER}
    entrypoint: ./my_entrypoint_conditional_wrapper.sh

In the entrypoint script, check the USER variable, and if the service should NOT start, simply exit 0, otherwise run the original entrypoint.

If the image you are using is one you didn't build, you can always use volumes to pass-through just the script:

    volumes:
      - ./my_entrypoint_conditional_wrapper.sh: /tmp/./my_entrypoint_conditional_wrapper.sh
    entrypoint: /tmp/my_entrypoint_conditional_wrapper.sh


来源:https://stackoverflow.com/questions/39138488/start-a-docker-container-based-on-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!