<p>I've got a basic architecture set up in Kubernetes, a Laravel container for my application level code, and a Mysql container for my database. And I'm looking to implement a code compiling API service (as a seperate container) which accepts user generated code which I then run a docker container to compile the code and return the output to the user. </p>

<p>There's some pretty raw implementations online but most of them use docker as a method of running user generated code compiling in an isolated environment (as you should) but the application itself is not hosted using containers or a container management system.</p>

<p>Questions is, how can I spin up docker containers to handle a task and then return the output to my Laravel API container before shutting the container down? </p>

<p>Apparently, running a docker container inside a docker container is not best practice.</p>

<p>The process:</p>

<ol>
<li>User sends a post request to Laravel API container</li>
<li>Laravel API container will take the request and run a docker container to compile code</li>
<li>Temporary docker container will return compiled output to Laravel API container before 
shutting down.</li>
<li>Laravel API container will return compiled response to end user.</li>
</ol>

<p>I'm running my app in a Kubernetes cluster, and a Docker/Kubernetes solution is needed. I rather not have to run raw commands of starting a Docker container in my application level code but have a more higher level solution. </p>

<p>You can use Kubernetes job resource to perform this kind of task. </p>

<p>The Jobs objects can be spawned to run a process inside and can be set to be automatically terminated afterwards. A job in Kubernetes is a supervisor for pods carrying out batch processes, that is, a process that runs for a certain time to completion. You are able to run multiple pod instances inside one job (parallel or sequentially). </p>

<p>Check this <a href="http://kubernetesbyexample.com/jobs/" rel="nofollow noreferrer">page</a> for more details about the jobs. </p>

<p>So basically your flow should look like this: </p>

<ol>
<li>User sends request for Laravel API container </li>
<li>Laravel API container needs to interact with the API server in order to create the job.</li>
<li>Pod inside the job will compile code and after compilation, will send a request to the Laravel API pod to deliver the compiled binary.</li>
</ol>

<p>The delivery of the binary should be coded by the user</p>

<ol start="4">
<li>Laravel API container will return compiled response to user. </li>
</ol>

<p><a href="https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster" rel="nofollow noreferrer">This documentation link</a> shows how to connect to the API, especially the section <a href="https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod" rel="nofollow noreferrer">Accessing the API from a Pod</a></p>
