DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Kubernetes - Day - 07 - Create App

Simple App

app.py


import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return f"{os.getenv('GEETING','HELLO')} from {os.getenv('APP_ENV', 'dev')}!"

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5001)
Enter fullscreen mode Exit fullscreen mode

requirements.txt

blinker=1.9.0
click=8.1.7
Flask=3.0.3
itsdangerous=2.2.0
Jinja2=3.1.4
MarkupSafe=2.1.5
Werkzeug=3.0.3
Enter fullscreen mode Exit fullscreen mode
  • For this simple application we are setting ENVIRONMENTAL variables.
  • GREETING & APP_ENV

  • Using variable change the app

Going to convert this Application into IMAGE

  • Dockerfile ( default name , but you can change this also )
  • What we need to create image?
  1. OS
  2. Up to date packages available
  3. Having working directory eg., docker1_project
  4. project files
  5. Installation detail files
  6. Installed packages
  7. Env variables
  8. Port should be available

Docker File

  • Everything should start with OS only i.e FROM
  • which is the base image.
FROM alphine:3.20
Enter fullscreen mode Exit fullscreen mode

  • This Alphine is given by Linux but in a samler version
  • Only basic will be available.
CMD ["echo","Hello World"]
Enter fullscreen mode Exit fullscreen mode
  • Initial command that is going to be executed inside the container.
  • Command will get executed and then it will die.
  • Why we are having the echo in square bracket ? Like LIST.

echo Hello-world

Hello-world

  • There are two format available.
  • Shell
  • Exec
  • CMD [] --> Its a Exec Format.
  • Exec is recommended format.

Let's build an image from docker file name


docker build -f alphine.Dockerfile -t alpine-test-myimage:1.0 .

f --> filename-default is --> Dockerfile
t --> tag
build --> command to , the image is ready 
. --> Current Directory or Build Content
Enter fullscreen mode Exit fullscreen mode


FROM alpine:3.20
CMD ["echo", "HELLO-WORLD"]
Enter fullscreen mode Exit fullscreen mode
  • These are layers , while we execute the command , you will get LAYERS.
  • Here , layers are
FROM alpine:3.20 --> LAYER 1
CMD ["echo", "HELLO-WORLD"] --> LAYER 2
Enter fullscreen mode Exit fullscreen mode

  • Layers are visible , you can see the word LAYERS.
docker build -f alphine.Dockerfile -t alpine-test-myimage:1.0 .
docker run alpine-test-myimage:1.0 ( executed and died )
docker ps -a
Enter fullscreen mode Exit fullscreen mode
  • How this is executed , notice that !!!

cat alphine.Dockerfile
FROM alpine:3.20
CMD ["echo", "HELLO-WORLD"]

  • It got concatenated and the output is printed like below

  • Now , lets update the docker file.
FROM alpine:3.20
CMD ["echo", "HELLO-WORLD 12345 "]
Enter fullscreen mode Exit fullscreen mode

  • If there is no change , then it won't change.
  • All will be cached, so it won't run again completely.

Build Docker File - Python

  • In python --> Slim , python.
  • CMD & ENTRY point --> Those EXE Script used only for these two commands.

Lets build the image

  • Its only for document purpose - EXPOSE - documentation.

  • It won't open because its not exposed. The EXPOSE is only for documentation purpose.

docker build -t flask-greetings:1.0 .

  • host port : container port

docker run -p 5001:5001 flask-greetings:1.0

  • For changing the ENV variable , no need to edit the Dockerfile rather you can send in the command itself.

docker run -p 5001:5001 -e GREETING="Vanakam Chennai DR Site" flask-greetings:1.0

  • E for Environment Variable


docker ps -a
docker inspect 3aefbecc5ef5
Enter fullscreen mode Exit fullscreen mode

  • RUN for the BUILD PART or IMAGE BUILD
  • CMD for CONTAINER RUN TIME ( Container start pannathum enna nadakanum )
  • Once the machine starts what is the process ID - app.py is the PID 1.
  • CMD only captures the SIGNAL. like kill -9
  • ENTRY POINT won't capture the signal.

  • Delete all images and check.

docker rm -f $(docker -ps aq)
Enter fullscreen mode Exit fullscreen mode

  • Change the below and check how its working

RUN THE Application

CMD ["python" , "app.py" ]

CMD python app.py --> shell format. --> the signal should not go.

  • Stop is like ( in shell format ) SIG KILL. Always in LIST Format.
  • In Dockerfile , we need to use EXEC format , இந்த format மட்டும்தான் யூஸ் பண்ணனும் . இப்ப shell use பண்ணீங்கன்னா , signal போகாது containerகு . But it will work , ஆனா STOP பண்ணும் போது , i mean docker stop shutdown ஒழுங்கா நடக்காது. SO ALWAYS USE EXEC FORMAT.

Commands

    python3 -m venv .venv
    source .venv/bin/activate.fish
    source .venv/bin/activate
    python -m pip install flask
    uvicorn app:app --reload
    uvicorn app:app --reload
    sudo apt install uvicorn
    uvicorn app:app --reload
    sudo apt install flask
    pip install flask
    uvicorn app:app --reload
    pip list
    python app.py
    export GREETING='HI'
    export APP_ENV='I am PROD Env'
    python app.py
    python -m pip freeze > requirements.txt


docker exec -it <run_docker> bash
docker stop <run_docker>

Enter fullscreen mode Exit fullscreen mode

Error

  • Run the python file alone.

  • Issue in the file name of CMD.

TBD

Top comments (0)