From 10d3eec647158a84d6f33ae4ba4c9fe3b07bdda7 Mon Sep 17 00:00:00 2001 From: haraphat01 Date: Mon, 5 Jun 2023 09:56:54 -0700 Subject: [PATCH 1/6] created the project foler --- app_python/main.py | 17 +++++++++++++++++ app_python/requirements.txt | 2 ++ 2 files changed, 19 insertions(+) create mode 100644 app_python/main.py create mode 100644 app_python/requirements.txt diff --git a/app_python/main.py b/app_python/main.py new file mode 100644 index 000000000..00c8b1f82 --- /dev/null +++ b/app_python/main.py @@ -0,0 +1,17 @@ +from datetime import datetime + +from flask import Flask + +app = Flask(__name__) + +def get_current_time(): + now = datetime.now() + current_time = now.strftime("Current Time in Moscow: %Y-%m-%d %H:%M:%S") + return current_time + +@app.route('/') +def home(): + return get_current_time() + +if __name__ == '__main__': + app.run(host="0.0.0.0", debug=True) \ No newline at end of file diff --git a/app_python/requirements.txt b/app_python/requirements.txt new file mode 100644 index 000000000..496a52930 --- /dev/null +++ b/app_python/requirements.txt @@ -0,0 +1,2 @@ +pytz +Flask==2.0.1 From ea7d0ac23abe2b30b9f9571face78e8d099bf9bc Mon Sep 17 00:00:00 2001 From: haraphat01 Date: Mon, 5 Jun 2023 09:59:31 -0700 Subject: [PATCH 2/6] Submitting the first lab for review --- app_python/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app_python/README.md diff --git a/app_python/README.md b/app_python/README.md new file mode 100644 index 000000000..10fe0e7c5 --- /dev/null +++ b/app_python/README.md @@ -0,0 +1,12 @@ +# Current Time in Moscow + +## Overview +This is a simple Python web application that displays the current time in Moscow. It uses the Flask web framework and the datetime module to get the current time in the Europe/Moscow timezone. The time is displayed in a user-friendly format on a web page. + +## Build +To build this application, you need to create a Python file with a main.py extension, for example, app.py. Then, copy the code in main.py to your file. Save the file and run it using the command python app.py in a terminal or command prompt. +The application will start running on your local machine. + +## Usage +To use the application, open a web browser and navigate to http://localhost:5000/. The current time in Moscow will be displayed on the web page. The time will be updated every time you refresh the page. + From 730c9a49676a55c4ff1722d30285895d3fcc4316 Mon Sep 17 00:00:00 2001 From: haraphat01 Date: Mon, 5 Jun 2023 10:11:48 -0700 Subject: [PATCH 3/6] updated the readme with the docker instruction and also added docker to the project --- app_python/Dockerfile | 15 +++++++++++++++ app_python/README.md | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 app_python/Dockerfile diff --git a/app_python/Dockerfile b/app_python/Dockerfile new file mode 100644 index 000000000..c0c54690e --- /dev/null +++ b/app_python/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3-alpine + +WORKDIR /main + +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +EXPOSE 8081 + +CMD ["python3", "main.py"] + + diff --git a/app_python/README.md b/app_python/README.md index 10fe0e7c5..0be939514 100644 --- a/app_python/README.md +++ b/app_python/README.md @@ -10,3 +10,22 @@ The application will start running on your local machine. ## Usage To use the application, open a web browser and navigate to http://localhost:5000/. The current time in Moscow will be displayed on the web page. The time will be updated every time you refresh the page. +## To run the Dockerfile in your project, you can follow these steps: + +1. Make sure that you have Docker installed on your local machine. You can download Docker from the official website: https://www.docker.com/products/docker-desktop + +2. Open a terminal or command prompt and navigate to the directory that contains your Dockerfile. + +3. Build the Docker image using the following command: + docker build -t . + Make sure to replace with the name that you want to give to your Docker image. The . at the end of the command specifies that the build context is the current directory. + +4. Once the build is complete, you can run the Docker container using the following command: + docker run -p : + +Make sure to replace with the port number on your local machine that you want to use to access the container, with the port number that your application is listening on inside the container, and with the name of the Docker image that you built in step 3. + +For example, if your application is listening on port 80 inside the container and you want to access it on port 8080 on your local machine, you can use the following command: + docker run -p 8080:80 + +5. Once the container is running, you can access your application by opening a web browser and navigating to http://localhost:. In the example above, you would navigate to http://localhost:8080. From 2d1d1a4389e302fc2f8e57210aadc31fd908b4bd Mon Sep 17 00:00:00 2001 From: haraphat01 Date: Sat, 15 Jul 2023 23:36:19 -0700 Subject: [PATCH 4/6] used mulitstage build for my app --- app_python/Dockerfile | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app_python/Dockerfile b/app_python/Dockerfile index c0c54690e..d7d3ef024 100644 --- a/app_python/Dockerfile +++ b/app_python/Dockerfile @@ -1,6 +1,7 @@ -FROM python:3-alpine +# Build stage +FROM python:3.9-alpine AS builder -WORKDIR /main +WORKDIR /app COPY requirements.txt . @@ -8,8 +9,15 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . -EXPOSE 8081 +# Runtime stage +FROM python:3.9-alpine + +WORKDIR /app -CMD ["python3", "main.py"] +COPY --from=builder /app . +RUN pip install flask + +EXPOSE 8081 +CMD ["python3", "main.py"] \ No newline at end of file From 29419675ed8a517bb729773522af6cb959a3d365 Mon Sep 17 00:00:00 2001 From: haraphat01 Date: Tue, 18 Jul 2023 01:48:03 -0700 Subject: [PATCH 5/6] Updated the dockerfile for best practises --- app_python/Dockerfile | 24 +++++------------------- app_python/main.py | 2 +- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/app_python/Dockerfile b/app_python/Dockerfile index d7d3ef024..d2c52d363 100644 --- a/app_python/Dockerfile +++ b/app_python/Dockerfile @@ -1,23 +1,9 @@ -# Build stage -FROM python:3.9-alpine AS builder - -WORKDIR /app - +FROM python:3-alpine +RUN addgroup -S lowuser && adduser -S lowuser -G lowuser +WORKDIR /main COPY requirements.txt . - RUN pip install --no-cache-dir -r requirements.txt - COPY . . - -# Runtime stage -FROM python:3.9-alpine - -WORKDIR /app - -COPY --from=builder /app . - -RUN pip install flask - -EXPOSE 8081 - +RUN chown -R lowuser:lowuser /main +USER lowuser CMD ["python3", "main.py"] \ No newline at end of file diff --git a/app_python/main.py b/app_python/main.py index 00c8b1f82..1c43ff072 100644 --- a/app_python/main.py +++ b/app_python/main.py @@ -14,4 +14,4 @@ def home(): return get_current_time() if __name__ == '__main__': - app.run(host="0.0.0.0", debug=True) \ No newline at end of file + app.run(host='0.0.0.0', port=8081) \ No newline at end of file From e7599f41337c3410409fdcba6c362d483a98b139 Mon Sep 17 00:00:00 2001 From: haraphat01 Date: Tue, 18 Jul 2023 16:29:43 -0700 Subject: [PATCH 6/6] updated the dockerfile for the corrections --- app_python/.dockerignore | 1 + app_python/Dockerfile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 app_python/.dockerignore diff --git a/app_python/.dockerignore b/app_python/.dockerignore new file mode 100644 index 000000000..1d1fe94df --- /dev/null +++ b/app_python/.dockerignore @@ -0,0 +1 @@ +Dockerfile \ No newline at end of file diff --git a/app_python/Dockerfile b/app_python/Dockerfile index d2c52d363..b9c98775a 100644 --- a/app_python/Dockerfile +++ b/app_python/Dockerfile @@ -3,7 +3,7 @@ RUN addgroup -S lowuser && adduser -S lowuser -G lowuser WORKDIR /main COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -COPY . . +COPY main.py . RUN chown -R lowuser:lowuser /main USER lowuser CMD ["python3", "main.py"] \ No newline at end of file