Django is a Python-based web framework that is powerful and flexible. Domino is known to support Flask and Dash for publishing Python apps, but we are happy to get a basic implementation of Django working on Domino as well.
A word of caution before continuing, the Django application that I have tested is incredibly basic, so more complex Django applications may be missing some additional configuration or they may not work at all on Domino.
Create a Compute Environment for your project
You'll need to edit or create a Compute Environment with a few packages installed first. If you are not familiar with how to do this, check the documentation here:
https://docs.dominodatalab.com/en/latest/user_guide/5dd2c1/edit-environment-definition/
Add the following to the Dockerfile Instructions of your Compute Environment:
USER root
RUN pip install Django postgres gunicorn
RUN apt update
RUN apt install nginx -y
Once the environment successfully builds go back to your Domino project and under the project Settings select your newly built Environment as the default for the project.
Create a basic Django example
1) From your Domino project start a workspace.
2) Create a sample django project in your workspace called 'mysite' with the following command in the workspace terminal:
django-admin startproject mysite
3) First thing we need to do is open the settings.py
file in /mnt/mysite/mysite/settings.py
to make some changes to allow Domino to interact with our Django application. We need to add the following lines to the end of that file:
ALLOWED_HOSTS = ['*'] X_FRAME_OPTIONS = 'ALLOWALL' XS_SHARING_ALLOWED_METHODS = ['POST','GET','OPTIONS', 'PUT', 'DELETE']
These options are meant to open up some configuration settings in Django in order to allow traffic to be routed from Domino into the Django app. These settings could be modified to be a bit more secure, but for the purpose of this example, we will keep them fairly liberal.
4) Add a configuration file for NGINX. So let's create a file named default
in the root of our project, /mnt/default
:
server { listen 8888 default_server; listen [::]:8888 default_server; client_max_body_size 1024M; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { proxy_pass http://127.0.0.1:55850/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; proxy_read_timeout 86400; } }
This file tells NGINX what to do when its running and receives traffic. A couple things of note in here, `listen 8888 default_server;` tells that NGINX will be listening on port 8888, which is the default app port that Domino will route traffic to. Secondly, `proxy_pass http://127.0.0.1:55850/;` is saying that when NGINX receives a request, forward it to this address, which is where our Django app is going to be running. If you are interested learning about some of the other configuration fields, then head over to the NGINX documentation.
5) Next, Domino published apps need to have an app.sh in the root of the project in order to tell Domino how the app should be published. Let's go ahead and create that file now, with the following contents, /mnt/app.sh
:
#!/usr/bin/env bash cd $DOMINO_WORKING_DIR sudo cp $DOMINO_WORKING_DIR/default /etc/nginx/sites-enabled/ sudo service nginx start cd /mnt/mysite gunicorn -b 0.0.0.0:55850 mysite.wsgi:application
The shell script above is responsible for starting NGINX and copying the config we made to the directory that NGINX expects it to be in, and then moves to start our app using Gunicorn, which is an HTTP server for Python.
6) Sync all of the changes in your workspace so they are saved to the Domino File System in your project. You should see the file changes you have made when you go to the Files dashboard in your Domino project. You can now stop your workspace.
7) Now we should be in a position where we can publish the app from Domino and see our Django site running! Go to the App dashboard in your project and publish your app. For more information on publishing apps in Domino, check out our documentation.
8) Once successfully published, press View App and you should see the below screenshot:
In conclusion, this is a quick way to get Django up and running with Domino. To do some more complex workloads with Django, you may need to do more than this, but hopefully this is a good starting point!
Comments
0 comments
Please sign in to leave a comment.