In previous lesson we installed Postman & PyCharm CE. We will use these software now. Now open VirtualBox and start Ubuntu 20 Tutorial (virtual server).

First create a folder named ‘tutorial’ in your desktop. Mouse Right Click > New Folder

Alternatively, you can use Terminal and create tutorial folder by command. I will suggest you to use command line because we will use command frequently from now.

To do this, Open terminal. Write the commands below:

 cd Desktop
 sudo mkdir tutorial

Terminal may ask for your password while creating new folder in Desktop. Enter the password of you virtual machine.

Open Pycharm CE > Open > Browse and Find tutorial folder > Select tutorial folder

Now we have to create some folders and files inside our tutorial folder or project

Select tutorial > New > File and create a file named docker-compose.yml

Warning : You may face an error while creating new file

Solution: Just open Terminal. Go to Desktop and then go to tutorial folder, modify the permission of that folder.

 cd Desktop
 cd tutorial
 sudo chmod -R 777 ./

Open docker-compose.yml file and paste the code below into docker-compose.yml

version: '3'
services:
  web:
    build: ./web
    ports:
      - "5000:5000"
    links:
      - db

  db:
    build: ./db

I will explain this code later because I don’t want to bring something complex and confusing to you right now! Just copy and paste the code.

Select tutorial folder and create 2 sub-folders or directories.

  • web
  • db

Create a file named Dockerfile inside the db folder and paste the code below:

 FROM mongo:3.6.4

In the same way, create a file named Dockerfile inside the web folder and paste the code below:

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "app.py" ]
EXPOSE 80

Create another file in web folder named requirements.txt and paste the code below into that file :

Flask
flask_restful
pymongo

I am skipping the code explanation of these files. Don’t worry, I will describe in details later!

Now create the last file named app.py inside web folder. Your project will look like this :

Now paste the code below into you app.py file.

from flask import Flask, jsonify
from flask_restful import Api, Resource
from pymongo import MongoClient


app = Flask(__name__)
api = Api(app)


client = MongoClient("mongodb://db:27017")
db = client["TestDB"]
testCollection = db["testCollection"]



# -- Welcome API
class Welcome(Resource):
    def get(self):
        # Show a welcome greetings
        retJson = {
            "status": "ok",
            "msg": "Welcome Turzo! Your Python & MongoDB based API server is working successfully!"
        }
        return jsonify(retJson)





api.add_resource(Welcome, '/welcome')


if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')

We have to install Docker and Docker-Compose into our virtual machine. You can visit their official site to install Docker and Docker-Compose.

How to install Docker?

How to install Docker-Compose?

or you can use the commands below. Just open Terminal and run the commands below:

How to install Docker?

 sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

If everything downloaded successfully. Run the command below to check if the docker installed successfully to your machine or not. This command will download the Docker Hello-World project and show the message Hello From Docker! in the terminal window.

sudo docker run hello-world

How to install Docker-Compose?

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now check the docker-compose version by running the command below:

docker-compose --version

If this command shows something like below then you installed docker-compose in your machine successfully.

docker-compose version 1.29.1, build c34c88b2

Now open the Terminal again, Go to Desktop > tutorial and run the command below to build your project:

sudo docker-compose build

This command will start building your project instantly. Please wait patiently until build is complete, it will take some minutes or more based on your internet speed.

After successful build run the command below to start your server.

sudo docker-compose up

This server will start as Docker Container but you shouldn’t worry about this now, I will explain everything gradually.

We are ready to test our server and first API. Open a browser in your virtual machine (Ubuntu 20 Tutorial). Paste the URL below into your browser’s tab:

localhost:5000/welcome

You will get a nice and simple response as JSON format if you hit the URL.

Response:

{
  "msg": "Welcome Turzo! Your Python & MongoDB based API server is working successfully!", 
  "status": "ok"
}

Hurrah! We have created our first API today. Congratulation!

Next Lesson →


Author :

Mahbubur Rahman Turzo

Full Stack Developer

Hi, I am Turzo. As a computer professional I worked for more than 6 years in Backend server development, iOS development, Android development, Game development, Robotics. I will guide you to acquire skills easily, efficiently and perfectly. I hope you will use these skills and knowledge in your real career as well as spread among others!

Leave a Reply

Your email address will not be published. Required fields are marked *