Using docker swarm to execute singular containers rather than “services”

牧云@^-^@ 提交于 2021-02-07 04:01:58

问题


I really enjoy the concept of having a cluster of docker machines available to execute docker services. I also like the additional features not available to singular docker containers (such as docker secret).

But I really have no need for long-standing services. My use case is to simply execute a bash script to use the docker swarm to take in an arbitrary number of finite commands, and execute each as a running docker container on the same docker image, while using the secrets loaded up with docker swarm's secrets.

Can I do this?
I do not want to have this container be "long running". I want it to run, and then exit with the output when the bash script loaded into the container is finished.


回答1:


You can apply the ideas presented in "One-shot containers on Docker Swarm" from alex ellis.

You still neeeds to create a service, but with the right restart policy.
For instance, for a quick web server:

docker service create --restart-policy=none --name crawler1 -e url=http://blog.alexellis.io -d crawl_site alexellis2/href-counter

So by setting a restart policy of 0, the container will be scheduled somewhere in the swarm as a (task). The container will execute and then when ready - it will exit.

If the container fails to start for a valid reason then the restart policy will mean the application code never executes. It would also be ideal if we could immediately return the exit code (if non-zero) and the accompanying log output, too.

For the last part, use his tool: alexellis/jaas.

Run your first one-shot container:

# jaas -rm -image alexellis2/cows:latest

The -rm flag removes the Swarm service that was used to run your container.

The exit code from your container will also be available, you can check it with echo $?



来源:https://stackoverflow.com/questions/46313541/using-docker-swarm-to-execute-singular-containers-rather-than-services

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