Port Binding : Facing a Issue

I am Learning 12 Factor App , " If we are running multiple instance of our application on same server" What does multiple instance means ?

It means running two copies (instances) of the same program (the app) on the same server. Only one application on a server can own a port, therefore each copy of the app you run must use a different port number - which it will get from configuration.

Each instance of the app will have its own private copy of the configuration, and the port number to use will be different for each.

1 Like

If multiple instance is bind and configured " Our App export HTTP as a service by binding to a specific port and listen incoming request on that port ,now unlike traditional web app, The 12- factor app is completely self contained , Does not rely on specific server to function " Explain this too.

Which lecture is this (link please)

1 Like

Facing Issue in Port Binding Explanation,

https://learn.kodekloud.com/user/courses/12-factor-app/module/086a3d2d-be7f-4b05-92ae-1b2e4ab90f6a/lesson/c1850baa-bddf-4f55-977c-cb3c9234ee15

A “traditional web app” in this lecture refers to applications that are run by specific web servers like Apache, nginx or Microsoft IIS. These three are the most popular web servers and apps that you run on them might be PHP or JavaScript based or numerous other languages. Web servers host applications using an interface called CGI (common gateway interface).

The 12 factor app does not require one of these web servers to host it. It has HTTP serving capability built into it. This is what the Flask library referred to in the lectures provides to Python code. The flask library can bind a port and listen for HTTP requests. This is known as “self-hosting”, because it doesn’t require something like Apache to host it.

In that lecture we have a small python flask program, and because it is currently NOT specifying a port to bind on, it will use the default for flask which is 5000.

Let’s make a change to that code to allow it to get a port from configuration and bind with that

from flask import Flask
from redis import Redis

app = Flask(__name__)
redisDb = Redis(host=os.getenv('HOST'), port=os.getenv('PORT')

@app.route('/')
def welcomeToKodeKloud():
    redisDb.incr('visitorCount')
    visitCount = str(redisDb.get('visitorCount'), 'utf-8')
    return "Welcome to KodeKloud! Visitor Count: " + visitCount

def get_port_number() -> int:
    # This would contain code to read the port number 
    # from the app's configuration file
    # and return the integer value which is the port number

if __name__ == "__main__":
    app.Run(host="0.0.0.0", port=get_port_number(), debug=true)

Here we assume that the app has a configuration file, and the function get_port_number is coded to retrieve the port number we should bind to from that configuration, then app.Run will bind to the requested port instead of always 5000.

Note that host="0.0.0.0" means that the app should be reachable via all network interfaces to the server.

To do this course it helps to have some basic understanding of writing software and how networks work in terms of hosts, ports etc.

1 Like