Dockerize your own Game of Thrones' API
Tags: docker, python, terraform, api
Published: 2021-12-29 14:40:43

This article will demonstrate a fun and useful use case of docker, where we will create and deploy to production a custom-made API. In our case, it will provide information about the episodes of the TV show “Game of Thrones”. Besides Docker, our stack will include:

|Tool|Description|Purpose|
|--|--|--|--|
|Flask|Python| microframework|Create the API project itself
MongoDB|NoSQL database|Store the API data
Digital Ocean|Cloud provider|Host the server where the API will be deployed to
|Terraform|IaC (Infrastructure as code) tool|Create and manage the cloud server
|Docker Compose V2 Plugin|Define an run multiple containers|Instantiate and connect our containers
|Postman|API platform|Create and publish the API| documentation and available endpoints|

## About the API
The API will serve information about episodes and comments about them. There are endpoints for posting, deleting and editing those comments as well. The documentation can be accessed [here] (https://documenter.getpostman.com/view/18638297/UVJhDEuV#3ab23a54-fbbe-49f8-846a-d0a4c3756591). Also, its raw .json version is in the repository, just in case the link is no longer up.

## About Terraform
This is an optional part for the project, but it is highly recommended as it demonstrates how easily we can automate the process of creating and managing cloud services. Here, we used it to create a server accordingly to our specification (Ubuntu 20.04, 1 CPU, 25 GB Disk), besides that, a SSH key was added to the server, so we can access it without being prompted for passwords. Learn more about SSH [here] (https://dev.to/costa86/managing-remote-servers-with-ssh-connection-using-windows-and-linux-45im).

## About Docker Compose V2 plugin
Instead of the well-known “docker-compose”, we will use its new version, which now is accessible by the command “docker compose” (space instead of dash). This new tool is built in Golang, so it is faster than the former (built in Python). It also includes new features, such as the “profiles” attribute, that can be used to create groups of containers we wish to run from the docker-compose.yml file. Learn more about Docker Compose V2 [here](https://www.youtube.com/watch?v=2MJn2yfa6A8).

## Sequence diagram
This sequence diagram illustrates the steps that will be taken in the whole process of deploying our API.


![Sequence diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urny21pdl70fchjaylmn.png)

In short, Terraform will create the new server on Digital Ocean, then, from the new server, we will clone the API repository from GitHub and start the containers with “docker  compose”. By doing so, the API will be available on the internet, via the server’s public address on port 5000 (used by the Flask project).

## Video demonstration
This project was presented at Docker Community All-hands, on Dec 09, 2021. Check the video [here](https://www.youtube.com/watch?v=fy6pXminRLU)

## Considerations
The remote server was accessed by using `root`, which is the default user set when the new server instance was created. Having `root` remote accessing a server is not considered a good practice, The alternative is to create a new user for that purpose and disable root access on the server (the SSH article referenced above demonstrates how to to so).
Regarding the API access, if you wish to use a similar project for professional/commercial usages, it is recommended to implement some mechanism to limit/control the API requests. There are several options out there for this purpose.

## Further information
API Repository: https://github.com/costa86/game-of-thrones-api
Digital Ocean referral link (get $ 100 in credit): https://m.do.co/c/76fe367e961f
About the author: https://costa86.com/