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

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.

Related

Restarting docker container in swarm mode

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

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>

Is there any difference between docker start and docker container start?

I was wondering if there is any difference between
docker start <container name>
and
docker container start <container name>
I personally always use docker container start though, because that is the method that was suggested to run a stopped container. What would be the difference if I use docker start instead?
There is no difference between docker container start and docker start.
Over time, the docker cli has become more organized so that, for example, there are separate docker container inspect and docker image inspect commands. Earlier, there was a single command that would do both depending on the arguments, which could be confusing. There are a number of commands that are there for historic reasons (like docker ps, docker inspect, etc) that duplicate functionality that is now also available via subcommands of docker container, docker image, and so on.

Docker: Difference between `docker run` and `docker service`

I am very new to docker , just started venturing into this. I read online about this. I came to know of the following commands of docker which is: docker run and docker service. As I understood , with docker run we are spinning a new container. However I am not clear what docker service do? Does it spin container in a Swarm?
Can anyone help understand in simple to understand?
The docker run command creates and starts a container on the local docker host.
A docker "service" is one or more containers with the same configuration running under docker's swarm mode. It's similar to docker run in that you spin up a container. The difference is that you now have orchestration. That orchestration restarts your container if it stops, finds the appropriate node to run the container on based on your constraints, scale your service up or down, allows you to use the mesh networking and a VIP to discover your service, and perform rolling updates to minimize the risk of an outage during a change to your running application.
Docker Run vs Docker service
docker run:
we can create number of containers with different images.
docker service:
we can create number of containers with same image in a single command line.
SYNTAX:
docker service create --name service-name --network network-name --replicas number-of-containers image-name
EXAMPLE:
docker service create --name service1 --network swarm-net --replicas 5 redis

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

Resources