dmartincy commited on
Commit
c6d4e5d
·
1 Parent(s): db9f671

First more or less working version

Browse files
Files changed (3) hide show
  1. Dockerfile +77 -60
  2. nginx.conf +2 -15
  3. start-services.sh +59 -18
Dockerfile CHANGED
@@ -1,13 +1,14 @@
1
- # Start with PostgreSQL image with pgvector
2
- FROM pgvector/pgvector:pg16
3
 
4
  # Set environment variables
5
- ENV POSTGRES_USER=db-user \
 
 
 
6
  POSTGRES_PASSWORD=password \
7
  POSTGRES_DB=ai_assistant \
8
  POSTGRES_INITDB_ARGS=--data-checksums \
9
- PGDATA=/var/lib/postgresql/data/pgdata \
10
- OPENAI_API_KEY="dumy-key" \
11
  PGUSER=db-user \
12
  PGPASSWORD=password \
13
  PGDATABASE=ai_assistant \
@@ -20,9 +21,25 @@ ENV POSTGRES_USER=db-user \
20
  DASHBOARD_PASSWORD=secret \
21
  SECRET_KEY_BASE=secret-key-base
22
 
23
- # Install required packages including nginx
24
- RUN apt-get update && \
25
- apt-get install -y nodejs npm python3-venv python3-pip wget curl unzip nginx-light procps && \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  mkdir -p /tmp/llamafile && \
27
  cd /tmp/llamafile && \
28
  wget --no-check-certificate https://github.com/Mozilla-Ocho/llamafile/releases/download/0.8.17/llamafile-0.8.17.zip && \
@@ -30,65 +47,65 @@ RUN apt-get update && \
30
  cp llamafile-0.8.17/bin/llamafiler /usr/local/bin/ && \
31
  chmod +x /usr/local/bin/llamafiler && \
32
  rm -rf /tmp/llamafile && \
33
- # Create all required nginx directories and files with correct ownership and permissions
34
- mkdir -p /var/run/nginx && \
35
- mkdir -p /var/lib/nginx/body && \
36
- mkdir -p /var/lib/nginx/proxy && \
37
- mkdir -p /var/lib/nginx/fastcgi && \
38
- mkdir -p /var/lib/nginx/uwsgi && \
39
- mkdir -p /var/lib/nginx/scgi && \
40
- mkdir -p /var/log/nginx && \
41
- touch /var/log/nginx/access.log && \
42
- touch /var/log/nginx/error.log && \
43
- chmod 755 /var/lib && \
44
- chmod 755 /var/log && \
45
- chmod 755 /var/run && \
46
- chown -R postgres:postgres /var/run/nginx /var/log/nginx /var/lib/nginx && \
47
- chmod -R 755 /var/lib/nginx /var/log/nginx && \
48
- chmod 777 /var/run/nginx && \
49
- chmod 666 /var/log/nginx/access.log && \
50
- chmod 666 /var/log/nginx/error.log
51
 
52
- # Create and activate virtual environment
53
- RUN python3 -m venv /venv
54
- ENV PATH="/venv/bin:$PATH"
 
 
 
 
 
 
 
55
 
56
- # Install Flask in the virtual environment
57
- RUN /venv/bin/pip install flask
 
 
 
58
 
59
- # Copy the Flask app
60
- COPY app.py /app.py
61
- COPY service-config.yml /service-config.yml
 
 
 
62
 
63
- # Create a healthcheck script
64
- COPY healthcheck.sh /usr/local/bin/healthcheck.sh
65
- RUN chmod +x /usr/local/bin/healthcheck.sh
 
 
 
 
 
66
 
67
- # Pull and extract the AI assistant image
68
- COPY --from=pspdfkit/ai-assistant:nightly / /ai-assistant
69
- COPY service-config.yml /ai-assistant/base/config/service-config.yml
 
 
70
 
71
- # Copy and prepare start script
72
- COPY start-services.sh /start-services.sh
73
- RUN chmod +x /start-services.sh
74
-
75
- # Copy Nginx configuration
76
- COPY nginx.conf /etc/nginx/nginx.conf
77
-
78
- # Create base directory and download models during build
79
- RUN mkdir -p /base && \
80
- wget -q https://huggingface.co/bartowski/gemma-2-2b-it-GGUF/resolve/main/gemma-2-2b-it-Q4_K_M.gguf -O /base/gemma-2b.gguf && \
81
- wget -q https://huggingface.co/leliuga/all-MiniLM-L6-v2-GGUF/resolve/main/all-MiniLM-L6-v2.F16.gguf -O /base/embeddings.gguf && \
82
- chown -R postgres:postgres /base
83
-
84
- # Expose ports
85
  EXPOSE 5432 7860 4000 8080 8081 8082
86
 
87
- # Add a healthcheck
88
- HEALTHCHECK --interval=3s --timeout=3s --retries=10 \
89
- CMD ["healthcheck.sh"]
90
 
91
- USER postgres
 
 
 
 
 
 
92
 
93
- # Start PostgreSQL, Flask, and AI assistant
94
- CMD ["sh", "-c", "/venv/bin/flask run --host=0.0.0.0 --port=7860 & docker-entrypoint.sh postgres & /start-services.sh"]
 
 
 
1
+ FROM pspdfkit/ai-assistant:nightly
 
2
 
3
  # Set environment variables
4
+ ENV HOME=/home/node \
5
+ PNPM_HOME=/root/.local/share/pnpm \
6
+ PATH=/root/.local/share/pnpm/bin:$PATH \
7
+ POSTGRES_USER=db-user \
8
  POSTGRES_PASSWORD=password \
9
  POSTGRES_DB=ai_assistant \
10
  POSTGRES_INITDB_ARGS=--data-checksums \
11
+ PGDATA=/home/node/pgdata \
 
12
  PGUSER=db-user \
13
  PGPASSWORD=password \
14
  PGDATABASE=ai_assistant \
 
21
  DASHBOARD_PASSWORD=secret \
22
  SECRET_KEY_BASE=secret-key-base
23
 
24
+ # Install system packages (as root)
25
+ USER root
26
+ RUN apk add --no-cache \
27
+ postgresql16 \
28
+ postgresql16-contrib \
29
+ py3-pip \
30
+ python3 \
31
+ py3-virtualenv \
32
+ wget \
33
+ curl \
34
+ unzip \
35
+ nginx \
36
+ procps \
37
+ build-base \
38
+ postgresql16-dev \
39
+ git && \
40
+ mkdir -p /run/postgresql && \
41
+ chown -R node:node /run/postgresql && \
42
+ # Install llamafiler
43
  mkdir -p /tmp/llamafile && \
44
  cd /tmp/llamafile && \
45
  wget --no-check-certificate https://github.com/Mozilla-Ocho/llamafile/releases/download/0.8.17/llamafile-0.8.17.zip && \
 
47
  cp llamafile-0.8.17/bin/llamafiler /usr/local/bin/ && \
48
  chmod +x /usr/local/bin/llamafiler && \
49
  rm -rf /tmp/llamafile && \
50
+ mkdir -p /root/.local/share/pnpm && \
51
+ chmod -R 755 /root/.local/share/pnpm && \
52
+ chown -R node:node /root/.local/share/pnpm && \
53
+ # Ensure prisma is executable from PATH
54
+ ln -s /base/node_modules/.pnpm/[email protected]/node_modules/prisma/node_modules/.bin/prisma /usr/local/bin/prisma && \
55
+ chmod +x /usr/local/bin/prisma
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ # Install nginx and set up directories for non-root usage
58
+ RUN mkdir -p /var/cache/nginx \
59
+ /var/log/nginx \
60
+ /var/lib/nginx && \
61
+ touch /var/run/nginx.pid && \
62
+ chown -R node:node /var/cache/nginx \
63
+ /var/log/nginx \
64
+ /var/lib/nginx \
65
+ /var/run/nginx.pid \
66
+ /run/nginx
67
 
68
+ # Copy application files to /base BEFORE switching user
69
+ COPY --chown=node:node app.py service-config.yml /base/app/
70
+ COPY --chown=node:node nginx.conf /etc/nginx/nginx.conf
71
+ COPY --chown=node:node healthcheck.sh start-services.sh /base/app/
72
+ COPY --chown=node:node service-config.yml /service-config.yml
73
 
74
+ # Create app directories and set up Python
75
+ USER node
76
+ RUN mkdir -p $HOME/models && \
77
+ mkdir -p $HOME/pgdata && \
78
+ python3 -m venv $HOME/venv && \
79
+ $HOME/venv/bin/pip install flask
80
 
81
+ # pgvector installation (needs root)
82
+ USER root
83
+ RUN git clone https://github.com/pgvector/pgvector.git && \
84
+ cd pgvector && \
85
+ make && \
86
+ make install && \
87
+ cd .. && \
88
+ rm -rf pgvector
89
 
90
+ # Download models as node user
91
+ USER node
92
+ RUN wget -q https://huggingface.co/bartowski/gemma-2-2b-it-GGUF/resolve/main/gemma-2-2b-it-Q4_K_M.gguf -O $HOME/models/gemma-2b.gguf && \
93
+ wget -q https://huggingface.co/leliuga/all-MiniLM-L6-v2-GGUF/resolve/main/all-MiniLM-L6-v2.F16.gguf -O $HOME/models/embeddings.gguf && \
94
+ chmod +x /base/app/healthcheck.sh /base/app/start-services.sh
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  EXPOSE 5432 7860 4000 8080 8081 8082
97
 
98
+ USER root
 
 
99
 
100
+ # Fix permissions for the entire pnpm path
101
+ RUN mkdir -p /root/.local/share/pnpm && \
102
+ chmod 755 /root && \
103
+ chmod 755 /root/.local && \
104
+ chmod 755 /root/.local/share && \
105
+ chmod -R 755 /root/.local/share/pnpm && \
106
+ chown -R node:node /root/.local/share/pnpm
107
 
108
+ USER node
109
+ WORKDIR /base
110
+ ENTRYPOINT []
111
+ CMD ["sh", "-c", "/base/app/start-services.sh"]
nginx.conf CHANGED
@@ -1,30 +1,17 @@
1
- pid /var/run/nginx/nginx.pid;
2
-
3
  events {
4
  worker_connections 1024;
5
  }
6
 
7
  http {
8
- include /etc/nginx/mime.types;
9
- default_type application/octet-stream;
10
-
11
  server {
12
  listen 8080;
13
 
14
  location /v1/embeddings {
15
- proxy_pass http://127.0.0.1:8081;
16
- proxy_set_header Host $host;
17
- proxy_set_header X-Real-IP $remote_addr;
18
- proxy_read_timeout 300;
19
- proxy_connect_timeout 300;
20
  }
21
 
22
  location /v1 {
23
- proxy_pass http://127.0.0.1:8082;
24
- proxy_set_header Host $host;
25
- proxy_set_header X-Real-IP $remote_addr;
26
- proxy_read_timeout 300;
27
- proxy_connect_timeout 300;
28
  }
29
  }
30
  }
 
 
 
1
  events {
2
  worker_connections 1024;
3
  }
4
 
5
  http {
 
 
 
6
  server {
7
  listen 8080;
8
 
9
  location /v1/embeddings {
10
+ proxy_pass http://127.0.0.1:8081;
 
 
 
 
11
  }
12
 
13
  location /v1 {
14
+ proxy_pass http://127.0.0.1:8082;
 
 
 
 
15
  }
16
  }
17
  }
start-services.sh CHANGED
@@ -1,13 +1,10 @@
1
  #!/bin/sh
 
2
 
3
- # Wait for PostgreSQL to be ready
4
- until pg_isready -h localhost -U db-user
5
- do
6
- echo "Waiting for PostgreSQL to be ready..."
7
- sleep 1
8
- done
9
 
10
- # Start nginx with explicit configuration
11
  echo "Starting nginx..."
12
  /usr/sbin/nginx -c /etc/nginx/nginx.conf
13
  if [ $? -ne 0 ]; then
@@ -20,21 +17,45 @@ if ! ps aux | grep nginx | grep -v grep > /dev/null; then
20
  echo "Nginx failed to start"
21
  exit 1
22
  fi
23
-
24
  echo "Nginx started successfully"
25
 
26
- # Start llamafiler instances
27
- echo "Starting llamafiler services..."
 
 
 
 
 
 
28
 
29
- # Create TMPDIR if it doesn't exist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  mkdir -p /tmp/llamafiler
31
 
32
  echo "Starting chat model..."
33
- TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model /base/gemma-2b.gguf --listen 0.0.0.0:8082 &
34
  GEMMA_PID=$!
35
 
36
  echo "Starting embedding model..."
37
- TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model /base/embeddings.gguf --listen 0.0.0.0:8081 &
38
  EMBEDDINGS_PID=$!
39
 
40
  # Wait for the models to start loading
@@ -65,8 +86,28 @@ done
65
 
66
  echo 'Servers are ready!'
67
 
68
- # Start the AI assistant application from the correct directory
69
- cd /ai-assistant/base && \
70
- NODE_PATH=/ai-assistant/base/node_modules \
71
- PATH=/ai-assistant/base/node_modules/.bin:/ai-assistant/base/bin:$PATH \
72
- exec /ai-assistant/base/bin/entrypoint.sh node app/main.bundle.js
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  #!/bin/sh
2
+ set -e
3
 
4
+ # 1. Start Flask (already started in the CMD)
5
+ echo "Flask is running..."
 
 
 
 
6
 
7
+ # 2. Start Nginx
8
  echo "Starting nginx..."
9
  /usr/sbin/nginx -c /etc/nginx/nginx.conf
10
  if [ $? -ne 0 ]; then
 
17
  echo "Nginx failed to start"
18
  exit 1
19
  fi
 
20
  echo "Nginx started successfully"
21
 
22
+ # 3. Start PostgreSQL
23
+ echo "Starting PostgreSQL..."
24
+ if [ ! -f "$PGDATA/PG_VERSION" ]; then
25
+ mkdir -p $PGDATA
26
+ echo "Current user: $(whoami)"
27
+ initdb -D $PGDATA --username=postgres --pwfile=<(echo "password")
28
+ fi
29
+ pg_ctl -D $PGDATA start
30
 
31
+ # Create the db-user (but let Prisma handle the database)
32
+ echo "Creating database user..."
33
+ PGPASSWORD=password PGUSER=postgres createuser -s "db-user" || echo "User db-user already exists"
34
+ PGPASSWORD=password PGUSER=postgres psql -c 'ALTER USER "db-user" WITH PASSWORD '\''password'\'';' postgres
35
+
36
+ # Wait for PostgreSQL (using db-user)
37
+ echo "Waiting for PostgreSQL..."
38
+ until pg_isready -U db-user; do
39
+ echo "Waiting for PostgreSQL to be ready..."
40
+ sleep 2
41
+ if [ $SECONDS -gt 30 ]; then
42
+ echo "Timeout waiting for PostgreSQL"
43
+ exit 1
44
+ fi
45
+ done
46
+
47
+ echo "PostgreSQL is ready!"
48
+
49
+ # 4. Start Llamafiler (reusing existing code)
50
+ echo "Starting llamafiler services..."
51
  mkdir -p /tmp/llamafiler
52
 
53
  echo "Starting chat model..."
54
+ TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/gemma-2b.gguf --listen 0.0.0.0:8082 &
55
  GEMMA_PID=$!
56
 
57
  echo "Starting embedding model..."
58
+ TMPDIR=/tmp/llamafiler /usr/local/bin/llamafiler --model $HOME/models/embeddings.gguf --listen 0.0.0.0:8081 &
59
  EMBEDDINGS_PID=$!
60
 
61
  # Wait for the models to start loading
 
86
 
87
  echo 'Servers are ready!'
88
 
89
+ echo 'All services are ready!'
90
+
91
+ # Ensure we have the same environment as the base image
92
+ export PNPM_HOME=/root/.local/share/pnpm
93
+ export PATH=$PNPM_HOME/bin:$PATH
94
+ cd /base # Ensure we're in the correct directory
95
+
96
+ echo "=== After Environment Setup ==="
97
+ echo "New working directory: $(pwd)"
98
+ echo "Updated PATH: $PATH"
99
+ echo "Contents of prisma directory:"
100
+ ls -la prisma/
101
+ echo "NODE_PATH environment: $NODE_PATH"
102
+ echo "PNPM_HOME contents:"
103
+ ls -la $PNPM_HOME
104
+ echo "Global node_modules:"
105
+ ls -la $PNPM_HOME/global/5/node_modules/@prisma || echo "No global prisma found"
106
+ echo "Local prisma version:"
107
+ prisma -v
108
+ echo "======================="
109
+
110
+ # Now execute the entrypoint with the proper environment
111
+ echo "Executing: /base/bin/entrypoint.sh node /base/app/main.bundle.js"
112
+ set -x # Enable command tracing
113
+ exec /base/bin/entrypoint.sh node /base/app/main.bundle.js