From b4eab64efda4cb9ccc27d7c661d156cb83adacd5 Mon Sep 17 00:00:00 2001 From: "boogie.bogiegie" Date: Tue, 23 Apr 2024 01:51:52 +0900 Subject: [PATCH 1/2] :sparkles: Add Dockerfile .dockerignore and moved all files to the root directory for dockerizing --- .dockerignore | 4 ++++ code_red_crawling/.gitignore => .gitignore | 0 Dockerfile | 9 +++++++++ code_red_crawling/app.py => app.py | 0 .../code_red_study.csv => code_red_study.csv | 0 .../docker-compose.yml => docker-compose.yml | 0 code_red_crawling/model.pkl => model.pkl | Bin code_red_crawling/news_study.py => news_study.py | 0 .../requirements.txt => requirements.txt | Bin code_red_crawling/stop_words.txt => stop_words.txt | 0 10 files changed, 13 insertions(+) create mode 100644 .dockerignore rename code_red_crawling/.gitignore => .gitignore (100%) create mode 100644 Dockerfile rename code_red_crawling/app.py => app.py (100%) rename code_red_crawling/code_red_study.csv => code_red_study.csv (100%) rename code_red_crawling/docker-compose.yml => docker-compose.yml (100%) rename code_red_crawling/model.pkl => model.pkl (100%) rename code_red_crawling/news_study.py => news_study.py (100%) rename code_red_crawling/requirements.txt => requirements.txt (100%) rename code_red_crawling/stop_words.txt => stop_words.txt (100%) diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..67edf2f --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.gitignore +.dockerignore +Dockerfile \ No newline at end of file diff --git a/code_red_crawling/.gitignore b/.gitignore similarity index 100% rename from code_red_crawling/.gitignore rename to .gitignore diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..88012d3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.9-buster + +COPY . /app + +RUN pip3 install flask + +WORKDIR /app + +CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"] \ No newline at end of file diff --git a/code_red_crawling/app.py b/app.py similarity index 100% rename from code_red_crawling/app.py rename to app.py diff --git a/code_red_crawling/code_red_study.csv b/code_red_study.csv similarity index 100% rename from code_red_crawling/code_red_study.csv rename to code_red_study.csv diff --git a/code_red_crawling/docker-compose.yml b/docker-compose.yml similarity index 100% rename from code_red_crawling/docker-compose.yml rename to docker-compose.yml diff --git a/code_red_crawling/model.pkl b/model.pkl similarity index 100% rename from code_red_crawling/model.pkl rename to model.pkl diff --git a/code_red_crawling/news_study.py b/news_study.py similarity index 100% rename from code_red_crawling/news_study.py rename to news_study.py diff --git a/code_red_crawling/requirements.txt b/requirements.txt similarity index 100% rename from code_red_crawling/requirements.txt rename to requirements.txt diff --git a/code_red_crawling/stop_words.txt b/stop_words.txt similarity index 100% rename from code_red_crawling/stop_words.txt rename to stop_words.txt From e7fdca30fa686a42e74ba53e4f42a867b99d6e3f Mon Sep 17 00:00:00 2001 From: "boogie.bogiegie" Date: Wed, 24 Apr 2024 01:34:22 +0900 Subject: [PATCH 2/2] :recycle: Refactor dockerizing --- Dockerfile | 18 +++++++++++++----- app.py | 15 ++++++++++++--- docker-compose.yml | 18 +++++++++++++++--- requirements.txt | Bin 6370 -> 3168 bytes 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 88012d3..bc16bcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,17 @@ -FROM python:3.9-buster +# 파이썬 베이스 이미지 사용 +FROM python:3.10.14-slim -COPY . /app +# 작업 디렉토리 설정 +WORKDIR /app -RUN pip3 install flask +# 필요한 파이썬 패키지 설치 +# requirements.txt 파일에 Flask와 기타 의존성이 명시되어 있다고 가정 +COPY requirements.txt . +RUN pip install -r requirements.txt -WORKDIR /app +# 현재 디렉토리의 모든 파일을 컨테이너의 작업 디렉토리로 복사 +COPY . /app -CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"] \ No newline at end of file +# Flask 서버 실행 +# "--host=0.0.0.0" 옵션은 Docker 컨테이너 외부에서 Flask 앱에 접근할 수 있도록 함 +CMD ["flask", "run", "--host=0.0.0.0"] diff --git a/app.py b/app.py index 3eed088..caf945b 100644 --- a/app.py +++ b/app.py @@ -5,7 +5,7 @@ from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By -import time +from time import sleep import jellyfish import os import psycopg2 @@ -22,14 +22,23 @@ scheduler.init_app(app) def create_connection(): - conn = psycopg2.connect( + retry_count = 5 + for i in range(retry_count): + try: + conn = psycopg2.connect( host=os.getenv('DB_HOST'), dbname=os.getenv('DB_NAME'), user=os.getenv('DB_USERNAME'), password=os.getenv('DB_PASSWORD'), port=os.getenv('DB_PORT') ) - return conn + print("Database connection successful") + return conn + except psycopg2.OperationalError as e: + print(f"Connection failed: {e}") + print("Trying again in 5 seconds...") + sleep(5) + raise Exception("Failed to connect to database after several attempts") conn = create_connection() diff --git a/docker-compose.yml b/docker-compose.yml index 6efbe39..3d8ca1a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,12 +4,10 @@ services: codered_postgresql: image: postgis/postgis restart: always - container_name: postgres + container_name: codered_postgresql ports: - "5432:5432" - # 환경 변수 설정 - environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres @@ -18,3 +16,17 @@ services: - ./data/postgres/:/var/lib/postgresql/data networks: - compose-networks + codered_crawling: + image: codered_crawling + restart: always + container_name: codered_crawling + ports: + - "5000:5000" + depends_on: + - codered_postgresql + networks: + - compose-networks + +networks: + compose-networks: + driver: bridge diff --git a/requirements.txt b/requirements.txt index 50e64bc0f0e55a94931802306a1613ce491dcc91..488b0bf27e0356d3c8b42f4362adfe0b53728528 100644 GIT binary patch delta 31 mcmaE4_&{QV13RxSLlQ$KLmopSLlHyeW_xy3#?1{J@0bCV_z4XF delta 81 zcmaDL@yKw41N&w_b`?h9a)wNXJO*P1BL-U_G-fbhFx#BT@tv84mw}66Ba