From 48585db67a2c8a3574f6bcbb476e70d06449b4c6 Mon Sep 17 00:00:00 2001 From: Mohamad Shaheen Date: Tue, 20 Aug 2024 15:00:53 +0300 Subject: [PATCH] Added running on docker support --- Dockerfile | 7 +++++++ README.md | 33 +++++++++++++++++++++++++++++++++ docker-compose.yml | 8 ++++++++ server.py | 2 +- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d214d27 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python +WORKDIR /app +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +EXPOSE 8000 8001 8002 +CMD ["python", "server.py"] diff --git a/README.md b/README.md index d43cbac..cfc61ef 100644 --- a/README.md +++ b/README.md @@ -202,3 +202,36 @@ An example of how a question and its details is stored in the database: ``` As shown in the above example, one question can be answered by many users and each user is represented by his ID + +# Running on Docker + +- In order to run the project on docker for the **first time**, ***or*** you have updated the server, run the following commands in terminal: + +```shell +cd path/to/your/project +docker-compose build +docker-compose up +``` + +- If you have already run the above commands before, and you just want to run the server on docker run the following command: + +```shell +docker-compose up +``` + +- To stop the server run the following command: + +```shell +docker-compose down +``` + +- Since the default environment is the `dev` the docker will run using `.env.dev` file on port `8002`. +- The docker file also exposed to ports `8000` and `8001`. So in order to run in `prod` environment you must change `globals.env_status` to `prod` which will run the docker on port `8001`. + +- To push the docker image to the docker hub you have to build it first and then run the following commands in the terminal: + +```shell +docker tag /: +docker login +docker push /: +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..521593a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,8 @@ +version: "3.8" +services: + server: + build: . + ports: + - "8000:8000" + - "8001:8001" + - "8002:8002" diff --git a/server.py b/server.py index a94094c..1222e86 100644 --- a/server.py +++ b/server.py @@ -29,7 +29,7 @@ def add_routes(): # start server add_routes() port = 8002 if globals.env_status == "dev" else 8001 - uvicorn.run(app, host="127.0.0.1", port=port) + uvicorn.run(app, host="0.0.0.0", port=port) except Exception as e: print(e)