BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Django with Kubenetes II: Gunicorn and Nginx with Docker Compose

django.png docker-icon.png kubenetes-icon.png




Bookmark and Share





bogotobogo.com site search:





Summary

In this article, we'll deploy Django app (bare minimum) using Docker-compose. The other pieces are Nginx and Gunicorn.


Here are the files after everything's done.

tree-docker-compose.png

Note that we created a Dockerfile in each of the service (web, nginx, and postgres). As we already know, the Dockerfile defines an application's image content via one or more build commands that configure that image. Once built, we can run the image in a container.


The result should look like this:

localhost-8087.png




djangoweb service

The requirements.txt in djangoweb directory is used by the RUN pip install -r requirements.txt command in the Dockerfile:

 FROM python:3.6
 ENV PYTHONUNBUFFERED 1
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
 RUN pip install -r requirements.txt
 ADD . /code/

This Dockerfile starts with a Python 3 parent image. The parent image is modified by adding a new code directory. The parent image is further modified by installing the Python requirements defined in the requirements.txt file.





Create Docker-compose script

The docker-compose.yml file describes the services that make our app.

"Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration." - Overview of Docker Compose

With the following docker-compose.yml file, we create the database service named db using the PostgreSQL alpine Linux, create the nginx service using the Nginx alpine Linux, and create our Django container service named web using the custom docker images generated from our Dockerfile.

docker-compose.yml:

version: '3'

services:

  db:
    #image: postgres
    build: ./postgres
    restart: unless-stopped
    expose:
      - "5432"
    environment:   # will be used by the init script
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres
    volumes:
      - ./postgres/pgdata:/var/lib/postgresql/data/  # persist container's db data to local pgdata/ (mounted)

  web:
    build: ./djangoweb
    # command: python3 manage.py runserver 0.0.0.0:8000
    command: gunicorn djangoweb.wsgi -b 0.0.0.0:8000
    volumes:
      - .:/code
    expose:
      - "8000"
    depends_on:
      - db

  nginx:
    restart: always
    build: ./nginx/
    ports:
      - "8087:80"  # host:container 
    links:
      - web

The compose file also describes which Docker images these services use, how they link together (depends_on), any volumes they might need mounted inside the containers.

The expose exposes ports without publishing them to the host machine - they'll only be accessible to linked services. Only the internal port can be specified.

As shown earlier, we get our app via http://localhost:8087.

Note also that we mounted volumes relative to the Compose file:

  1. For postgres service:
    volumes:
       - ./postgres/pgdata:/var/lib/postgresql/data/
    
  2. For web:
    volumes:
      - .:/code
    

We are using restart:

no is the default restart policy, and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.

restart: "no"
restart: always
restart: on-failure
restart: unless-stopped





Install Docker

Ref: Docker Guide: Dockerizing Python Django Application

We will install docker-ce community edition and docker-compose that support compose file version 3.

$ sudo apt install -y docker-ce

$ docker --version
Docker version 18.06.1-ce, build e68fc7a

Then, we may want to add a new user named 'kube' and add it to the docker group:

$ sudo useradd -m -d /home/kube -s /bin/bash kube
$ sudo usermod -a -G docker kube
$ sudo passwd kube

Login as the "kube" user and run docker command as below and make sure we get the hello-world message from Docker:

$ su - kube

$ docker run hello-world
...
Hello from Docker!




Install Docker-compose

we will be using the latest docker-compose support for compose file version 3 and download the latest version of docker-compose using curl command to the '/usr/local/bin' directory and make it executable using chmod:

$ sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose

$ docker-compose version
docker-compose version 1.21.0, build 5920eb0
docker-py version: 3.2.1
CPython version: 3.6.5
OpenSSL version: OpenSSL 1.0.1t  3 May 2016




Build and Run the Docker image

Now, let's build the docker images using the docker-compose command.

If we change a service's Dockerfile or the contents of its build directory, we can run docker-compose build to rebuild it.

$ docker-compose build
Building db
Step 1/2 : FROM postgres:10.3-alpine
...
Step 2/2 : COPY ./init/db_setup.sh /docker-entrypoint-initdb.d/db-setup.sh
...
Successfully tagged django-gunicorn-nginx-2_db:latest
Building web
Step 1/7 : FROM python:3.6
...
Step 2/7 : ENV PYTHONUNBUFFERED 1
...
Step 3/7 : RUN mkdir /code
...
Step 4/7 : WORKDIR /code
...
Step 5/7 : ADD requirements.txt /code/
...
Step 6/7 : RUN pip install -r requirements.txt
...
Installing collected packages: pytz, Django, psycopg2-binary, gunicorn
Successfully installed Django-1.11.15 gunicorn-19.7.0 psycopg2-binary-2.7.5 pytz-2018.5
...
Step 7/7 : ADD . /code/
...
Successfully tagged django-gunicorn-nginx-2_web:latest
Building nginx
Step 1/2 : FROM nginx
...
Step 2/2 : COPY default.conf /etc/nginx/conf.d/default.conf
...
Successfully tagged django-gunicorn-nginx-2_nginx:latest

Start all services:

$ docker-compose up
Creating network "django-gunicorn-nginx-2_default" with the default driver
Creating django-gunicorn-nginx-2_db_1 ... done
Creating django-gunicorn-nginx-2_web_1 ... done
Creating django-gunicorn-nginx-2_nginx_1 ... done
Attaching to django-gunicorn-nginx-2_db_1, django-gunicorn-nginx-2_web_1, django-gunicorn-nginx-2_nginx_1
db_1     | 2018-09-30 17:24:54.489 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1     | 2018-09-30 17:24:54.489 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1     | 2018-09-30 17:24:54.845 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1     | 2018-09-30 17:24:55.316 UTC [18] LOG:  database system was shut down at 2018-09-30 01:12:15 UTC
db_1     | 2018-09-30 17:24:55.463 UTC [1] LOG:  database system is ready to accept connections
web_1    | [2018-09-30 17:24:59 +0000] [1] [INFO] Starting gunicorn 19.7.0
web_1    | [2018-09-30 17:24:59 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_1    | [2018-09-30 17:24:59 +0000] [1] [INFO] Using worker: sync
web_1    | [2018-09-30 17:24:59 +0000] [8] [INFO] Booting worker with pid: 8
nginx_1  | 192.168.80.1 - - [30/Sep/2018:17:25:09 +0000] "GET / HTTP/1.1" 200 1716 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"

The docker-compose up command aggregates the output of each container (essentially running docker-compose logs -f). When the command exits, all containers are stopped. Running docker-compose up -d starts the containers in the background and leaves them running.

Wait for Docker to build our Python image and download the nginx and postgresql docker images. Once it's complete, check running container and list docker images on the system using following commands.

Then, we will get three containers running and list of Docker images on the system as shown below:

$ docker-compose ps
$ docker ps
CONTAINER ID  IMAGE                  COMMAND                 CREATED        STATUS        PORTS                NAMES
c8d37a368e06  djangoproject-2_nginx  "nginx -g 'daemon of…"  2 minutes ago  Up 2 minutes  0.0.0.0:8087->80/tcp djangoproject-2_nginx_1
11a72245216a  djangoproject-2_web    "gunicorn djangoweb.…"  2 minutes ago  Up 2 minutes  8000/tcp             djangoproject-2_web_1
5bd3e0ad7ec3  djangoproject-2_db     "docker-entrypoint.s…"  2 minutes ago  Up 2 minutes  5432/tcp             djangoproject-2_db_1


$ docker-compose images
       Container               Repository          Tag       Image Id      Size  
---------------------------------------------------------------------------------
djangoproject-2_db_1      djangoproject-2_db      latest   7b1990e7f317   37.7 MB
djangoproject-2_nginx_1   djangoproject-2_nginx   latest   6e84cc2ff709   104 MB 
djangoproject-2_web_1     djangoproject-2_web     latest   1cbc8e6d2726   921 MB 

As we can see nginx container has it's 80 mapped to port 8087 of the docker host (machine on which we're running docker-compose). Simply hit the localhost i.e. http://localhost:8087 and we'll have the Django homepage appear, and we should see the following:

localhost-8087.png



Docker-compose stop/rm/down

To stop a docker application that is running in the background, use the docker-compose stop. There are two steps to stop a docker application containers:

  1. First, stop the running containers using docker-compose stop
  2. Second, remove the stopped containers using docker-compose rm -f

$ docker-compose stop && docker-compose rm -f

Here, we used && which will execute the 2nd command only after the 1st command is successful. So, it will do "rm -f", only after stopping the docker containers successfully.


We can also use docker-compose down to stop containers and remove containers, networks, volumes, and images created by up. By default, the only things removed are:

  1. Containers for services defined in the Compose file
  2. Networks defined in the networks section of the Compose file
  3. The default network, if one is used






Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Django 1.8



Introduction - Install Django and Project Setup

Creating and Activating Models

Hello World A - urls & views

Hello World B - templates

Hello World C - url dispatcher

Hello World D - Models and SQLite Database

MVC - Hello World

Hello World on a Shared Host A

Hello World on a Shared Host B

Hello World - Admin Site Setup

virtualenv

Creating test project on virtualenv

Test project's settings.py

Creating Blog app and setting up models

Blog app - syncdb A

Blog app - syncdb B

Blog app - views and urls

Blog app - templates

Blog app - class based templates

Image upload sample code - local host

Authentication on Shared Host using FastCGI

User Registration on Shared Host A

User Registration with a Customized Form on Shared Host B

Blogs on Shared Host

Serving Django app with uWSGI and Nginx

Image upload sample code - shared host

Managing (Deploying) Static files (CSS, Images, Javascript) on Shared Host

Forum application on a Shared Host

Django Python Social Auth : Getting App ID (OAuth2) - Facebook, Twitter, and Google

Django: Python social auth, Facebook, Twitter, and Google Auth

Django: Python social auth, Facebook, Twitter, and Google Auth with Static files

...

Django 1.8 hosted on Linode VPS ==>

1. Setup CentOS 7 hosted on VPS

1B. Setup CentOS 7 hosted on VPS (multi-domain hosting setup) - Name server and Zone File settings (from GoDaddy to Linode)

2. ssh login and firewall

3. Apache Install

4. Install and Configure MariaDB Database server & PHP

5. Install and Configure Django

6. Model

7. Model 2 : populate tables, list_display, and search_fields

8. Model 3 (using shell)

9. Views (templates and css)

10. Views 2 (home page and more templates)

11. TinyMCE

12. TinyMCE 2

13. ImageField/FileField : Serving image/video files uploaded by a user

14. User Authentication 1 (register & forms)

15. User Authentication 2 (login / logout)

16. User Authentication 3 (password reset) - Sent from Email (gmail) setup etc.

17. User Authentication 4 (User profile & @login_required decorator)

18. User Authentication 5 (Facebook login)

19. User Authentication 6 (Google login)

20. User Authentication 7 (Twitter login)

21. User Authentication 8 (Facebook/Google/Twitter login buttons)

22. Facebook open graph API timeline fan page custom tab 1

23. Facebook Open Graph API Timeline Fan Page Custom Tab 2 (SSL certificate setup)

24. Facebook open graph API timeline fan page custom tab 3 (Django side - urls.py, settings.py, and views.py)

...

A sample production site Django 1.8.7: sfvue.com / einsteinish.com ==>

A sample production app (sfvue.com) with virtualenv and Apache

2. Upgrading to Django 1.8.7 sfvue.com site sample with virtualenv and Apache

(*) Django 1.8.7 einsteinish.com site - errors and fixes

Django 1.8.12 pytune.com site - local with Apache mod_wsgi

Django 1.8.12 pytune.com site - local with Nginx and uWSGI

Django 1.8.12 pytune.com site - deploy to AWS with Nginx and uWSGI

Django Haystack with Elasticsearch and Postgres

Django Compatibility Cheat Sheet

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...









Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master