编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

使用泊坞窗群来执行单个容器而不是“服务".:Using docker swarm to execute singular containers rather than "services"

AceMood 云计算 2022-5-10 14:48 6人围观

腾讯云服务器
使用泊坞窗群来执行单个容器而不是“服务".的处理方法

我真的很喜欢拥有可用于执行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.

问题解答

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

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-policy=none --name crawler1 -e url=http://blog.alexellis.io -d crawl_site alexellis2/href-counter 

因此,通过将重新启动策略设置为0,可以将容器作为(任务)安排在群集中的某个位置.容器将执行,然后准备就绪-它将退出.

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.

最后一部分,使用他的工具: 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 $?

这篇关于使用泊坞窗群来执行单个容器而不是“服务".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^