Create and Dockerize a project with RASA, Flask and Mongodb with Docker and Docker-compose
By: Raoof Naushad
Tags: Mongodb, Docker Compose, Rasa, Docker, Flask

Create and Dockerize a project with RASA, Flask and Mongodb with Docker and Docker-compose Raoof Naushad Follow Jul 16 · 8 min read

I was searching for tutorials for setting up and dockerizing an intent and entity recognition model. After some search I was able to find docker containers of rasa, rasa_core, rasa_nlu etc. Since I just wanted to use my own architecture which I developed using virtual environment I thought of adding the RASA nlp engine inside my web_app container. This blog will give you an understanding of how you can deploy an NLP engine for intent and entity powered by Flask deployed by Docker.

Here I attach the github link for the project for more reference.

What are the things you need to install?

Flask RASA Docker and Docker-compose Mongodb

In this blog I am not going to explain how you wanted to download, install and configure 3 and 4. But I will add reference if you need.

There are lots of cool stuffs you can do with Docker and Docker-compose just read the document if you are curious.

I hope you installed and configured everything. Now I hope docker and docker-compose is there in your machine. You can check it by typing docker — version also docker-compose can be checked with docker-compose --version command. You will get the output as below.

Generated By Author

I also hope that mongodb is there on your machine. You can check that with typing mongo you will get output as below.

Generated By Author

This also means that mongodb is running and you can communicate it with the port address mongodb://127.0.0.1:27017 which is default. You can try and play with some basic commands of mongodb if you want to.

We need to install some packages now!

Before that let’s setup everything in virtual environment. So let’s install virtual environment. Follow the steps if you are new to this.

## Installing virtual environment

pip install virtualenv

## Create a virtualenv with python3

virtualenv -p python3 venv

## Activate the virtual environment

source venv/bin/activate

Generated by Author

Now you are in venv install some packages inside venv to start working.

pip install flask rasa Flask-PyMongo requests

now let’s create a rasa sample rasa engine. Rasa with great community support and easy to use mechanism help us starting our project with one single command. Create a directory called rasa_engine and cd into it. type rasa init . It will ask you to some queries answer it and you will get a project structure along with model trained on your machine itself.

Generated By Author

I am not going to explain the project structure of rasa except the one we use and what it does because it is well documented in RASA docs. Check it.

data we need to place our data in .md or json format in data folder. you can already see the data there.

format in data folder. you can already see the data there. models is where our model get stored.

config.yml is where you have to keep the pipeline of your training.

That’s all it. I don’t need the rest for my task I am going to remove those.

My final rasa_engine project structure will look like this.

Generated By Author

Go to your project directory. Now for training an nlu model you can use this command.

rasa train nlu -c rasa_engine/config.yml — out rasa_engine/models/ -u rasa_engine/data/

Now using the below command you can run an webserver for nlp engine

rasa run -m rasa_engine/models/ -p 5055 --enable-api

Using postman or curl command check out your nlp engine response. A post request with the below format will on http://localhost:5055/model/parse this will give you response.

Generated By Author

Now we will create a flask app to serve this and mongodb to store the response.

Creating flask app and configuring mongodb and Rasa engine

Create a file called app.py in your project directory. And check out the code below

Generated By Author

Generated By Author

I will explain this code. It is simple actually.

1) We are creating a flask application

2) Creating a connection to the mongodb and creating objects for mongodb database and collection.

3) There are two endpoints / for checking the app is working.

4) /parsed when called send a post req to nlp engine which is running and get response back.

For checking this

1) Run rasa engine in a terminal with the above command.

2) Run the app.py with the command python3 app.py

Generated By Author

Generated By Author

Now the final and important part let’s deploy this using docker

Creating a Docker Container for this application.

Let’s divide this into two parts.

DockerFile

We need to write a dockerfile. But before that lets create two important files.

A) requirements.txt:

This will be used to install all depencies when docker container is build.

Generated By Author

B) Entry point script file — engine_start.sh

This script file will be used as an entry point when docker is build and run. So we need to add two things here. One is to enable RASA_ENGINE API and also to run the flask app.

Generated By Author

So out final project structure will be like this

Generated By Author

Now we will move to the Dockerfile

Generated By Author

What does this file do?

Pulling an Ubuntu 18.04 image from docker hub updating and installing important packages like python, nano, sudo etc Adding user and adding permissions Copying everything from out current working directory to the destination directory inside docker. Creating our working directory Adding to path since the packages we install via requirements.txt will be installed there. Upgrading pip and installing depencies in requirements.txt Exposing RASA_ENGINE and APP ports outside of the container. Starting our entry point. So whenever Docker runs it will start with that script. That means it start enabling RASA engine and APP.

Now we need an another container for mongodb to work with. This can be created with an image of mongodb directly pulled from docker hub.

let’s work on Docker-Compose.yml

Generated By Author

Generated By Author

Let’s walk through this:

After specifying the version and services, we have to specify the services (containers) we want to build. So here there are two containers

a) webapp : For our flask and rasa

b) mongodb : For our mongo database In webapp container we specified the location to build and docker file. Also we named container and specified some environment variables.

MONGODB_DATABASE: flaskdb

MONGODB_USERNAME: mongodbuser

MONGODB_PASSWORD: password

MONGODB_HOSTNAME: mongodb

Before when we tried this experiment with virtual environment we didn’t used authentication for mongodb database. But for the security here we are using authentication. For that we created the environment variables. I will show where we will change this in our app.py

4. Here we specified network for connecting mongo and webapp inside the container. Also we expose ports from container to the host machine. Last but not the least we mount the volume with current directory and directory where the project resides in docker container. This will really help when you need to change the code we can change it in our host machine and changes will reflect inside container.

networks:

- backend ports:

- "5056:5056"

- "5055:5055" depends_on:

- mongodb volumes:

- .:/home/accubits/ivr/

5. mongodb server is just same as the webapp server except for the fact that instead of building an image we are loading mongo:4.0.8 image from docker hub and give it a container name. And we also reference some environment variables like mongodb username and password as i said for more security.

6. The yml file continue with volume and network that we have already covered.

7. Atlast we specify the network and volume and their drivers.

For more details about each of the variables and its usage Docker documents are too good you can give it a shot.

Here I changed app.py little for authentication of mongodb. Check that below.

Generated By Author

I know it took a lot of time but PAIN IS GAIN

Now you can deploy you NLP APP where ever you want with just one command.

chmod +x engine_start.sh

Incase if you are getting error try to give permission to the executable script in the host machine

docker-compose up

## or docker-compose up -d for daemon mode.

Finished building container — Generated By Author

You can check the response with postman or curl command.

Generated By Author

If you want to enter into the docker container you can use the below command and experiment it like an ubuntu machine.

## For the webapp container

docker exec -it webapp bash

## For the mongodb container

docker exec -it mongodb bash

As I said this tutorial just showed how you can merge these technologies from development to deployment.

You can improve with

1) Adding more data for your use case

2) Configuring pipeline for your use case and data

3) Add custom components etc.

4) More processing on the web app side.