How to stop/start docker containers in swarm mode without removing?

Viewed 15658

I am new in docker swarm mode. As you know it possible to start/stop docker container but i see no possibility to do this in swarm.
For example, I've deployed swarm and created new tasks with replica(2/2):

docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                                                               PORTS
2o3a6z30q9df        contactactivity     replicated          2/2                 myimage/live-springboot-myservice:19ff0be1f0087asd1dasdb52c345151e9985b4a5a2   *:111->1111/tcp

Is it possible to stop one of the swarm containers without removing it and bring it back in future?

P.S. I am just want to clarify that remove or recreate - is not the option in my case.

To make Q clear: If you stop container and then start - this container wont be a part of swarm. I cannot believe that in swarm mode there is no way to do simple restart or start/stop.

3 Answers

No, this is not possible 'out-of-the-box' and even if you manage to do in some hard way, it would be in total contrast with the Docker Swarm approach.

The steps you are trying to do are typical (manual) operation steps: with Swarm you demand this kind of operations to the orchestrator (UCP, the logic in the manager nodes).

Swarm start a new task, a container instance, everytime it decide to do and it is always a fresh booting container. This has two consequences that are docker best practice and, maybe, together can be a workaround for your problem:

  • you want your containers to start as fast as they can
  • if a container has something inside that you fear to lose or takes long to be created/collected, just persist it outside of the container itself (e.g. in a shared docker volume, where newly created container will find it ready at their startup)

I find it strange myself, but apparently the approach docker swarm wants is to delete the service and then re-deploy it from the stack or the command line. Additionally if the service is global with a restart on failure policy and the container is killed, two instances of it will be present (but not if it gets gracefully stopped).

Related