From a3d73e0bc768a6a2f25bd332231ec5d7e525f54a Mon Sep 17 00:00:00 2001 From: Totto16 Date: Fri, 19 Jan 2024 00:19:40 +0100 Subject: [PATCH 1/5] add simple Dockerfile --- Dockerfile | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..13e432b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.12 +WORKDIR /app + + +# install python dependencies + +COPY requirements.txt requirements.txt + +RUN pip3 install --no-cache-dir -r requirements.txt + +COPY lobby/ lobby/ + +## add finalization arguments + +HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 CMD curl -f -X GET http://localhost:1717/health + +EXPOSE 3001 + +CMD [ "python3", "-m" , "flask", "--app=lobby.main", "run", "--host=0.0.0.0", "--port=1717"] From 81b0f484f5f4c5653fc4939f9e44b2608a129c79 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Fri, 19 Jan 2024 00:19:56 +0100 Subject: [PATCH 2/5] add simple docker-compose file --- docker-compose.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0531fd4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3.8" +name: "obpf-lobby" +services: + flask: + container_name: obpf-lobby + image: obpf-lobby:latest + ports: + - "1717:1717" + volumes: + - "./files/:/app/files/" From 1ea6aef3221b1b52b34f1dbaa8edc59174138ac5 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Fri, 19 Jan 2024 00:20:20 +0100 Subject: [PATCH 3/5] add information on how to use the docker-compose file --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 8d90286..27ba3b1 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,30 @@ # The Lob(b)y Server Everybody Asked For! More to come here... + + +## Deploy with docker compose + +You need a running docker service and docker compose installed + + +Then build the image: + +```bash +docker build -t obpf-lobby . +``` + +Now you can run the docker compose file. + +```bash +docker compose up -d +``` +Note: the port is 1717 to proxy it through something like nginx. + + +Other common docker compose comands include: +```bash +docker compose down # stop all containers +docker compose ps # get details about running containers +docker compose logs -f # see the logs of all containers (-f watches them) +``` From 86cd0d5f8fa274bedac16b654a302d39ceaa5570 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Fri, 19 Jan 2024 00:22:33 +0100 Subject: [PATCH 4/5] - add fixes for flask warning: - add simple health api for docker compose --- lobby/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lobby/main.py b/lobby/main.py index f28c7df..1693a02 100644 --- a/lobby/main.py +++ b/lobby/main.py @@ -81,6 +81,11 @@ def from_id(cls, id_: str) -> Self: return cls(id_, user.username) +@app.route("/health", methods=["GET"]) +def health_check(): + return create_ok_response({}) + + @app.route("/lobbies", methods=["GET"]) def lobby_list(): @dataclass @@ -198,4 +203,6 @@ class RegisterRequest(JsonSchemaMixin): return create_response(HTTPStatus.CREATED, {"id": new_user.id}) -app.run(debug=True) +# this runs, when you launch this file manually, otherwise flask imports this file and ignores this +if __name__ == "__main__": + app.run(debug=True) From 426d988a2c5f2557f14838c8a9e9cf2829172eda Mon Sep 17 00:00:00 2001 From: Totto16 Date: Fri, 19 Jan 2024 00:30:45 +0100 Subject: [PATCH 5/5] add production server, as suggested by flask --- Dockerfile | 4 ++-- lobby/main.py | 8 +++++++- requirements.txt | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 13e432b..1c9fb40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,6 +14,6 @@ COPY lobby/ lobby/ HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 CMD curl -f -X GET http://localhost:1717/health -EXPOSE 3001 +EXPOSE 1717 -CMD [ "python3", "-m" , "flask", "--app=lobby.main", "run", "--host=0.0.0.0", "--port=1717"] +CMD [ "python3", "lobby/main.py", "prod"] diff --git a/lobby/main.py b/lobby/main.py index 1693a02..8df5b04 100644 --- a/lobby/main.py +++ b/lobby/main.py @@ -1,4 +1,5 @@ import os.path +import sys import time import typing from dataclasses import dataclass @@ -205,4 +206,9 @@ class RegisterRequest(JsonSchemaMixin): # this runs, when you launch this file manually, otherwise flask imports this file and ignores this if __name__ == "__main__": - app.run(debug=True) + print(sys.argv) + if len(sys.argv) >= 2 and sys.argv[1] == "prod": + from waitress import serve + serve(app, host="0.0.0.0", port=1717) + else: + app.run(debug=True) diff --git a/requirements.txt b/requirements.txt index fc526d2..c31d378 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,5 @@ setuptools==69.0.3 six==1.16.0 SQLAlchemy==2.0.25 typing_extensions==4.9.0 +waitress==2.1.2 Werkzeug==3.0.1