简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM