Restarting docker container in swarm mode - docker

I am running a docker service in swarm mode. When I want to restart it, there are 2 options I know:
from swarm manager: docker service scale myservice=0 then docker service scale myservice=1
from server running the server: docker ps, take the container id of my service and do docker stop <containerId>
And this works fine. However, if I go with option #2 and instead of docker stop I write docker restart it will restart the current instance, but because being in swarm mode it will also start a new one. So in the end I will end up having 2 of the same service, even though in my compose I have specified I want only 1 replica.
Is there any way to prevent docker restart and the docker swarm to start a 2nd service while one is already there?
I am using docker 18.09.2 on ubuntu 18.04

Related

How to stop docker running inside docker when the outer container dies

I'm using docker swarm to run containers. I need some of my containers to have privileged rights, and the way achieve this with docker swarm is to use Docker in Docker. So my services utilize the docker image and then start the image I want to use with privileged rights when they start. However, when I remove my services, the inner docker containers are left running.
Is there a way to remove these dangling containers automatically when the service that starts them is removed? I can manually kill the containers by checking their ids with docker ps and then use docker kill, but that it is far from optimal.

Docker Container restart after VM reboot

we have deployed docker container and the restart policy is --restart unless-stopped. This will start the container after VM reboot. The docker service is also enabled to start the service after reboot.
The problem is whenever reboot happen, the list of containers and images all are gone. Solution for this is to restart the docker service. so after this container is coming up.
so question is why do we need to restart the docker service even after it is enabled to start after reboot?
appreciate help on this?
I don't know the truly internals of Docker Engine, but I make my assumptions:
On Virtual Machine (or native host) restart, the docker engine is stopped, so the containers receive a "stop" signal too (in background a systemctl stop docker will be performed)
What you are doing with systemctl restart docker (or similar command) is sending a "restart" signal to the docker engine, not a "stop" one.
You should use --restart always if you want to be sure that the containers are restarted automatically when the VM is freshly started/restarted.

Rancher exited after host machine power off and reboot

Can someone explain me why after host machine reboot all containers are exited?How can i find a way to restart containers specially rancher containers and everything be as before?
You can use the docker restart policy to control container automatic startup. Check Start containers automatically for more info.
As for the current container that are stopped you need to start them manually:
docker ps -a
docker start <container>

Difference between docker restart & docker-compose restart

I'm using docker-compose.yml to setup docker containers. And I have started the services using docker-compose up -d.
Now every time I deploy the application to the server I need to restart one of the services.
Previously I used to run the container without docker-compose using just the docker run command like this: docker run --name test-mvn -v "$(pwd)":/usr/src/app test/mvn-spring-boot -d.
And to restart the container I used to do docker restart test-mvn.
But now there are two options out there docker-compose restart and docker restart. I'm not sure which one I should prefer.
I want to know what is the difference between these two options and which one I should use in my case.
With docker-compose you manage a services, typically constituting multiple containers, while docker manages individual containers. Thus docker-compose restart will restart all the containers of a service and docker restart only the given containers.
Assuming "one of the services" in your question refers to an individual container I would suggest docker restart.

How do we set docker containers to restart on reboot and on-failure?

I would like the docker containers to come up
On host reboot or when docker is restarted on host
On failure with maximum retries .
I am aware that the docker restart policies unless-stopped and always are in the same direction but I would want them to fail after maximum retries.
Thinking of below steps as a solution
No restart policy on docker containers
systemd-docker to restart containers on-failure and enabled on reboot.
is there a better way to do this?
I would like the docker containers to come up On host reboot or when docker is restarted on host
This is not a complete answer, but know that docker 1.12 will add a daemonless container mode
(PR 23213):
Daemonless Containers
Starting with Docker 1.12 containers can run without Docker or containerd running.
This allows the Docker daemon to exit, be upgraded, or recover from a crash without affecting running containers on the system.
To enable this functionality you need to add the --live-restore flag when launching dockerd. This will ensure that Docker does not kill containers on graceful shutdown or on restart leaving the containers running.

Resources