Deploying a Python web app
We’re going to get a simple Python app running on Orchard. We need just two files: a Python file which runs the app, and a Dockerfile which builds an image with its environment.
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!\n' if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)
FROM orchardup/python:2.7 RUN pip install Flask ADD . /code WORKDIR /code CMD python app.py
Here’s what’s happening:
- We start from a pre-made image with Python 2.7 (FROM).
- Install Flask via pip (RUN).
- Add the current directory to the container at
/codeto be the default working directory for the container (WORKDIR).
- Set the container’s default command to run the app (CMD).
We can now build the app’s image on Orchard. First, create your default Docker host if it isn’t already running:
$ orchard hosts create Default host running at 188.8.131.52
Then build the image with
orchard docker build:
$ orchard docker build -t python-example .
This will upload the current directory to the Docker host, run the commands in the Dockerfile and create an image called
python-example based on the resulting container.
Once the image has built, we can run it:
$ orchard docker run -d -p 80:5000 python-example 62fee4358adb20b6e00c1c4a4c975395ebf999c475fbdfde19efe26bd34b8a19 $ orchard docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 62fee4358adb python-example:latest /bin/sh -c python ap 6 seconds ago Up 5 seconds 0.0.0.0:80->5000/tcp
ps command indicates that the app is running. We can now get the IP of our Docker host with
$ orchard hosts NAME SIZE IP default 512M 184.108.40.206
We can now access the app at that address:
$ curl 220.127.116.11 Hello World!
We can stop the container using its ID (or a prefix of it):
$ orchard docker kill 62fee4358adb 62fee4358adb
Finally, hosts are charged based on how long they’re around for, so if you’re done, you can remove it:
$ orchard hosts rm