part-2 )( Dockerize and deploy Machine learning model as REST API using Flask

#cloudcomputing #machinelearning #docker #deployment #datascience

Sumit Kumar Sept 22 2020 · 1 min read
Share this

In this story, we will see how to dockerize the API and deploy it. (pat-1)

let’s undetstand some most useful and basic commands of Docker

  • FROM : will create base image which is created with docker hub.
  • COPY : will copy the files in the docker image.
  • EXPOSE : will expose the port number which we want to use.
  • WORKDIR : defines the working directory of a Docker Container.
  • RUN : will let you execute command inside your docker image.
  • CMD : defines default command which user can easily override.
  • step 1 — Create Dockerfile

    Create a new file and rename it    Dockerfile

    before moving, we have to make some changes in flask app

    Replace

    if name=='__main__':
    app.run()

    with

    if name=='__main__':
    app.run(host='0.0.0.0')

    Now, Inside Dockerfile

    FROM continuumio/anaconda3:4.4.0  
    COPY . /usr/app
    EXPOSE 5000
    WORKDIR usr/app
    RUN pip install -r requirements.txt
    CMD python flask_app.py

    Creat your requirements.txt file by following command

    pip freeze > requirements.txt

    Step 2 — Build docker image

    docker build -t "<app_name>" . 
    --------------------------------
    Eg: docker build -t noteAuth_api .

    Step 3 — Run the Docker container after build

    docker run -p 8000:8000 noteAuth_api
  • p : to make the port available for the browser externally
  • manage your all running container

    docker ps   # You can see you container id , status and name etc

    If you want to access the ip address for a specific running container

    docker inspect "<Container_id>"

    Kill and remove container

    docker rm "<container_id>" -f

    If you will use flasgger_app.py from my github repo which is created with flasgger ( pip install flasgger ) for User interface. Output will be like running on Doker

    Congratulations 🥳 ! You made it.This is it with Dockers.

    There is a lot to learn on dockers but at beginner level it is sufficient.

    In the upcoming story, we will see How to deploy Machine Learning pipeline on Google kubernates Engines.

    Thank You

    Comments
    Read next