1

我是 Docker 的新手,所以我为任何无知道歉。

我有一个需要触发 Python 脚本运行的 Web API。该脚本非常占用资源(CPU 和 RAM),因此它需要在与 API 服务器不同的服务器上运行。我打算在 Docker 容器中运行这个脚本。我也打算使用 Kubernetes。

我的 API 将从图像构建一个容器,然后 Python 脚本需要在图像启动并运行后运行。

如何触发脚本运行?为此使用 Rundeck 有意义吗?或者简单地使用 SSH 会更有意义吗?或者是其他东西?

4

1 回答 1

2

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
于 2016-05-19T01:15:19.497 回答