Search code examples
dockerservicecontainersswarm

Swarm | Start a service to execute just one time then be deleted


I have a Swarm cluster and I have a service that will just create a container, execute one script and then delete the container. And currently it works fine, the container execute the script then delete him-self after the execution. But after a few seconds, the service restart the container and execute again the script. But I would like to delete the service after the first execution, like that the service don't do the job several times. 

My only idea is to in my first script that launch the service, I put a timer, like 4 seconds and after that i just delete the service in the script like that :

#!/bin/bash

docker stack deploy -c docker-compose.yaml database-preapre
sleep 4
docker service rm database-prepare

But if one day, the service make more time to execute his script and I cut the service when he is running. Or if the service execute itself one time real fast and then after 3 seconds restart it-self for the second execution. I don't want to cut him...


Solution

  • To resolve this issue, I just adjust some things in my script. First, in the script that create the temporary container, I just sleep 10 seconds and I destroy the container. (10 seconds is very large but i want to be sure the container have the time to do the job)

    Secondly, when the container finished executing the entrypoint script he will just deleted himself and restart (cause swarm restart it), but I don't want that my script go Up, down and Up and be cut during the second execution and destroy the job. So I add a sleep timer to the script that my temporary container run, so he will don't go down and up and down and up ... He executes his job and then wait.