From 5c433b1a66746d5633e9e9ff31a355c2807d5c12 Mon Sep 17 00:00:00 2001 From: Tushar <30565750+tushar5526@users.noreply.github.com> Date: Thu, 28 Dec 2023 00:51:46 +0530 Subject: [PATCH] feat: DOCKERIZED --- server/.dockerignore | 1 + server/.env.sample | 7 +++++++ server/Dockerfile | 15 +++++++++++++++ server/docker-compose.yml | 14 ++++++++++++++ server/requirements.txt | 3 ++- server/utils.py | 9 +++++---- 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 server/.dockerignore create mode 100644 server/.env.sample create mode 100644 server/Dockerfile diff --git a/server/.dockerignore b/server/.dockerignore new file mode 100644 index 0000000..e6905a2 --- /dev/null +++ b/server/.dockerignore @@ -0,0 +1 @@ +.env* \ No newline at end of file diff --git a/server/.env.sample b/server/.env.sample new file mode 100644 index 0000000..317b91d --- /dev/null +++ b/server/.env.sample @@ -0,0 +1,7 @@ +# vars mostly used in local development +BASE_DIR='/Users/tusharsamagra/projects/sarthi/server/deployments' +NGINX_PROXY_CONF_LOCATION='/Users/tusharsamagra/projects/sarthi/server/nginx-confs' +ENV=local +DEPLOYMENT_HOST='localhost' +# mac hack +DOCKER_MOUNT_DIR='/Users/tusharsamagra/projects/sarthi/server/deployments' \ No newline at end of file diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..237619d --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3.11 + +WORKDIR /app + +RUN apt update -y && apt install docker-compose -y + +COPY requirements.txt ./ + +RUN pip install -r requirements.txt + +COPY . . + +EXPOSE 5000 + +CMD [ "flask", "run", "--host=0.0.0.0", "--port=5000"] \ No newline at end of file diff --git a/server/docker-compose.yml b/server/docker-compose.yml index 38bc658..624ccf1 100644 --- a/server/docker-compose.yml +++ b/server/docker-compose.yml @@ -8,3 +8,17 @@ services: - "80:80" volumes: - ./nginx-confs:/etc/nginx/conf.d + + sarthi: + build: . + ports: + - 5000:5000 + volumes: + # hack to bypass file resource sharing error to make my development easy on mac + - ./deployments:${DOCKER_MOUNT_DIR:-/deployments} + - ./nginx-confs:/nginx-confs + - /var/run/docker.sock:/var/run/docker.sock + environment: + BASE_DIR: '/Users/tusharsamagra/projects/sarthi/server/deployments' + NGINX_PROXY_CONF_LOCATION: '/nginx-confs' + ENV: local diff --git a/server/requirements.txt b/server/requirements.txt index 352f903..6c4e1a2 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,4 +1,5 @@ pyyaml flask pyjwt -Flask - HTTPAuth +Flask-HTTPAuth +python-dotenv \ No newline at end of file diff --git a/server/utils.py b/server/utils.py index b917ab0..ef89db6 100644 --- a/server/utils.py +++ b/server/utils.py @@ -175,11 +175,12 @@ def find_free_port(self) -> str: while current_port <= self._end_port: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: try: - s.bind((self._host_name, current_port)) + s.connect((self._host_name, current_port)) + s.close() + current_port += 1 + except ConnectionRefusedError: self._port = current_port return current_port - except socket.error: - current_port += 1 raise RuntimeError(f"Could not find a free port in the specified range.") @@ -251,7 +252,7 @@ def _test_nginx_config(self): def reload_nginx(self): self._test_nginx_config() subprocess.run( - ["docker", "exec", "-it", "sarthi_nginx", "nginx", "-s", "reload"], + ["docker", "exec", "sarthi_nginx", "nginx", "-s", "reload"], check=True, ) logger.info("Nginx reloaded successfully.")