9

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.

Sergey Gorshkov
  • 109
  • 1
  • 1
  • 3
  • What you cannot believe is completely right thing to be doing for swarm. Docker containers are there for immutable style infrastructure where it really doesn't matter if a container dies. Because swarm will kill it and spin up a new one. If that was not the case the system won't remain healthy when something bad happens. For you to except a container to be stop and re-used means you are not using docker analogy right – Tarun Lalwani Aug 03 '17 at 12:18
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Aug 03 '17 at 12:54
  • Question is "Is it possible to stop one of the swarm containers without removing it and bring it back in future?", and it is a development related question indeed. As I answered the correct answer is "no", swarm is not designed to allow this. There are workarounds but they are worst practices. – mrq Feb 05 '21 at 20:46

3 Answers3

8

List the containers running for the service & stop them for example docker ps | grep service-name and then stop the container by using docker stop <ID> from above command.

                                 OR

If you want to stop all containers then just scale down the service to 0 like below docker service scale servicename=0 bring them back using docker service scale servicename=N

Harshad Yeola
  • 1,060
  • 9
  • 10
  • Yes 'stop' works, but it makes not possible to start it in future. Scalling will remove all containers, it is not a good solution on production. – Sergey Gorshkov Aug 03 '17 at 10:14
  • after you `stop` the container you can start it using `docker start `. – Harshad Yeola Aug 03 '17 at 10:51
  • Yes it will be started,but wont be part of swarm. – Sergey Gorshkov Aug 03 '17 at 11:08
  • You can always re-attach to the swarm `docker swarm join-token worker` https://docs.docker.com/engine/swarm/swarm-tutorial/add-nodes/ – isaac weathers Aug 15 '18 at 21:19
  • 1
    Docker swarm join deals with adding a new worker node to a swarm cluster, not reattaching a standalone Docker container back into swarm. – Toddinpal Feb 28 '19 at 17:52
  • 2
    doing `docker service scale servicename=0` erases data! – Captain Levi Mar 11 '19 at 07:36
  • 1
    This answer is completely wrong. 1. Stopping and starting directly a container removes it from the service, making it useless in most cases and, in fact, this is not done "in swarm" like the question asked. 2. Scaling a service to zero and then to (N=) 1 does not "bring back" the container, a completely new one is created, so no "start/stop" in this case, too. – mrq Feb 06 '21 at 17:55
1

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)
mrq
  • 460
  • 2
  • 12
0

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

John White
  • 917
  • 1
  • 12
  • 26