Production Grade Docker Compose

Posted On 28 June 2021 ( 5.3 Minute Read )

In this day and age, Docker is the status quo of building applications, allowing us to build and deploy applications using containers. 

Docker is an open-source containerization technology utilizing the concept of containers.

There are dozens of benefits of using containerization technologies like docker including easy packaging and deployment of an application, easy collaboration between developers, easy isolation between systems, simple configuration patterns among many other advantages.

Now if you are a heavy docker user you will know that when building apps on top of docker we quickly run it to a problem, the problem is the management of several different containers quickly becomes complicated entangled.

Docker Compose is a tool that helps us overcome this problem and easily handle multiple containers at once.

version: "3.7"
services:
…
volumes:
…
networks:
…

Let’s see what these elements are.

Services

services refer to the basic containers’ configuration, let say we have an application dockerized and split into different images, so we will add each image as a separate service,

 services: 
      frontend: 
             image: my-react-app
      backend: 
             image: node-app 
      db: 
             image: Postgres 

Volumes & Networks

In order to be able to save (persist) data and also to share data between containers, we use the volumes and networks. you can read more about them here.

https://docs.docker.com/storage/volumes/

Finally, now we have out of the way let us, deep-dive, into some tips and tricks for best practice and smart usage of the technology.

Best Practices

Making use of environment variables

The main advantage of using an external file for your environment variables is that you can keep said file out of your source control. After all, no one enjoys their passwords/API keys/other super-secret information being displayed all over the internet

After you have variables stored in your .env file you can use them directly in the docker-compose.yml file.

Using string interpolation (${string}) we can insert variables directly into our compose file.

version: ‘3.1’

services:
 db:
 container_name: Mongo-db
 image: mongo:latest
 restart: always
 volumes:
 - ./myData:/data/db
 environment:
 - MONGO_INITDB_DATABASE=${DATABASE}
 - MONGODB_USER=${USERNAME}
 - MONGODB_PASS=${PASS}
 ports:
 - 27020:27017

Defining your HEALTHCHECK in Docker Compose

A Health Check means to check the health of any resource to determine whether that resource is operating normally. Here, it is about checking the health of Docker containers. 

     healthcheck:
       test: "${DOCKER_WEB_HEALTHCHECK_TEST:-curl localhost:8000/test}"
       interval: "60s"
       timeout: "3s"
       start_period: "5s"
       retries: 3 

Limiting CPU and memory resources of your containers:

   web:
     deploy:
       resources:
         limits:
           cpus: "${DOCKER_WEB_CPUS:-0}"
           memory: "${DOCKER_WEB_MEMORY:-0}" 

If you set 0 then your services will use as many resources as they need which the default property.

Containerise everything!! : using docker in Ethiopia

Posted On 23 May 2021 ( 8.3 Minute Read )

You might have come across buzzwords such as containerization, docker and wondered what all the fuss was about. And why it is important to use docker in Ethiopia. Well, This tutorial series is just what you need to join the conversation. We will look through the basics of docker and why it might be useful to your workflow. Not only that by the end of this series you will be able to containerize your applications and get them ready for deployment.

What exactly is Docker?

Docker is a containerization platform that packages your app and all its dependencies together in the form called a docker container to ensure that your application works seamlessly in any environment. This environment might be a production or staging server. Docker pulls the dependencies needed for your application to run from the cloud and configures them automatically. You don’t need to do any extra work. Below are some benefits of Using docker

1. It is cost-effective: It takes fewer resources to run your production application. And an even fewer team of developers to maintain it. The biggest driver of most management decisions when selecting a new product is the return on investment. And docker has proven to be a very efficient option.


2. Standardization & productivity
: Docker containers ensure consistency across multiple developments, release cycles and standardizing your environment. One of the biggest advantages of a Docker-based architecture is standardization. Docker provides repeatable development, build, test, and production environments. Standardizing service infrastructure across the entire pipeline allows every team member to work on a production parity environment. By doing this, engineers are more equipped to efficiently analyze and fix bugs within the application. This reduces the amount of time wasted on defects and increases the amount of time available for feature development. Another key use for docker in Ethiopia

3. Compatibility & maintainability: Eliminate the “it works on my machine” problem once and for all. One of the benefits that the entire team will appreciate is parity. Parity, in terms of Docker, means that your images run the same no matter which server or whose laptop they are running on. For your developers, this means less time spent setting up environments, debugging environment-specific issues, and a more portable and easy-to-set-up codebase. Parity also means your production infrastructure will be more reliable and easier to maintain.

4. Rapid Deployment: Docker manages to reduce deployment to seconds. This is because it creates a container for every process and does not boot an OS, unlike virtual machines. Data can be created and destroyed without worry that the cost to bring it up again would be higher than affordable. Also by sharing container reduce dependency install time while using slow network making more use of docker in Ethiopia

5. Continuous Deployment & Testing: If you need to perform an upgrade during a product’s release cycle, you can easily make the necessary changes to Docker containers, test them, and implement the same changes to your existing containers. This sort of flexibility is another key advantage of using Docker. Docker allows you to build, test and release images that can be deployed across multiple servers and cloud services like Kubernetes. Even if a new security patch is available, the process remains the same. You can apply the patch, test it and release it to production.

6. Security: And the last benefit of using docker is — security. From a security point of view, Docker ensures that applications that are running on containers are completely segregated and isolated from each other, granting you complete control over traffic flow and management. No Docker container can look into processes running inside another container. From an architectural point of view, each container gets its own set of resources ranging from processing to network stacks.

Now it’s time to get our hands dirty.  The first thing you need to do is install Docker on your own system and to do that just follow the official guidelines here.

After you have done that clone this sample Project locally. It is a simple Nodejs express server that we are going to containerize using docker. Run the following instructions to clone the repo and start it locally

#clone repo
git clone https://gitlab.com/Dagmawi-A/starterproject.git 

#cd into project directory 
cd starterProject 

#install project dependencies 
npm  install 

The next step is going to be to build our docker image and run it. Don’t be intimidated if the terms seem unfamiliar. We will go in-depth in part two of this tutorial.
Run the commands below

#build our docker image
docker build -t <your-name-here>/sampleproject:v1

#run our docker image and expose ports
docker run -p 8000:8000 <your-name-here>/sampleproject:v1

Now that we have got our application running in our Docker container we can access it locally in our browser. Just go to this link.

Congrats you’ve just created your first docker container. Stay tuned for part 2 !!

Web development in Ethiopia using Kubernetes

Posted On 23 May 2021 ( 8.7 Minute Read )

Why is it important we discuss Kubernetes in Ethiopia.

The main struggle in Web development in Ethiopia is a proper server infrastructure. If you have been at any governmental agencies to get any kind of work approved you probably have stayed for hours waiting for a system to get back online. One of the main reason for this kind of issue is the servers running one version of application. Whenever a bug or uncaught error is found in the server the entire city waits for the servers to restart. This phenomenon is also known as system shortage(system yelem). By creating a stable infrastructure we can minimize the number of hour spent waiting in line.

What is Kubernetes ?

The name Kubernetes originated from Greek meaning pilot, This software released by google as an open-source software in 2014 allows one to freely manage containerized applications and services.

A simple way to view it is to compare it to a symphony orchestra. Where there is one conductor instructing in who, how high and when each musician and instrument should play.  Each instrument and musician represents a containerized application or service.
The conductor also called the master node will instruct the musicians in kubernetes terminology called pods, services, ingress to work together in a perfect harmony. 

Kubernetes makes it easy to manage hardware resources, scale applications and manage release. With almost no downtime it is a good way to roll out a new version without system shortage. It also solves the Hardware shortage by only allocating resources to the application that is currently in need of it. No more system shortage  and smoother web development in Ethiopia.
Without going deep into how it works we will discuss the main idea of kubernetes and why we use it in our development process in Addis software.




Recommend to installing minikube for testing kubernetes on local development.

Kubernetes cluster
A cluster is network of virtual or physical machines known as nodes. Nodes are designated hardware space to run the kubernetes application and all the applications running on each node. Each application runs wrapped within temporary VMs called pods. A promotion assigned to one of the nodes to be a master node. A kubernetes cluster contains a masted node, at least one worker node, pods running inside each node

Master node

Main task is to  automatically handles scheduling the pods across the Nodes in the cluster from a given rule    

kubectl describe node <insert-node-name-here>

Deployment

Is Kubernetes api to manage a deployment  and allows you to describe an application’s life cycle, such as which images to use for the app. An example of a simple configuration file. 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      run: my-app
  template:
    metadata:
      labels:
        run: my-app
    spec:
      containers:
      - name: hello-app
        image: gcr.io/google-samples/hello-app:1.0

To run the above deployment save the above snippet in a yaml file and run kubectl apply -f deploymentFileName , now you have created a deployment

To check the status of the deployment run kubectl describe deployment [DEPLOYMENT_NAME]If you have forgotten your deployment name then you can run  kubectl get deployments to view a list of deployments. When a deployment is done it will create a pod running your application. When it’s time for you to scale up your application you can simply run the following command with the number of replicas you want to create kubectl scale deployment [DEPLOYMENT_NAME] –replicas [NUMBER_OF_REPLICAS] and  to remove a deployment kubectl delete deployment [DEPLOYMENT_NAME]

Pods

A pod is an isolated space where a containerized application runs in, it is viewed as a minimal running unit. it’s also mostly viewed as a temporary unit rather than a permanent one. It is also considered as the final resting place for your application code. Each pod is assigned an id, internal and external ip, and the containers will share the pods networks space. 

Once a deployment is done, you should be able to see the pod list by running kubectl get pods.

  • Pending: a pod have been accepted by kubernetes master and is in progress
  • Running: a deployment successfully started a pod
  • Succeeded: a pod is successfully terminated and will not be started
  • Failed: a container within the pod have some kind of error and could not be started
  • Unknown: the status of the pod is unknown

Service 

Service is kubernetes api to manage a running containers network, as pods are created and destroyed depending on the master node instruction a service main task id to track and manage the network for each pod or set of replicas.

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

The above example shows a simple service that looks into all pods running with a label called MyApp and designate a TCP network port 9376 to those pods port 80

Ingress

Is kubernetes api to connect the external world to each specific service. 

Finally

Use Kubernetes to enhance your application and platform in web development in Ethiopia

The magical link between developers and music.

Posted On 05 February 2021 ( 3.2 Minute Read )

Music and developers

There is no denying that music is one of the building blocks of software development. It is, on most occasions, what keeps us sane and holds us together on heavy-work days.

For those of us who like to mix our productivity with a playlist running in the background, music has a special place in our hearts. It is almost as if it brings out the best in us — our “inner creative”.

Although the pros of music are endless, some do it for the energy it brings them, others use the headsets to warm up their ears in cold seasons, and most use it to cancel out background noise and focus on their work — their noisy way to silence!

Whichever the reason, music is bliss.

Research showed that the quality of work was at its lowest without music, and suggests that playing your favorite track while working can improve mood and productivity. But music can also be distracting at times, you could waste valuable time finding the perfect songs which fits your mood, creating playlists, and even adjusting volumes, etc. After all, haven’t we been in a situation where we accidentally typed in the lyrics of the music we were listening to?  

For these reasons, it is suggested that listening to instrumental music works best when working. Because, you guessed it, there’s no lyrics to “accidentally” type, and the best you can do is nod your head!

Our recommendation: 

Lo-Fi or low-fidelity music, is a kind of instrumental music with a relaxed aesthetic touch. It fits very well into the developer work style, perfect for when pulling an all-nighter, and it doesn’t interrupt your focus. A triple threat!

Note that this is not always the case. Memorizing new information is best done with no music, and hence make sure you are working, and not learning a new language, etc.

Our Lo-Fi favorites: