使用 docker swarm 执行单个容器而不是“服务" [英] Using docker swarm to execute singular containers rather than "services"

查看:55
本文介绍了使用 docker swarm 执行单个容器而不是“服务"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢拥有一组 docker 机器来执行 docker 服务的概念.我也喜欢单个 docker 容器不具备的附加功能(例如 docker secret).

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).

但我真的不需要长期服务.我的用例是简单地执行一个 bash 脚本来使用 docker swarm 接收任意数量的有限命令,并在同一个 docker 映像上将每个命令作为正在运行的 docker 容器执行,同时使用加载了 docker swarm 秘密的秘密.

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.

我可以这样做吗?
我不想让这个容器长时间运行".我希望它运行,然后在加载到容器中的 bash 脚本完成后退出.

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.

推荐答案

您可以应用Docker Swarm 上的一次性容器"来自 alex ellis.

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

您仍然需要创建一个服务,但是使用正确的重启策略.
例如,对于快速 Web 服务器:

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

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

(--restart-条件,而不是--restart-policy,如ethergeist 评论)

(--restart-condition, not --restart-policy, as commented by ethergeist)

因此,通过将重启条件设置为 0,容器将作为(任务)调度到 swarm 中的某个位置.容器将执行,然后在准备就绪时退出.

So by setting a restart condition 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.

对于最后一部分,使用他的工具:alexellis/jaas.

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

运行您的第一个一次性容器:

Run your first one-shot container:

# jaas -rm -image alexellis2/cows:latest

-rm 标志删除用于运行容器的 Swarm 服务.

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

您的容器的退出代码也将可用,您可以使用 echo $? 进行检查.

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

这篇关于使用 docker swarm 执行单个容器而不是“服务"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆