This article will demonstrate a fun and useful docker use case, where we will build and deploy to produce a custom API. In our case, it will provide information about episodes of the TV show “Game of Thrones”. Besides Docker, our stack will include:
The API will provide information about and comments about episodes. There are endpoints for posting, deleting, and editing these comments as well. The documents can be accessed here. Also, its raw .json version is in the repository, just in case the link doesn’t work.
This is an optional part of the project, but is highly recommended because it shows how easy it is to automate the process of creating and managing cloud services. Here, we used it to create a server according to our specifications (Ubuntu 20.04, 1 CPU, 25 GB disk), and additionally, an SSH key was added to the server, so we can access it without asking you for passwords. Learn more about SSH here.
About Docker Compose V2
Instead of the well-known “docker-compose” we will use its new version, which can now be accessed by the command “docker compose” (a space instead of a dash). This new tool is built in Golang, so it’s faster than the previous one (built into Python). It also includes new features, such as the “Profiles” attribute, that can be used to create groups of containers that we want to run from the docker-compose.yml file. Learn more about Docker Compose V2 here.
This sequence diagram shows the steps that will be taken in the entire process of deploying our API.
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 this, the API will be available online, via the server’s public address on port 5000 (used by the Flask project).
The remote server was accessed using ‘root’, which is the default user created when the new server instance is created. It’s not a good practice to remotely “root” the server, the alternative is to create a new user for the purpose of thar and disable root access on the server (the SSH article referenced above shows how).
Regarding API access, if you want to use a similar project for professional/commercial uses, it is recommended to implement some mechanism to limit/control API requests. There are many options available for this.
API Repository: https://github.com/costa86/game-of-thrones-api
About the author: https://costa86.com/
This is a guest blog post from Docker community member Lorenzo Costa. The blog originally appeared here. A video demo of this post from the Docker Community All-Hands community can be found here.
Join us at DockerCon2022 on Tuesday, May 10th. DockerCon is a free, one-day virtual event that is a unique experience for developers and development teams building the next generation of modern applications. If you want to learn how to move from code to the cloud quickly and how to solve your development challenges, DockerCon 2022 offers live content to help you build, share, and run your applications. Register today at https://www.docker.com/dockercon/