Soutrik commited on
Commit
7c37fc5
1 Parent(s): 0f27535

added: docker compose

Browse files
Files changed (5) hide show
  1. Dockerfile +42 -29
  2. docker-compose.yaml +54 -66
  3. main.py +2 -27
  4. src/train.py +1 -5
  5. src/utils/logging_utils.py +1 -1
Dockerfile CHANGED
@@ -1,35 +1,48 @@
1
- # Use an official Python base image
2
- FROM python:3.10.15-slim
3
-
4
- # Set environment variables for Poetry
5
- ENV POETRY_VERSION=1.6.1 \
6
- POETRY_HOME="/opt/poetry" \
7
- POETRY_VIRTUALENVS_IN_PROJECT=true \
8
- PYTHONUNBUFFERED=1
9
-
10
- # Install Poetry
11
- RUN apt-get update && apt-get install -y curl \
12
- && curl -sSL https://install.python-poetry.org | python3 - \
13
- && ln -s ${POETRY_HOME}/bin/poetry /usr/local/bin/poetry \
14
- && apt-get clean \
15
- && rm -rf /var/lib/apt/lists/*
16
-
17
- # Set working directory
18
  WORKDIR /app
19
 
20
- # Copy Poetry files for dependency installation
21
- COPY pyproject.toml poetry.lock ./
 
 
 
 
 
22
 
23
- # Install dependencies with Poetry (without development dependencies)
24
- RUN poetry install --no-root --only main
25
 
26
- # Copy the application source code
27
- COPY src/ src/
28
- COPY app/ app/
29
- COPY main.py .
 
 
 
 
 
 
 
 
 
 
30
 
31
- # Expose the port for FastAPI
32
- EXPOSE 8000
33
 
34
- # Command to run the FastAPI application with uvicorn
35
- CMD ["poetry", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
1
+ # Stage 1: Build environment with Poetry and dependencies
2
+ FROM python:3.10.15-slim as builder
3
+
4
+ LABEL maintainer="Soutrik soutrik1991@gmail.com" \
5
+ description="Docker image for running a Python app with dependencies managed by Poetry."
6
+
7
+ # Install Poetry and necessary system dependencies
8
+ RUN apt-get update && apt-get install -y --no-install-recommends curl && \
9
+ curl -sSL https://install.python-poetry.org | python3 - && \
10
+ apt-get clean && rm -rf /var/lib/apt/lists/*
11
+
12
+ # Add Poetry to the PATH explicitly
13
+ ENV PATH="/root/.local/bin:$PATH"
14
+
15
+ # Set the working directory to /app
 
 
16
  WORKDIR /app
17
 
18
+ # Copy pyproject.toml and poetry.lock to install dependencies
19
+ COPY pyproject.toml poetry.lock /app/
20
+
21
+ # Configure Poetry environment
22
+ ENV POETRY_NO_INTERACTION=1 \
23
+ POETRY_VIRTUALENVS_IN_PROJECT=1 \
24
+ POETRY_CACHE_DIR=/tmp/poetry_cache
25
 
26
+ # Install dependencies without installing the package itself
27
+ RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main --no-root
28
 
29
+ # Stage 2: Runtime environment
30
+ FROM python:3.10.15-slim as runner
31
+
32
+ # Copy application source code and necessary files
33
+ COPY src /app/src
34
+ COPY configs /app/configs
35
+ COPY .project-root /app/.project-root
36
+ COPY main.py /app/main.py
37
+
38
+ # Copy virtual environment from the builder stage
39
+ COPY --from=builder /app/.venv /app/.venv
40
+
41
+ # Set the working directory to /app
42
+ WORKDIR /app
43
 
44
+ # Set the environment path to use the virtual environment
45
+ ENV PATH="/app/.venv/bin:$PATH"
46
 
47
+ # Default command
48
+ CMD ["python", "-m", "main"]
docker-compose.yaml CHANGED
@@ -1,81 +1,69 @@
1
  version: '3.8'
2
 
3
  services:
4
- postgres:
5
- image: postgres:16-alpine
6
- environment:
7
- POSTGRES_DB: ${POSTGRES_DB}
8
- POSTGRES_USER: ${POSTGRES_USER}
9
- POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
10
- DOCKER_ENV: 1
11
- ports:
12
- - "5432:5432"
13
  volumes:
14
- - postgres_data:/var/lib/postgresql/data
15
- networks:
16
- - my_network
17
- healthcheck:
18
- test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
19
- interval: 30s
20
- timeout: 10s
21
- retries: 5
22
-
23
- redis:
24
- image: redis/redis-stack:latest
25
- ports:
26
- - "6379:6379"
27
- - "8001:8001"
28
  environment:
29
- DOCKER_ENV: 1
30
- REDIS_PORT: ${REDIS_PORT}
31
- REDIS_HOST: ${REDIS_HOST}
32
  networks:
33
- - my_network
34
- healthcheck:
35
- test: ["CMD", "redis-cli", "ping"]
36
- interval: 30s
37
- timeout: 5s
38
- retries: 5
39
 
40
- flower:
41
- image: mher/flower:0.9.7
42
- environment:
43
- DOCKER_ENV: 1
44
- command: ["flower", "--broker=${BROKER_URL}", "--port=5555"]
45
- ports:
46
- - 5557:5555
47
- depends_on:
48
- - redis
49
- networks:
50
- - my_network
51
-
52
- app:
53
  build:
54
  context: .
55
- dockerfile: Dockerfile # Assumes the Dockerfile is in the root directory
 
 
 
 
 
 
56
  environment:
57
- - DATABASE_URL=${DATABASE_URL}
58
- - REDIS_URL=${REDIS_URL}
59
- - BROKER_URL=${BROKER_URL}
60
- - FLOWER_BASIC_AUTH=${FLOWER_BASIC_AUTH}
61
- - POSTGRES_DB=${POSTGRES_DB}
62
- - POSTGRES_USER=${POSTGRES_USER}
63
- - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
64
- - DOCKER_ENV=1
65
- depends_on:
66
- postgres:
67
- condition: service_healthy
68
- redis:
69
- condition: service_healthy
70
  networks:
71
- - my_network
72
- ports:
73
- - "8000:8000"
74
- command: ["poetry", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  volumes:
77
- postgres_data:
 
 
 
78
 
79
  networks:
80
- my_network:
81
- driver: bridge
 
1
  version: '3.8'
2
 
3
  services:
4
+ train:
5
+ build:
6
+ context: .
7
+ command: |
8
+ python -m src.train experiment=catdog_experiment ++task_name=train ++train=True ++test=False && \
9
+ touch /app/checkpoints/train_done.flag
 
 
 
10
  volumes:
11
+ - ./data:/app/data
12
+ - ./checkpoints:/app/checkpoints
13
+ - ./artifacts:/app/artifacts
14
+ - ./logs:/app/logs
 
 
 
 
 
 
 
 
 
 
15
  environment:
16
+ - PYTHONUNBUFFERED=1
17
+ - PYTHONPATH=/app
18
+ shm_size: '4g'
19
  networks:
20
+ - default
21
+ env_file:
22
+ - .env
 
 
 
23
 
24
+ eval:
 
 
 
 
 
 
 
 
 
 
 
 
25
  build:
26
  context: .
27
+ command: |
28
+ sh -c 'while [ ! -f ./checkpoints/train_done.flag ]; do sleep 10; done && python -m src.train experiment=catdog_experiment ++task_name=eval ++train=False ++test=True'
29
+ volumes:
30
+ - ./data:/app/data
31
+ - ./checkpoints:/app/checkpoints
32
+ - ./artifacts:/app/artifacts
33
+ - ./logs:/app/logs
34
  environment:
35
+ - PYTHONUNBUFFERED=1
36
+ - PYTHONPATH=/app
37
+ shm_size: '4g'
 
 
 
 
 
 
 
 
 
 
38
  networks:
39
+ - default
40
+ env_file:
41
+ - .env
42
+
43
+ # inference:
44
+ # build:
45
+ # context: .
46
+ # command: |
47
+ # sh -c 'while [ ! -f ./checkpoints/train_done.flag ]; do sleep 10; done && python -m src.infer experiment=catdog_experiment'
48
+ # volumes:
49
+ # - ./data:/app/data
50
+ # - ./checkpoints:/app/checkpoints
51
+ # - ./artifacts:/app/artifacts
52
+ # - ./logs:/app/logs
53
+ # environment:
54
+ # - PYTHONUNBUFFERED=1
55
+ # - PYTHONPATH=/app
56
+ # shm_size: '4g'
57
+ # networks:
58
+ # - default
59
+ # env_file:
60
+ # - .env
61
 
62
  volumes:
63
+ data:
64
+ checkpoints:
65
+ artifacts:
66
+ logs:
67
 
68
  networks:
69
+ default:
 
main.py CHANGED
@@ -1,28 +1,3 @@
1
- from fastapi import FastAPI
2
- from app.api import chat
3
- from app.db.database import Base, engine
4
- from app.core.config import settings
5
- from aiocache import caches
6
 
7
- app = FastAPI()
8
-
9
-
10
- # Create tables on startup
11
- @app.on_event("startup")
12
- async def startup():
13
- async with engine.begin() as conn:
14
- await conn.run_sync(Base.metadata.create_all)
15
-
16
- # Configure aiocache with in-memory backend
17
- caches.set_config(
18
- {
19
- "default": {
20
- "cache": settings.cache_backend,
21
- "ttl": 300, # Default Time-To-Live for cache entries (in seconds)
22
- }
23
- }
24
- )
25
-
26
-
27
- # Include the chat route
28
- app.include_router(chat.router)
 
1
+ from loguru import logger
 
 
 
 
2
 
3
+ logger.info("Hello, World!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/train.py CHANGED
@@ -23,12 +23,8 @@ import rootutils
23
  load_dotenv(find_dotenv(".env"))
24
 
25
  # Setup root directory
26
- try:
27
- root = rootutils.setup_root(__file__, indicator=".project-root")
28
- except Exception as e:
29
- root = os.getcwd()
30
 
31
- os.environ.setdefault("PROJECT_ROOT", str(root))
32
 
33
 
34
  def initialize_callbacks(cfg: DictConfig) -> List[L.Callback]:
 
23
  load_dotenv(find_dotenv(".env"))
24
 
25
  # Setup root directory
 
 
 
 
26
 
27
+ root = rootutils.setup_root(__file__, indicator=".project-root")
28
 
29
 
30
  def initialize_callbacks(cfg: DictConfig) -> List[L.Callback]:
src/utils/logging_utils.py CHANGED
@@ -15,7 +15,7 @@ def setup_logger(log_file):
15
  sys.stderr,
16
  format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
17
  )
18
- logger.add(log_file, rotation="1MB")
19
 
20
 
21
  def task_wrapper(func):
 
15
  sys.stderr,
16
  format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
17
  )
18
+ logger.add(log_file, rotation="1MB", retention="7 days", encoding="utf-8")
19
 
20
 
21
  def task_wrapper(func):