Bilal Shafiq
@mrbilalshafiq

Follow

@mrbilalshafiq

Follow
11. HANDS-ON TUTORIAL: Deploying Loosely-Coupled Microservices as Part of a Cloud-Native Application using Docker & Docker-Compose

11. HANDS-ON TUTORIAL: Deploying Loosely-Coupled Microservices as Part of a Cloud-Native Application using Docker & Docker-Compose

A modernised application built upon microservice architecture

Bilal Shafiq's photo
Bilal Shafiq
·Jan 19, 2023·

3 min read

Table of contents

Introduction

This tutorial will guide you through the process of deploying a cloud-native application using microservices architecture and Docker containers. We will be using the popular open-source tools Docker and Docker-Compose to build and manage our microservices.

The application we will be building will consist of four services:

  • Service 1 is the front-end core service that will render the Jinja2 templates and communicate with the other three services.

  • Service 2 generates a 3-letter component of an account number.

  • Service 3 generates a 7-digit component of the account number.

  • Service 4 combines these two components to form the final account number and also determines the prize based on the logic coded into it.

By the end of this tutorial, you will have a solid understanding of how to install Docker & Docker-Compose and how to deploy a cloud-native application using microservices with Docker containers.

Tools Utilised:

  • Ubuntu

  • Docker

  • Docker-Compose

  • Git

  • GitHub

  • Python

  • Flask

  • MySQL

  • NGINX

Step-by-Step Guide

Install Docker

1. Update and install the necessary packages

sudo apt update && sudo apt install curl -y

2. Use the official Docker script to install it

To install Docker on Linux, we can use an official script from get.docker.com:

curl https://get.docker.com | sudo bash

3. Add the current user to the group

The script installs Docker and creates a Docker user group with access to the commands. This means that Docker can only be used with sudo privileges or if the current user is in the Docker user group:

sudo usermod -aG docker $(whoami)

4. Give /var/run/docker.sock correct permissions

sudo chmod 666 /var/run/docker.sock

5. Test Docker works

docker run --rm hello-world

The --rm flag removes the container when it exits or when the daemon exits.

Install Docker-Compose

6. Install the necessary packages

sudo apt install -y jq

7. Set the version to download

version=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r '.tag_name')

8. Download to /usr/local/bin/

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

9. Make the file executable

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

10. Verify the installation by checking the version

docker-compose --version

Deploy Microservices in Containers

11. Clone the repo and go into the folder

git clone https://github.com/mrbilalshafiq/DevOps-Project-2 && cd DevOps-Project-2/

12. Start containers using docker-compose.yaml

docker-compose up -d

13. View Containers

docker ps

14. Find your public IP

curl ifconfig.me

15. Enter your public IP in your browser

Example:
3.8.95.11
You don't need to enter the port number because of the nginx.conf file in the NGINX folder. (Have a look!)

16. Test the application

Clean Up

17. Remove all running and stopped containers

docker rm -f $(docker ps -aq)

Conclusion

In conclusion, deploying microservices in containers is a great way to build and manage a cloud-native application. By breaking the application into smaller, loosely-coupled services, each with a specific responsibility, we were able to create a highly-scalable and maintainable system. Services communicate with each other through a network, using a well-defined API, which ensures loose coupling and ease of scaling.

By using Docker and Docker-Compose, we were able to easily deploy and manage these services, making it simple to scale and update the application as needed. Overall, this approach provided a lot of flexibility and simplicity, and it is a great way to build a cloud-native application using microservices.


Did you find this article valuable?

Support Bilal Shafiq by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this