Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to start a stopped container from another container

There are two containers A and B. Once container A starts, one process will be executed, then the container will stop. Container B is just an web application (say expressjs). Is it possible to kickstart A from container B ?

like image 544
tony.0919 Avatar asked Sep 13 '16 11:09

tony.0919


People also ask

Can you start a stopped container in docker?

When we try to run /bin/sh on a stopped container using docker exec , Docker will throw a No such container error. We have to transform the stopped Docker container into a new Docker image before we can inspect the internals of the container. We can transform a container into a Docker image using the commit command.

How do I stop a container in Docker?

The container has been stopped using docker stop: You can manually stop a container using the docker stop command. The Docker daemon has restarted, and it terminated and restarted the container: Docker can restart containers if you need it to.

Can I restart a specific container in a container group?

You can't restart a specific container in the group. After you manually restart a container group, the container group runs according to the configured restart policy. Learn more about restart policy settings in Azure Container Instances.

How do I prevent a container from stopping?

There are a few ways that you can prevent a container from stopping - all of these are simply ways to keep a process alive: Run a long-lived service inside the container: Most containers run a long-living service, like a database or a web server. These processes keep running until they receive a shutdown signal.

Why do containers stop running?

Containers are not like virtual machines. You probably know that VMs are like virtual computers that keep running until you tell them to stop. But containers don’t run an entire operating system. They only run (contain) a specific process. When this process finishes, the container exits. So why does a container stop? 1. Look at the logs 2.


2 Answers

It is possible to grant a container access to docker so that it can spawn other containers on your host. You do this by exposing the docker socket inside the container, e.g:

docker run -v /var/run/docker.sock:/var/run/docker.sock --name containerB myimage ... 

Now, if you have the docker client available inside the container, you will be able to control the docker daemon on your host and use that to spawn your "container A".

Before trying this approach, you should be aware of the security considerations: access to docker is the same as having root access on the host, which means if your web application has a remote compromise you have just handed the keys to your host to the attackers. This is described more fully in this article.

like image 123
larsks Avatar answered Sep 21 '22 17:09

larsks


It is possible by mounting the docker socket.

Container A
It will print the time to the stdout (and its logs) and exit.

docker run --name contA ubuntu date 

Container B
The trick is to mount the host's docker socket then install the docker client on the container. It will then interact with the daemon just as if you were using docker from the host. Once docker is installed, it simply restart container A every 5 seconds.

docker run --name contB -v /var/run/docker.sock:/var/run/docker.sock ubuntu bash -c " apt-get update && apt-get install -y curl && curl -sSL https://get.docker.com/ | sh &&  watch --interval 5 docker restart contA" 

You can see that contA is being called by looking at its logs

docker logs contA 

That said, Docker is really meant for long running services. There's some talk over at the Docker github issues about specifying short lived "job" services for things like maintenance, cron jobs, etc, but nothing has been decided, much less coded. So it's best to build your system so that containers are up and stay up.

like image 40
Bernard Avatar answered Sep 18 '22 17:09

Bernard