891rpm

Deploy Django, Gunicorn, NGINX, PostgresQL using Docker

This post mainly based on this blog: https://docs.docker.com/compose/django/.I will be extending this post by serving django+gunicorn using Nginx, also I will using Postgresql docker container to use it as database.

Lets not waste time and go to the following steps.

1. Let’s make an empty directory named myproject and add another folder inside name it srcsrc should contain the django project. For testing purpose lets put a simple django project inside named mydjango.

2. Let’s create a subdirectory inside myproject and name it config. Lets put a requirement.pip file inside config and write these line in it:

3. Now let’s make a Dockerfile inside the myproject. This should contain the following lines:

So this Dockerfile starts with a Python 3.5 based image. Then the container is modified by adding the requirement.pip file in /config directory within the container and installing the packages from it.4. Let’s create a file called docker-compose.yml in myproject directory.

The docker-compose.yml file describes the services that make your app. Here we need a web service(Django+Gunicorn), A database(Postgres), and Proxy Server(Nginx). It also describes which Docker images these services will use, how they will link together, any volumes they might need mounted inside the containers. Finally, the docker-compose.yml file describes which ports these services expose. See the docker-compose.yml reference for more information on how this file works. Don’t forget to add docker-compose
to your python environment by running pip install docker-compose.

5. Let’s add the following configuration to the docker-compose.yml file:

It says that there are three services for this project: nginx, web, db. nginx depends on web, web depends on db. db container uses postgres’s latest image from dockerhub. Default username for db is postgres and password is postgres
web container is build using project’s Dockerfile. It mounts src directory into it and exposes port 8000. version is being used for which format to use to compose the docker file.nginx uses nginx’s latest image from dockerhub. This proxy server is accessible from port 8000. It mounts src and config directory.

6. Now let’s write a nginx configuration config file named mydjango.conf inside myproject‘s config folder and put it in a subdirectory named nginx.

So what it does that, nginx acts as a reverse proxy for any connections going to django server and all connections goes through nginx to reach django server.Project Directory should look like this:

7. To communicate from django to postgres, we need to put database configuration in django applications settings file. It should look like this:

8. All is done. Now lets run docker-compose build in terminal within the project directory. It will build/rebuild(if necessary) all the containers. For first time running the containers, run docker-compose up -d. Lets go to browser and type: localhost:8000. We should see the django application up and running.

9. For stopping the docker, run docker-compose stop. Re-running docker, use docker-compose start.10. For shell accessing.

For logs:

Thats it. You can see an working example here in my repo: https://github.com/ruddra/docker-djangoAlso another deployment example for Ruby on rails here: https://github.com/ruddra/deploy-notebook
(Thanks to Akimul Islam for the source)

Cheers!!

Update:

Serving django with gunicorn won’t allow you to serve static files with it. You need to serve static files seperately. You can follow this post: http://ruddra.com/2016/11/02/serve-static-files-by-nginx-from-django-using-docker/ for how to do serve static files using Nginx from docker.

 

How to Install and Connect to PostgreSQL on CentOS 7

postgresqlPostgreSQL (pronounced ‘post-gres-Q-L’) is a free, open-source object-relational database management system (object-RDBMS), similar to MySQL, and is standards-compliant and extensible. It is commonly used as a back-end for web and mobile applications. PostgreSQL, or ‘Postgres’ as it is nicknamed, adopts the ANSI/ISO SQL standards together, with the revisions.

Pre-Flight Check
  • These instructions are intended specifically for installing PostgreSQL on CentOS 7.
  • I’ll be working from a Liquid Web Self Managed CentOS 7 server, and I’ll be logged in as root.
Step 1: Add the PostgreSQL 9.3 Repository

In this case we want to install PostgreSQL 9.3 directly from the Postgres repository. Let’s add that repo:

Step 2: Install PostgreSQL

First, you’ll follow a simple best practice: ensuring the list of available packages is up to date before installing anything new.

Then it’s a matter of just running one command for installation via apt-get:

PostgreSQL should now be installed.

Step 3: Start PostgreSQL

Configure Postgres to start when the server boots:

Start Postgres:

Step 4: Switch to the Default PostgreSQL User

As part of the installation Postgres adds the system user postgres and is setup to use “ident” authentication. Rolesinternal to Postgres (which are similar to users) match with a system user account.

Let’s switch into that system user:

And then connect to the PostgreSQL terminal (in the postgres role):

That’s it! You’re connected and ready to run commands in PostgreSQL as the postgres role.

Synology NAS: Run Fsck To Check and Repair a Linux File System

DS509+I own a Linux powered Synology dedicated Network Attached Storage (NAS) server for my home office use. How do I run fsck on Synology DiskStation that offers RAID storage using Linux command line options over an ssh session?

This server is powered by Linux operating system and comes with the e2fsck program that can be used to check the ext3/ext4 family of file systems.

Difficulty: Intermediate
Root privileges: Yes
Requirements: Synology server

First, you need to login using ssh interface. The syntax is as follows:

Once logged in you need to stop running services such as smb/nfs/pgsql and so. To see current volumes or mount point type the following command:

Sample outputs:

To see current services accessing the /volume1/ and /opt/, run:

Sample outputs:

You need to stop pgsql service, enter:

Sample outputs:

In short, you need to stop services that are running and accessing data shares such as SMB,NFS,pgsql,mysql and so on. You can use web interface to stop these services too. cd to /usr/syno/etc/rc.d/ and stop all file sharing services. Finally, unmount volumes as follows:

Verify that /opt and /volume1/ are unmounted:

Sample outputs:

Run fsck on ext4 file system:

OR

Sample outputs:

Reboot the server: