<p>I am brand new to Docker, so I apologize for any ignorance.</p>

<p>I have a web API which needs to trigger a Python script to run. The script is very resource intensive (CPU and RAM), so it needs to run on a different server than the API server. I am planning to run this script within a Docker container. I am also planning to use Kubernates.</p>

<p>My API will build a container from an image, and then the Python script needs to run once the image is up and running.</p>

<p>How can I trigger the script to run? Would it make sense to use Rundeck for this? Or would it make more sense to simply use SSH? Or something else?</p>

<p>Off the top of my head I can think of two approaches, both of which I've used:</p>

<ul>
<li>If you want the script to start immediately, use <code>CMD</code> or <code>ENTRYPOINT</code> in your Dockerfile to kick off the script at start of the container</li>
</ul>

<p>So, abstracted, something like this</p>

<pre><code>FROM python

COPY myscript.py /tmp

CMD ["python", "/tmp/myscript.py"]
</code></pre>

<ul>
<li>If you want the script to only run when triggered, say, by an event of web request, "wrap" the Python code inside something like flask, bottle or uwsgi.</li>
</ul>

<p>This could look like this:</p>

<p>Your wrapper script is <code>mywrapper.py</code> which has:</p>

<pre><code>#!usr/bin/env python
import bottle
app = bottle.Bottle()
@app.route('/')
def main():
   call_my_python_code()
</code></pre>

<p>In that case your <code>Dockerfile</code> may set this up through exposing a port and then starting the web server:</p>

<pre><code>FROM python
COPY wrapper.py runserver.sh myscript.py /tmp

EXPOSE 80
CMD ["/bin/bash", "-x", "/tmp/runserver.sh" ]
</code></pre>

<p>With <code>runserver.sh</code> just setting up the server bit:</p>

<pre><code>#!/bin/bash
set -e
exec uwsgi --http 0.0.0.0:80 --wsgi-file wrapper.py --callable app
</code></pre>
