Saturday, 9 May 2020

Docker: Python: Flask: Build an image using Docker file

Step 1: Create a directory ‘flask_hello_world’.

Step 2: Navigate to flask_hello_world and create Dockerfile.
$mkdir flask_hello_world
$
$cd flask_hello_world/
$
$touch Dockerfile
$ls
Dockerfile

Step 3: Update Dockerfile with below content.

Dockerfile
FROM ubuntu

RUN apt-get update

RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb

RUN pip install flask

COPY app.py /opt/app.py

ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0

Step 4: Create a file app.py in the folder where Dockerfile is located.

app.py
import os
from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
    return "Hello World"

@app.route('/welcome')
def welcome():
    return 'Welcome to Docker Container'

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)

$ls
Dockerfile   app.py

Step 5: Execute the command ‘docker build -t flask_hello_world .’ from the directory where Dockerfile is located.
$docker build -t flask_hello_world .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/6 : RUN apt-get update
 ---> Using cache
 ---> 93102f1bf79f
Step 3/6 : RUN apt-get install -y python python-setuptools python-dev build-essential python-pip python-mysqldb
 ---> Using cache
 ---> c04fb08e8e87
Step 4/6 : RUN pip install flask
 ---> Running in a57ed8d799a6
Collecting flask
  Downloading https://files.pythonhosted.org/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
Collecting click>=5.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
Collecting Werkzeug>=0.15 (from flask)
  Downloading https://files.pythonhosted.org/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1 (from flask)
  Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 flask-1.1.1 itsdangerous-1.1.0
Removing intermediate container a57ed8d799a6
 ---> 080f962d64bf
Step 5/6 : COPY app.py /opt/app.py
 ---> 217efc0c9c63
Step 6/6 : ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
 ---> Running in 272c2c703d4a
Removing intermediate container 272c2c703d4a
 ---> a04372b9c2d0
Successfully built a04372b9c2d0
Successfully tagged flask_hello_world:latest

Step 6: Execute the command ‘docker images’ to see list of images.
$docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
flask_hello_world               latest              a04372b9c2d0        55 seconds ago      467MB
mysql                           latest              62a9f311b99c        2 weeks ago         445MB
jboss/wildfly                   latest              4e440230e01d        2 months ago        748MB

You can see that ‘flask_hello_world’ image is available.

Step 7: Run the container by executing the below command.
docker run -p 1234:5000 flask_hello_world
$docker run -p 1234:5000 flask_hello_world
 * Serving Flask app "/opt/app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
172.17.0.1 - - [30/Aug/2019 21:47:34] "GET / HTTP/1.1" 200 -
172.17.0.1 - - [30/Aug/2019 21:47:34] "GET /favicon.ico HTTP/1.1" 404 –

Open the url ‘http://localhost:1234/’ in browser, you can see below kind of screen.

Open the url ‘http://localhost:1234/welcome’ in browser, you can see below kind of screen.





Previous                                                    Next                                                    Home

No comments:

Post a Comment