7

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?


user4794170
  • 203
  • 1
  • 4
  • 8

1 Answers1

7

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.

kojiro
  • 74,557
  • 19
  • 143
  • 201
  • Thanks for the reply. I will give this a shot. I'm guessing this won't work if the console script is supposed to run on a different host, since the socket is being passed in is from the Flask host. How would I go about passing in the socket from a different machine? – user4794170 Apr 30 '15 at 17:56
  • The trickiest part of that is figuring out how to identify that host, and of course, making sure that host is reachable from the Flask container via tcp. Then the Flask container needs the host client certs. It actually sounds like a nightmare – is there a good reason not to just make sure both run on the same host? – kojiro Apr 30 '15 at 18:50
  • Although sometimes may be needed, such a solution has freate security risks. See more here https://stackoverflow.com/questions/40844197/what-is-the-docker-security-risk-of-var-run-docker-sock – Charalamm May 17 '21 at 09:25