Sample Python Code For To Build Application
Create a directory named scaling and open that directory on the code editor of your choice.
First, create a Main.py file and copy-paste the below code :
Python
from flask import Flask import socket app = Flask(__name__) @app .route( '/' ) def get_hostname(): hostname = socket.gethostname() return f "Hostname: {hostname}\n" if __name__ = = '__main__' : app.run(debug = True , port = 5000 ) |
The above script will run a Flask server on port 5000. Now create a Docker image from the above file using a Dockerfile. Create a file named Dockerfile without any extension in the scaling directory.
Copy and paste the below code into that file:
FROM python:3.8
RUN pip install flask
COPY app.py /app.py
CMD [“python”, “app.py”]
Now create a Docker image which will be stored on your machine using the below command :
docker build -t flaskapi .
How to Use Docker Images For Scaling Applications?
Docker image is a lightweight piece of software that includes everything that your application needs to run, it includes code, software packages, and libraries. Docker images are built using a Docker file which includes the instruction for creating the image. The Docker image is built on top of a base image which is specified on top of the Dockerfile.
Docker images are typically stored in the docker registry such as the DockerHub, or Artifactory. Dockerfile is a source code for Docker images.