Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - Run Container from Inside Container

Tags:

python

docker

I have two applications:

  • a Python console script that does a short(ish) task and exits
  • a Flask "frontend" for starting the console app by passing it command line arguments

Currently, the Flask project carries a copy of the console script and runs it using subprocess when necessary. This works great in a Docker container but they are too tightly coupled. There are situations where I'd like to run the console script from the command line.

I'd like to separate the two applications into separate containers. To make this work, the Flask application needs to be able to start the console script in a separate container (which could be on a different machine). Ideally, I'd like to not have to run the console script container inside the Flask container, so that only one process runs per container. Plus I'll need to be able to pass the console script command line arguments.


Q: How can I spawn a container with a short lived task from inside a container?


like image 729
user4794170 Avatar asked Apr 30 '15 17:04

user4794170


People also ask

Can we run a docker container inside Docker container?

Yes, we can run docker in docker, we'll need to attach the unix socket /var/run/docker. sock on which the docker daemon listens by default as volume to the parent docker using -v /var/run/docker.

Can a docker container run another container?

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.

How can I run another process inside a running container?

Use a process manager which can run multiple processes: You can set the container's entrypoint to a specialised program which is capable of running and managing multiple processes. One example of this is supervisord. You can use supervisord as your container entrypoint, which will then load the services that you need.


1 Answers

You can just give the container access to execute docker commands. It will either need direct access to the docker socket or it will need the various tcp environment variables and files (client certs, etc). Obviously it will need a docker client installed on the container as well.

A simple example of a container that can execute docker commands on the host:

docker run -v /var/run/docker.sock:/var/run/docker.sock your_image

It's important to note that this is not the same as running a docker daemon in a container. For that you need a solution like jpetazzo/dind.

like image 68
kojiro Avatar answered Oct 04 '22 07:10

kojiro