I have two applications:
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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With