Django with Docker and Gitlab Runner example

In the second post of this series (check the first one!) we will talk about technologies that will be used in our day-to-day development process. A friend of mine wanted to participate on this project and therefore I decided to set-up my old laptop to be a full-time development server that will be running the newest version of my application.

 

I would like to make a CI/CD pipeline that will look something like this:

  1. Run pip freeze >> requirements.txt only if you added/removed/changed versions of any libraries in your virtual environment,
  2. Push new commit into gitlab with some changes,
  3. Gitlab runner should automatically run tests against the changes,
  4. If the tests pass successfully, gitlab runner should build the docker image,
  5. Gitlab runner should deploy the new version (called staging deployment environment).

 

#1 Export all python libraries with their versions defined to the requirements.txt file

Let's say we added/removed/modified the libraries we use in the current project. For example, if I update Django library to the newer version in my virtual environment and use any function from that new version, my colleagues that don't have the updated version of the library will be unable to run the project. We can solve this issue with the of help pip freeze >> requirements.txt command.

This may be a reason why the development server won't work after commit. We need to avoid this!

 

Command pip freeze does one simple thing. It lists all installed libraries in the current virtual environment. The >> symbol in Linux means that the output of the previous command will be redirected into a file. In our case the file is called requirements.txt. Then we can push the updated file into git.

 

It is really easy to install all libraries from requirements.txt file. Just use the following command: pip install -r requirements.txt . We will use this command in dockerfile as you will see in a second.

 

#2 Create docker file

Docker is more or less a new container technology. This technology is beyond scope of this post, so if you don't understand what docker is and you would like to know something more just click here.

 

# Official Python image
FROM python:3.6

# create root directory for project, set the working directory and move all files
RUN mkdir /cryptoapp
WORKDIR /cryptoapp
ADD . /cryptoapp

# Web server will listen to this port
EXPOSE 8000

# Install all libraries we saved to requirements.txt file
RUN pip install -r requirements.txt

The FROM instruction tells docker which image should be used as base image. In this particular case, we specified python 3.6. Although don't forget to specify the version you've been using on your development machine.

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. That means every RUN command will create a new docker image that will be used in subsequent commands.

The ADD command copies files from local folder (in this case whole folder where Dockerfile is located) and copies it to filesystem of the image.

EXPOSE command informs the docker that the container will listen to port 8000.


Now let's move to create docker-compose.yml file.  My pick for database is postgresql so the first service will be db with image: postgres. The second service is django. The most important part is anything behind "command". python manage.py runserver 0.0.0.0:8000 starts python webserver.

 

version: "2"
services:
  db:
    image: postgres
  django:
    build: .
    restart: always
    container_name: cryptoapp
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    depends_on:
      - db
#3 Set up gitlab runner

The last step to achieve our goal is to set up gitlab runner. Gitlab runner has its configuration stored in a file called "gitlab-ci.yml". Let's create the basic config (as you can see, docker-compose command is used, so you need to include the python package docker-compose in your requirements.txt):

stages:
  - test
  - build
  - deploy

test:
  stage: test
  script: echo "Testing the app"

build:
  stage: build
  script:
  - echo "Building the app"
  - docker-compose build

deploy_staiging:
  stage: deploy
  only:
  - master
  script:
  - echo "Deploying the app"
  - docker-compose up -d

As you can see have 3 separate stages.

The first one is just a testing phase, currently just dummy (the script just prints out the "Testing the app" string. The second one is build phase, where we build docker image.

The third phase is the phase where magic happens, that means we run the docker container up and our server should be ready with the database turn on as well.

 

#4 Summary

To sum this up completely, we have working CI/CD pipeline for our project. After every commit to gitlab, this pipeline will start. All steps that gitlab runner makes are recoreded in .gitlab-ci.yml file.

01: Crypto dev info: Idea & Choosing technologies

#1 – A Short Introductory to Cryptocurrency

Bitcoin in the past few years has skyrocketed, reaching nearly $20,000 in value in late 2017. Today, it remains at a steady $7,000 but the value of digital assets has significantly gone up over the past few years. Cryptocurrency was invented by a person or team we only know as Satoshi Nakamoto. The official Wikipedia on Satoshi Nakamoto lists 13 different people as being the official creator for Bitcoin.

 

Not long into the creation of Bitcoin, Satoshi had vanished from the project and was never to be seen or heard from again. Since the creation of Bitcoin, cryptocurrency has surged well above anyone’s original expectations and since then, a lot of new digital assets have been created as well. We’ll discuss the top three digital currencies at this point in time.

 

#2 – The Top Three Most Famous Cryptocurrencies

 

#2.1 – Ethereum

Designed by Vitalik Buterin in 2013, Ethereum has risen quickly among the ranks of digital coins. In 2016, a failed DAO project resulted in Ethereum being separated into two different blockchains. There is now Ethereum Classic and Ethereum. Ethereum is still maintained by Vitalik Buterin but Ethereum Classic doesn’t seem to be going anywhere anytime soon with stagnant development.

 

 #2.2 – XRP

cryptocurrencyXRP was released in 2012 but today, the coin is officially owned by Ripple. Ripple today focuses more on payment processing services and financial institutions. If you want more information on Ripple, you can go to the following blog: https://xrphodor.wordpress.com/or you can go to the official Ripple website.

 

#2.3 – Bitcoin Cash

In 2017, a separate blockchain was created when a small team of developers wanted to increase the blocksize of Bitcoin and they wanted to change the initial code. As a result, a hard fork occurred and this split the coin into two different entities. Now, there’s the original Bitcoin and Bitcoin Cash.

 

#3 – An Overview of The Current Market Evaluation and Progress

For the evaluation of the market and cryptocurrency’s total assets, we can use Market Cap. Market Cap is simply a formula of the total number of coins and the price of each coin. Each individual coin has its own value and we can use Market Cap to evaluate this.

 

For example: If Bitcoin is currently valued at $7,376.41 (changes daily) and there’s 17,249,375 coins in circulation, then the total market cap of BTC is valued at $127,238,394,412.

 

As of 4/9/2018, the total market cap was at $240,129,298,509 which is a significant change from January of 2018 which had a total market cap of $800,000,000,000. The price change can be attributed to the fundamentals of FUD, which are Fear, Uncertainty and Doubt. This is still a huge price jump from 2013 though, because the total market cap back then was only $1,000,000,000.

 

#4 – Market Manipulation and ICO Frauds

However, with the success and rise of cryptocurrency, there’s bound to be some projects that are questionable and some that are downright scams. There’s been a lot of promising projects that turned out to either be not worth their own weight in salt or just outright scams. (Bitconnect comes to mind).

 

My application doesn’t have a name yet but my main mission is to provide development information for all crypto coins. Think of it as a general Wiki for everything cryptocurrency based with information such as how many commits are made, the number of contributors, the number of releases and more. For contributors, that doesn’t just mean the inventor, that means how many software engineers are on the project and how many people are actively working on it at the time being.

 

The main benefit of this project will be so that investors can look up information on a coin, learn a little about their history and decide whether or not this is a good investment for them. With any investment opportunity, you should always due your diligent research but this project will serve as a nice starting point.

 

If you have any new ideas or features you’d like to see in this project, please let me know and I will discuss these ideas with my team to see if we can bring them to life.

 

#5 – The Technology Involved

For the next few posts on this blog, I want to discuss the technology and fundamentals that’ll be going into this project. This will be to provide useful information to developers and people like minded who may be interested in the project. The backend will be programmed in Python with the Django framework. The UI is nice and clean, which was made possible thanks to the use of proper HTML formatting, clean CSS, clean JS scripting and the Bootstrap framework.