How to stop a docker container which started with `--restart=always` - docker

Is there any way to stop a docker container which started with --restart=always like following
sudo docker run -it --restart=always <image_id>

Here's the mighty eagle that docker has recently included. :D
You can update docker container.
use sudo docker update --restart=no <container_id> to update --restart flag of the container.
Now you can stop the container.

You should be able to just use docker stop and then docker rm to make sure the container doesn't restart when the daemon restarts.
Your question is an issue on the docker github and someone has made some comments about to how to solve here
I'm not sure if it's intended behavior to restart a stopped container on daemon restart... but for sure docker rm would be all that is needed, no need to remove the image.

If you use docker stop or docker kill, you're manually stopping the container so it will not restart. You can do some tests about restart policies: restarting the docker daemon, rebooting your server, using a CMD inside a container and running an exit...
See this answer for more details:
https://serverfault.com/a/884823/381420

TL;DR
Also check docker swarm if there are any stacks that spin up containers there. Simply run docker stack ls followed by docker rm <stack_name>.
Longer version
This is not exactly an answer to your question, but I had a very similar issue where containers kept spinning up even if I ran docker update --restart=no <container_id>, docker stop <container_id> and docker rm <container_id>. These were some old containers, so I had no clue how I generated them.
After some Googling, I realized that it was a docker swarm stack that kept spinning up containers. By running docker stack ls followed by docker rm <stack_name>, I was able to stop the auto spin-up of the containers and thus remove them entirely.

Related

Starting docker service with a specific container on Ubuntu

I am using docker on ubuntu and I have some containers with different projects.
Each time I start docker (sudo service docker start), all my containers are started and I just would like to start one specific container. Is that possible?
The way your container restart is handled depends on the restart policy you use when your launch your container. This is passed through the --restart option to docker run
Basically:
no option or --restart no: Do not automatically restart the container when it exits. This is the default.
--restart on-failure[:max-retries] : Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.
--restart unless-stopped: Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
--restart always: Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
I used this command in order to stop launching automatically my containers when I start docker :
sudo docker update --restart=no container_name
and it works, thanks !
Yes, for sure... you can run something like :
sudo docker exec -it [interactive] frosty_brahmagupta [container name] bin/bash [here is what you want to run on docker ]
to see all your container you can run, even the inactive ones
docker ps -a

How to run docker container in production in Centos?

So on my server, I run my docker container as a daemon process via:
docker run -p 80:80 -td example
It seems to work fine - for now!
But, what if I restart my server or my docker container crashes? Then it doesn't work fine.
What is the best/conventional/standard way to keep my docker container running?
Thanks!
What you are looking for is an orchestrator. An orchestrator will manage your container life cycle for you. You might want to try the Docker orchestrator, Swarm. You can also check Kubernetes or Mesos.
You could also use Docker compose to make things easier.
As #wassim-dif pointed out you might want to use an orchestrator.
If you just want your docker container to restart automatically in case of failure and when you restart your server then you need to run it using the --restart flag, such as:
docker run -p 80:80 -td --restart=always example
This way, your container will restart automatically.

Why is docker-compose ps different from docker ps?

Why does docker compose create containers that are only accecible from docker-compose ps and that persist after killing running container ?
It doesn't.
docker ps only shows running containers, docker-compose ps shows all containers related to the current compose file, running and stopped. docker-compose kill just force stops the container and it can be restarted with docker-compose start, it will therefore be visible when running docker-compose ps but not docker ps.
To list all containers with docker use docker ps -a. To removed stopped containers related to a compose file run docker-compose rm, if you want to stop and remove all containers, have a look at docker-compose down.
docker-compose is the software wrapper around docker and there is not full support still. You can try to read a little bit about kubernets and mesos for the comparing different clusters built on top of docker or similar container systems.
article on blog about swarm and compose

Docker ps disappeared after restart system

Run docker container on a Linux system. From docker ps can see all the processes.
After restart the system and run docker ps can't see some containers, but use docker ps -a can see them. Is the container still running?
If you don't set the option --restart=always when run the docker container, these containers will not be started automatically, after you restart the system.
Restart policies (–restart)
always - Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.
Refer: docker run - Restart policies (–restart)

Docker swarm-manager displays old container information

I am using docker-machine with Google Compute Engine(GCE)
to run a
docker swarm cluster. I created a swarm successfully with 2
nodes
(swnd-01 & swnd-02) in the cluster. I created a daemon container
like this
in the swarm-manager environment:
docker run -d ubuntu /bin/bash
docker ps shows the container running on swnd-01. When I tried
executing a command over the container using docker exec I get the
error that container is not running while docker ps shows otherwise.
I ssh'ed into swnd-01 via docker-machine to come to know that container
exited as soon as it was created. I tried docker run command inside the
swnd-01 but it still exits. I don't understand the behavior.
Any suggestions will be thankfully received.
The reason it exits is that the /bin/bash command completes and a Docker container only runs as long as its main process (if you run such a container with the -it flags the process will keep running while the terminal is attached).
As to why the swarm manager thought the container was still running, I'm not sure. I guess there is a short delay while Swarm updates the status of everything.

Resources