feat: add docker integration

This commit is contained in:
patilk 2024-11-12 05:17:00 +01:00
parent b25f04d218
commit c5b61893ac
Signed by: s500042
GPG Key ID: 1921AD722E7392EE
5 changed files with 132 additions and 0 deletions

26
.dockerignore Normal file
View File

@ -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

26
app/Dockerfile Normal file
View File

@ -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"]

26
docker-compose.yaml Normal file
View File

@ -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:

47
start.py Normal file
View File

@ -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()

7
start.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
docker compose up -d
docker compose wait webscraper > /dev/null
docker compose down