Skip to content

Sir-Chester-King/Python-Application-Using-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Application Using Docker

Table Of Contents


Description

This application is a simple a quiz game, as education purpose to take conscious of how application is used within a container using the Docker technology to containeraize the application it self.
The application ask to an user to answer a simple question via terminal command (NO GUI).
The application is write in Python and use the user input via command terminal to continue the usage.


Main Application

The application ask to an user to answer a simple question via terminal command (NO GUI), and it wrote in Python.
Questions available are:

questions = {
    "Who is the first American's President?": "B",
    "When was the second world war?": "A",
    "In which country was invented the pizza?": "C"
}

As you can see, it was used a dictionary for the questions to use the pair Key: Values to bind the correct answer with the correct question.

Main.py

This function is structured with the questions and the correct answers, store in the main body of application.
After that, it call another function called New_Game.py

New_Game.py

Thsi function use a nested FOR cycle to iterate first one, the questions dictionary (to show one question per time), and the second one, the answer (to show all possible answer per question).

    # Iterate over the questions and print them
    for key in questions.keys():
        print(F"Question number {count_questions}: {key}")
        print("Options:")

        # So, question number 1 ---> answer number [question - 1]
        for index in possible_answer[count_questions - 1]:
            print(index)

The variable index "count_questions" start from one 'cause it was used to show the number of question showed to user (ex: question number 1°).

Display_Score.py

This function show the answer that user choose during the game, and all the correct answer for all the questions. It shiw the score percentage of result too.

print("Answer given: ", end="")
for index in answer_given:
    print(index, end="")

print("\n")
print("Correct answer: ", end="")
for index in list_question_value:
    print(index, end="")
score = (correct_answer / len(questions_value)) * 100
print("\n")
print("Score: ", score)

Check_Answer.py

This function check only if the answer that user given during the game it was right.

def check_answer(questions_key, answer_input):
    if questions_key == answer_input:
        print("Correct Answer !!!\n")
    else:
        print("Wrong Answer !!!\n")

Play_Again.py

This function call the New_Game.py when the game reached the end, and the user wants to play again.

def play_again(questions, possible_answer):
    New_Game.new_game(questions, possible_answer)

Dockerfile

This file contain all commands used to build the Image that Containers will use.
The Image is a snapshot of the source code, and when it did build, the Image is in read-only mode, and you cannot change the code. If you want to create a container based to the new image, you must re-build the image.

Command Image

The commands used to build the image that it'll be used to create the container that has the code, you must declare some parameters.
In this image it used the following commands:

  • FROM
  • LABEL
  • WORKDIR
  • COPY
  • CMD

The FROM command it used to pull all dependenties based on the image that we pass as a parameter.
In this case, we defined an image for a Python appl, therefore with this command, we pull oll the dependenties from the official Python Imgae, stored in the Docker Hub.

FROM python:latest

The word " latest " define to use the latest versione of the image we want to pull.

The WORKDIR command it used to define our work directory that all the next following command in the Dockerfile will be executed.

# This is the directory where all the following commands will be executed (COPY, RUN, CMD will be executed in the directory WORKDIR specified)
WORKDIR /Docker_Directory

The COPY command it used to say to Docker, that it must copy all the file stored in the same directory of Dockerfile, to some directory in the container (that we pecified)

# This command define to copy all the file located in the current directory (" . ") which in the Dockerfile is created
# In this case is in the Project APP Python; and copy all files into the specified directory (copy all file in the sub-directory "./Image_DIrectory").
# The copy will be placed in the /Docker_Directory/Image_Directory directory.

# Equivalent command -> COPY Local_Path_Where_Dockerfile_Is_Placed /Docker_Directory/Image_Direcotry
COPY . ./Image_Directory

The CMD command it used to say to Docker to run the command we specified in the dockerfile.

# This command define to run the "main.py" file located under the sub-directory definetd
CMD ["python","Image_Directory/Source_Code/main.py"]

Build Image

To build image, you must use the BUILD command, and pass where the dockerfile is stored, as a parameter.
It be the result.

docker build .

Alt text To view the image was builted, you can view with the following command:

docker image ls

Alt text

Run The Container

After you successfully build the Image, you can create and run the Container will contain the python app.
To crate the container, you must use the following command:

docker run --name Container_App_Python -it 296ac232d224

If you see a stranger string (296ac232d224), it's 'cause, it is the ID of Image builted previously.
When you create the container, the app start immediatly, 'cause, in the Dockerfile we declared a CMD command the run the "main.py" file.
Alt text If you wanna see the list of container created, you must use the following command:

docker ps

if you wanna see the list of container that no longer used, for example, such as it was terminated 'cause the app in the container finished the work.
You must use the following command:

docker ps -a

About

Python application using the Docker container technology to run the app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published