How can I run a Python script in a remote Docker container?

5/19/2016

I am brand new to Docker, so I apologize for any ignorance.

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.

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

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?

-- Chad Johnson
docker
kubernetes
rundeck

1 Answer

5/19/2016

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

  • If you want the script to start immediately, use CMD or ENTRYPOINT in your Dockerfile to kick off the script at start of the container

So, abstracted, something like this

FROM python

COPY myscript.py /tmp

CMD ["python", "/tmp/myscript.py"]
  • 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.

This could look like this:

Your wrapper script is mywrapper.py which has:

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

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

FROM python
COPY wrapper.py runserver.sh myscript.py /tmp

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

With runserver.sh just setting up the server bit:

#!/bin/bash
set -e
exec uwsgi --http 0.0.0.0:80 --wsgi-file wrapper.py --callable app
-- Marakai
Source: StackOverflow