From 19ecfc7155d99a836ce094c915f4e83b9e89867e Mon Sep 17 00:00:00 2001 From: Iliyan Vutoff Date: Sun, 15 Oct 2023 15:00:31 +0300 Subject: [PATCH 1/6] Simple Python Flask app --- .python-version | 1 + app/app.py | 12 ++++++++++++ app/app_test.py | 17 +++++++++++++++++ requirements.txt | 8 ++++++++ 4 files changed, 38 insertions(+) create mode 100644 .python-version create mode 100644 app/app.py create mode 100644 app/app_test.py create mode 100644 requirements.txt diff --git a/.python-version b/.python-version new file mode 100644 index 000000000..9919bf8c9 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.10.13 diff --git a/app/app.py b/app/app.py new file mode 100644 index 000000000..5e4513f07 --- /dev/null +++ b/app/app.py @@ -0,0 +1,12 @@ +from flask import Flask + +app = Flask(__name__) + + +@app.route("/") +def hello_world(): + return "Hello, World!" + + +if __name__ == "__main__": + app.run() diff --git a/app/app_test.py b/app/app_test.py new file mode 100644 index 000000000..a1b1bacb2 --- /dev/null +++ b/app/app_test.py @@ -0,0 +1,17 @@ +import unittest + +from app import app + + +class TestApp(unittest.TestCase): + def setUp(self): + self.client = app.test_client() + + def test_hello_world(self): + response = self.client.get("/") + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data, b"Hello, World!") + + +if __name__ == "__main__": + unittest.main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..75cb9a31a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +blinker==1.6.3 ; python_version >= "3.10" and python_version < "4.0" +click==8.1.7 ; python_version >= "3.10" and python_version < "4.0" +colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and platform_system == "Windows" +flask==3.0.0 ; python_version >= "3.10" and python_version < "4.0" +itsdangerous==2.1.2 ; python_version >= "3.10" and python_version < "4.0" +jinja2==3.1.2 ; python_version >= "3.10" and python_version < "4.0" +markupsafe==2.1.3 ; python_version >= "3.10" and python_version < "4.0" +werkzeug==3.0.0 ; python_version >= "3.10" and python_version < "4.0" From 6549d8a1241f944eb0243bb0025efaf3fd486d95 Mon Sep 17 00:00:00 2001 From: Iliyan Vutoff Date: Wed, 25 Oct 2023 17:59:51 +0300 Subject: [PATCH 2/6] Make listen port configurable via ENV variable Use env var PORT to specify listen port. --- app/app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/app.py b/app/app.py index 5e4513f07..3a06f8f5e 100644 --- a/app/app.py +++ b/app/app.py @@ -1,3 +1,5 @@ +import os + from flask import Flask app = Flask(__name__) @@ -9,4 +11,4 @@ def hello_world(): if __name__ == "__main__": - app.run() + app.run(port=os.environ.get("PORT", 5000)) From ebf35498e0fc5e6250de22da35ccf8b5f39f1d9e Mon Sep 17 00:00:00 2001 From: Lina Date: Wed, 25 Oct 2023 19:07:59 +0200 Subject: [PATCH 3/6] First Dockerfile with python and pip --- Dockerfile | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..ac4aa528e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM ubuntu:22.10 + +ARG PYTHON_VERSION + +RUN sudo apt-get update \ + sudo apt-get upgrade \ + sudo apt get install python -y python=3.10.13 + +ENTRYPOINT [ "pip", "--version" ] From 1926bd2c0505c8231dc6d014a2ef7c5ef9765439 Mon Sep 17 00:00:00 2001 From: Lina Date: Sun, 29 Oct 2023 22:36:19 +0100 Subject: [PATCH 4/6] Dockerfile updated to install Python and pip --- Dockerfile | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac4aa528e..beb88597c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,22 @@ -FROM ubuntu:22.10 +FROM ubuntu:22.04 + +RUN apt-get update \ + && apt-get upgrade -y \ + && apt-get install software-properties-common -y \ + && add-apt-repository ppa:deadsnakes/ppa -y \ + && apt-get update -ARG PYTHON_VERSION +RUN apt-get install python3.10 -y \ + && apt-get install python3-pip -y -RUN sudo apt-get update \ - sudo apt-get upgrade \ - sudo apt get install python -y python=3.10.13 +WORKDIR /app -ENTRYPOINT [ "pip", "--version" ] +COPY requirements.txt /app + +RUN pip install -r requirements.txt + +COPY app /app + +EXPOSE 5000 + +ENTRYPOINT [ "python3", "app.py" ] From 48eae01dd5413fc92e9d9f29ba930408763ce618 Mon Sep 17 00:00:00 2001 From: Lina Date: Sun, 29 Oct 2023 22:38:21 +0100 Subject: [PATCH 5/6] Fixed Flask app --- app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.py b/app/app.py index 3a06f8f5e..2129eb1bc 100644 --- a/app/app.py +++ b/app/app.py @@ -11,4 +11,4 @@ def hello_world(): if __name__ == "__main__": - app.run(port=os.environ.get("PORT", 5000)) + app.run(host="0.0.0.0") From c6d9fbf68d1a22ce3d79aad63e42df0dfd6c3692 Mon Sep 17 00:00:00 2001 From: Lina Date: Sun, 29 Oct 2023 22:46:14 +0100 Subject: [PATCH 6/6] Updated to multi stage build --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index beb88597c..86868241c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:22.04 +FROM ubuntu:22.04 as builder RUN apt-get update \ && apt-get upgrade -y \ @@ -9,6 +9,8 @@ RUN apt-get update \ RUN apt-get install python3.10 -y \ && apt-get install python3-pip -y +FROM builder + WORKDIR /app COPY requirements.txt /app