Monday, June 15, 2015

Docker based Python Project Deployment with NginX, Supervisor, uwsgi on Ubuntu

For Docker installation refer :

Assume your python code is inside /var/app/project/ and server run file is

Note : Create folder/path if don't exists. for example /var/app/run/ which don't exits then create it. similarly for other paths.

Step 1 :

Create one custom supervisord.conf file inside /var/app/supervisor/

Content of file should be :

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
files = /var/app/supervisor/conf.d/*.conf

Step 2 :

Create "superisor.docker.conf" file inside /var/app/supervisor/conf.d/

Content of file should be :

command = /usr/bin/docker run --rm --name pyenv -v /var/app:/var/app felixonmars/archlinux supervisord -c /var/app/supervisor/supervisord.conf

Step 3 :

make link of file /var/app/supervisor/conf.d/superisor.docker.conf in /etc/supervisor/conf.d/ using :

sudo ln -s /var/app/supervisor/conf.d/superisor.docker.conf /etc/supervisor/conf.d/

Step 4 :

Now onwards , your all supervisor .conf files will be inside /var/app/supervisor/conf.d/

Create project_supervisor_docker.conf file inside /var/app/supervisor/conf.d/ and its content should be :

command = /usr/bin/uwsgi --plugin=python2
    --limit-as=512 --processes=2 --max-request=2000
    --memory-report --enable-threads
    --master --no-orphans --logdate --chmod-socket=660
    --uid=33 --gid=33 --need-app

Step 5 :

Create project_master_supervisor_docker.conf inside /var/app/supervisor/conf.d/ and its content :

command = /usr/bin/python2

Step 6 :

Create Nginx Configuration file for project. Its content should be :


upstream push_daemon_project {
    server; #Assume push socket on port 8284

server {
    listen 80;
    listen [::]:80;


    location = /nginx_stub_status {
        stub_status on;
        deny all;

    location / {
        return 301$request_uri;

server {
    listen 443 ssl spdy;
    listen [::]:443 ssl spdy;

    ssl_certificate /var/app/cert/; # certificate ssl
    ssl_certificate_key /var/app/cert/; # certificate key ssl

    access_log /var/log/nginx/project.access.log;
    error_log /var/log/nginx/project.error.log;

    root /var/app/project/web;  ## template (html) directory

    location /static {
        alias /var/app/project/static;  ## static files

    location = /nginx_stub_status {
        stub_status on;
        deny all;

    location /websocket {
        proxy_pass http://push_daemon_project;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///var/app/run/uwsgi/project.socket;

Step 7 :

Restart all instances :

sudo chown -R www-data:www-data /var/app/project
sudo service nginx restart
sudo supervisorctl update
sudo /etc/init.d/supervisor force-reload
sudo docker ps -a # you can see docker process id : for ex : 6a2dfdeb0000
sudo docker restart 6a2dfdeb0000

Grunt , bower, nodejs installation & commands


sudo apt-get install --reinstall --install-recommends nodejs
sudo npm install -g grunt
sudo npm install -g grunt-cli
sudo npm install -g bower

# Locate package.json and run : 
 sudo npm install
# Locate bower.json and run :
 bower install

# Locate .Gruntfile.js and run : 
 "grunt dev --force" in local
 "grunt build --force" in live

* Commands * Git Submodule , merge , git rm reset / undo

git clone <SSH-URL> <directory_name>
git submodule init
git submodule update

# merge branch in master
git checkout master
git merge <branch_name>

# Undo "git rm -r ." command.
git reset HEAD

Mongo Useful Commands and steps for Dump , Restore from live with ssh access ( without FTP )

# Run in live server 
sudo mongodump --host <host_url> --port 27017 --db <db_name> 
sudo tar -zcvf <compress_file_name_of_dump>.tar.gz dump
# Note down path like
scp <server_user>@<server_ip_address>:/var/app/dump/<compress_file_name_of_dump>.tar.gz <local_absolute_path_where_dump_will_download>
mongorestore --host localhost --db <db_name> --port 27017 <downloaded_dump_folder_absolute_path>
If you are not able to connect mongo server ...
sudo service mongodb stop
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start