Python run on containers
We have alrady explain Website hosted as a container. In this post explained how to host flask web application.
Suppose your main python application, Dockerfile, Runfile and requirments.txt in the same folder.
tree <folder>
For example suppose you need to create Flask application, the your requriements.txt:
flask
The Runfile to run the flask
web: flask run --host 0.0.0.0
The main application is:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Ojitha!'
Here the Dockerfile:
FROM python:3.8.5
WORKDIR /src
RUN pip install flask
COPY app.py .
EXPOSE 5000
CMD ["flask", "run", "--host", "0.0.0.0"]
We are using the python version 3.8.5 image. The app working directory in the container is src
. In addition to that expose port 5000.
build the image:
docker build -t localhost:5001/ojflaskimage .
Now run the docker container
ocker run -d -p 5000:5000 --name=ojflaskapp localhost:5001/ojflaskimage
now test with
curl http://localhost:5000