From c5b61893ac1d9b8e4bb26d8aa34e5829d9e0f7c7 Mon Sep 17 00:00:00 2001 From: paprykdev <58005447+paprykdev@users.noreply.github.com> Date: Tue, 12 Nov 2024 05:17:00 +0100 Subject: [PATCH] feat: add docker integration --- .dockerignore | 26 +++++++++++++++++++++++++ app/Dockerfile | 26 +++++++++++++++++++++++++ docker-compose.yaml | 26 +++++++++++++++++++++++++ start.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ start.sh | 7 +++++++ 5 files changed, 132 insertions(+) create mode 100644 .dockerignore create mode 100644 app/Dockerfile create mode 100644 docker-compose.yaml create mode 100644 start.py create mode 100755 start.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..1429109 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,26 @@ +# Ignore Python cache and compiled files +__pycache__/ +*.py[cod] +*.pyo + +# Ignore virtual environment directories +.venv/ +venv/ +env/ + +# Ignore logs +logs/ +*.log + +# Ignore environment variables +.env + +# Ignore Docker files +Dockerfile +docker-compose.yml + +# Ignore build directories +dist/ +build/ + +# Ignore any other files or directories you want to exclude \ No newline at end of file diff --git a/app/Dockerfile b/app/Dockerfile new file mode 100644 index 0000000..c6d92f0 --- /dev/null +++ b/app/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.9-slim + +WORKDIR /usr/src/app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +RUN apt-get update && apt-get install -y \ + wget \ + unzip \ + curl \ + docker \ + libx11-dev \ + libgdk-pixbuf2.0-0 \ + libcanberra-gtk-module \ + && wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip \ + && unzip chromedriver_linux64.zip \ + && mv chromedriver /usr/local/bin/ \ + && chmod +x /usr/local/bin/chromedriver + +# RUN chown -R python /usr/src/app +# USER python + +CMD ["python", "main.py"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..2a7fd63 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,26 @@ +services: + webscraper: + build: + context: ./app + container_name: webscraper + depends_on: + - redis + volumes: + - ./app:/usr/src/app + develop: + watch: + - path: ./app/requirements.txt + action: rebuild + - path: ./app + target: /usr/src/app + action: sync + redis: + image: "redis:alpine" + volumes: + - redis_data:/data + ports: + - "6379:6379" + +volumes: + redis_data: + app: diff --git a/start.py b/start.py new file mode 100644 index 0000000..9d25bd4 --- /dev/null +++ b/start.py @@ -0,0 +1,47 @@ +import subprocess +import time + + +def run_command(command): + result = subprocess.run( + command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE + ) + if result.returncode != 0: + print(f"Error running command: {command}") + print(result.stderr.decode()) + return result.stdout.decode() + + +def wait_for_webscraper(): + while True: + result = run_command("docker compose ps -q webscraper") + container_id = result.strip() + + if not container_id: + print("Webscraper container not found.") + break + + status = run_command( + f"docker inspect --format '{{.State.Status}}' {container_id}" + ) + + if status.strip() == "exited": + print("Webscraper has finished.") + break + + print("Waiting for webscraper to finish...") + time.sleep(3) + + +def main(): + print("Starting Docker Compose services...") + run_command("docker compose up -d") + + wait_for_webscraper() + + print("Stopping and removing Docker Compose services...") + run_command("docker compose down") + + +if __name__ == "__main__": + main() diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..1bcd812 --- /dev/null +++ b/start.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +docker compose up -d + +docker compose wait webscraper > /dev/null + +docker compose down \ No newline at end of file