IBM Cloud Functions: How to run a Docker function?

600 views Asked by At
FROM python:3.7
COPY ./src /data/python
WORKDIR /data/python
RUN pip install --no-cache-dir flask
EXPOSE 8080
CMD ["python", "main.py"]
  • main.py
import os
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return {'body': os.environ.items()}


def run():
    app.run(host='0.0.0.0', port=8080)


if __name__ == '__main__':
    run()

click invoke result

[
  "2021-01-29T09:53:30.727847Z    stdout: * Serving Flask app \"main\" (lazy loading)",
  "2021-01-29T09:53:30.727905Z    stdout: * Environment: production",
  "2021-01-29T09:53:30.727913Z    stdout: WARNING: This is a development server. Do not use it in a production deployment.",
  "2021-01-29T09:53:30.727918Z    stdout: Use a production WSGI server instead.",
  "2021-01-29T09:53:30.727923Z    stdout: * Debug mode: off",
  "2021-01-29T09:53:30.731130Z    stderr: * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)",
  "2021-01-29T09:53:30.747035Z    stderr: 172.30.139.167 - - [29/Jan/2021 09:53:30] \"\u001b[33mPOST /init HTTP/1.1\u001b[0m\" 404 -",
  "2021-01-29T09:53:30.748Z       stderr: The action did not initialize or run as expected. Log data might be missing."
]

I've added the Docker container to IBM Cloud Functions

What would be the best way to approach this?

2

There are 2 answers

0
mime On

Docker images which are uploaded to IBM Cloud Functions must implement certain REST interfaces. The easiest way to achieve this is to base your container on the openwhisk/dockerskeletonimage.

Please see How to run a docker image in IBM Cloud functions? and https://github.com/iainhouston/dockerPython for more details

0
data_henrik On

The docs for IBM Cloud Functions have some pointers on how to create Docker-based functions. IMHO Cloud Functions are more for short-running serverless workloads and I would like to point you to another serverless technology in the form of IBM Cloud Code Engine. Its model is based on Docker containers and one of its use cases are http-based web applications, e.g., like your Flask app.

You would define the Dockerfile as you want, don't need a special skeleton and could just follow this guide on Dockerfile best practices for Code Engine.