Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improve] Improved Docker build #514

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Location of Ollama models at your HOST machine
HOST_OLLAMA_MODELS_DIR="/path/to/your/.ollama/models"
# UI port at the HOST system
DEVIKA_UI_PORT=3033
# Ollama daemon configuration variables
OLLAMA_ORIGINS="*"
OLLAMA_KEEP_ALIVE="10m"
OLLAMA_MODELS="/root/.ollama/models"
# Ollama service networking configuration
OLLAMA_SERVICE_HOST=ollama
OLLAMA_PORT=11434
OLLAMA_PORT_HOST=11435
OLLAMA_DEBUG=
# Devika backend application folder
DEVIKA_APP_ROOT="/home/devika"
DEVIKA_API_PORT=1337
# PyTorch
TOKENIZERS_PARALLELISM=true
# UI frontend
VITE_API_BASE_URL="http://127.0.0.1:1337"
#
DEBUG=true
# Specify playwright version if necessary
PLAYWRIGHT_VERSION=1.43.0
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ cython_debug/
.idea/

notes.md
data/
data/
**/.history
*.code-workspace
49 changes: 28 additions & 21 deletions app.dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
FROM debian:12
FROM node:18.20.2-bullseye

# setting up build variable
ARG VITE_API_BASE_URL
ENV VITE_API_BASE_URL=${VITE_API_BASE_URL}
ARG vite_api_base_url
ARG user
ARG uid
ARG debug
ARG dev_mode
ARG apt_cache_dir=/var/cache/apt

# setting up os env
USER root
WORKDIR /home/nonroot/client
RUN groupadd -r nonroot && useradd -r -g nonroot -d /home/nonroot/client -s /bin/bash nonroot
ENV VITE_API_BASE_URL=${vite_api_base_url}
ENV DEBUG="$debug"
ENV DEBIAN_FRONTEND=noninteractive

# install node js
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y build-essential software-properties-common curl sudo wget git
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
RUN apt-get install nodejs
WORKDIR /root/webui

# copying devika app client only
COPY ui /home/nonroot/client/ui
COPY src /home/nonroot/client/src
COPY config.toml /home/nonroot/client/
RUN --mount=type=cache,target=${apt_cache_dir},sharing=locked \
if [ -n "${debug}" ]; then set -eux; fi && \
apt-get -q update > /dev/null && \
if [ -z "${dev_mode}" ]; then apt-get -qy upgrade > /dev/null; fi && \
apt-get install -qy build-essential software-properties-common curl wget git > /dev/null

RUN cd ui && npm install && npm install -g npm && npm install -g bun
RUN chown -R nonroot:nonroot /home/nonroot/client
COPY ui ui
COPY src src
COPY config.toml .

USER nonroot
WORKDIR /home/nonroot/client/ui
ARG npm_cache_dir=/var/cache/npm

ENTRYPOINT [ "npx", "bun", "run", "dev", "--", "--host" ]
WORKDIR /root/webui/ui

RUN --mount=type=cache,target=${npm_cache_dir},sharing=locked \
npm config set --global cache "${npm_cache_dir}" && \
npm install -g npm@latest > /dev/null && \
yarn install --frozen-lockfile > /dev/null

ENTRYPOINT [ "yarn", "run", "dev", "--host" ]
107 changes: 76 additions & 31 deletions devika.dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,81 @@
FROM debian:12
FROM python:3.11.9-bookworm as backend

# setting up os env
USER root
WORKDIR /home/nonroot/devika
RUN groupadd -r nonroot && useradd -r -g nonroot -d /home/nonroot/devika -s /bin/bash nonroot
ARG root_dir

WORKDIR "${root_dir}"

ARG debug
ARG dev_mode
ARG apt_cache_dir=/var/cache/apt

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV DEBUG="$debug"
ENV DEBIAN_FRONTEND=noninteractive
ENV NVIDIA_VISIBLE_DEVICES=all

RUN --mount=type=cache,target=${apt_cache_dir},sharing=locked \
if [ -n "${debug}" ]; then set -eux; fi && \
apt-get update && \
if [ -z "${dev_mode}" ]; then apt-get -qy upgrade > /dev/null; fi && \
apt-get install -y --no-install-recommends software-properties-common \
curl wget git

ADD --checksum=sha256:4da8dde69eca0d9bc31420349a204851bfa2a1c87aeb87fe0c05517797edaac4 https://repo.anaconda.com/miniconda/Miniconda3-py311_24.3.0-0-Linux-x86_64.sh /tmp/

ARG conda_root=/var/miniconda3
ARG venv_name=devika_env

ENV CONDA_ROOT="${conda_root}"
ENV VENV_NAME="${venv_name}"
ENV APP_ROOT="${root_dir}"

RUN if [ -n "${debug}" ]; then set -eux; fi && \
echo "Installing Miniconda..." && \
mkdir -p "${CONDA_ROOT}" && \
bash /tmp/Miniconda3-py311_24.3.0-0-Linux-x86_64.sh -b -u -p ${CONDA_ROOT} > /dev/null

ARG user=root
ARG uid
ARG conda_pkgs_dir="${CONDA_ROOT}/pkgs"

ENV PATH="/home/${user}/.local/bin:${CONDA_ROOT}/bin:${PATH}"
ENV PYTHONPATH="${root_dir}/devika"

WORKDIR ${APP_ROOT}

COPY environment.yml .

RUN --mount=type=cache,target=${conda_pkgs_dir},sharing=locked \
if [ -n "${debug}" ]; then set -eux; fi && \
conda config --add channels conda && \
conda config --add channels conda-forge && \
conda config --add channels anaconda && \
conda config --add channels microsoft && \
conda install -qy pip && \
conda env create --file environment.yml -n "${venv_name}"

COPY src src
COPY sample.config.toml .
COPY devika.py .
COPY entrypoint.sh /docker-entrypoint.sh

ARG ollama_endpoint
ENV OLLAMA_ENDPOINT=$ollama_endpoint

# Patch source files for Docker environment
RUN if [ -n "${debug}" ]; then set -eux; fi && \
chmod a+x /docker-entrypoint.sh && \
sed -i 's#OLLAMA = "http://127.0.0.1:11434"#OLLAMA = "OLLAMA_ENDPOINT"#' sample.config.toml && \
echo "import os" | cat - src/llm/ollama_client.py > temp_file && mv -f temp_file src/llm/ollama_client.py && \
sed -i 's#Config().get_ollama_api_endpoint()#os.getenv(Config().get_ollama_api_endpoint())#g' src/llm/ollama_client.py


ENTRYPOINT [ "/docker-entrypoint.sh" ]

# Activate Miniconda environment
RUN eval "$(conda shell.bash activate "$venv_name")"
# Make RUN commands use the new environment
SHELL [ "conda", "run", "-n $venv_name /bin/bash -c" ]

# setting up python3
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y build-essential software-properties-common curl sudo wget git
RUN apt-get install -y python3 python3-pip
RUN curl -fsSL https://astral.sh/uv/install.sh | sudo -E bash -
RUN $HOME/.cargo/bin/uv venv
ENV PATH="/home/nonroot/devika/.venv/bin:$HOME/.cargo/bin:$PATH"

# copy devika python engine only
RUN $HOME/.cargo/bin/uv venv
COPY requirements.txt /home/nonroot/devika/
RUN UV_HTTP_TIMEOUT=100000 $HOME/.cargo/bin/uv pip install -r requirements.txt
RUN playwright install-deps chromium

COPY src /home/nonroot/devika/src
COPY config.toml /home/nonroot/devika/
COPY devika.py /home/nonroot/devika/
RUN chown -R nonroot:nonroot /home/nonroot/devika

USER nonroot
WORKDIR /home/nonroot/devika
ENV PATH="/home/nonroot/devika/.venv/bin:$HOME/.cargo/bin:$PATH"
RUN mkdir /home/nonroot/devika/db
RUN playwright install chromium

ENTRYPOINT [ "python3", "-m", "devika" ]
CMD [ "python3", "-m", "devika" ]
77 changes: 49 additions & 28 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,61 +1,82 @@
version: "3.9"

services:
ollama-service:
devika-ollama-service:
image: ollama/ollama:latest
expose:
- 11434
ports:
- 11434:11434
hostname: ${OLLAMA_SERVICE_HOST}
# cURL not provided by image
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:11434/ || exit 1"]
interval: 5s
test: ["CMD-SHELL", "wget http://localhost:${OLLAMA_PORT:?}/ > /dev/null || exit 1"]
interval: 15s
timeout: 30s
retries: 5
retries: 4
start_period: 30s
networks:
- devika-subnetwork
devika-subnetwork:
# Uncomment below to access Ollama from HOST machine
# ports:
# - "${OLLAMA_PORT_HOST:?}:${OLLAMA_PORT:?}"
environment:
- OLLAMA_HOST=0.0.0.0:${OLLAMA_PORT:?}
- OLLAMA_ORIGINS=${OLLAMA_ORIGINS:?}
- OLLAMA_KEEP_ALIVE=${OLLAMA_KEEP_ALIVE:?}
- OLLAMA_DEBUG=${OLLAMA_DEBUG}
- OLLAMA_MODELS=${OLLAMA_MODELS:?}
volumes:
- ${HOST_OLLAMA_MODELS_DIR}:${OLLAMA_MODELS:?}
tty: true

devika-backend-engine:
image: devika-backend:conda-cuda12
hostname: backend.devika
build:
context: .
dockerfile: devika.dockerfile
depends_on:
- ollama-service
expose:
- 1337
ports:
- 1337:1337
args:
- debug=true
- dev_mode=true
- root_dir=${DEVIKA_APP_ROOT}
- host_docker_root=/etc/systemd/system/docker-compose.d
- ollama_endpoint=http://${OLLAMA_SERVICE_HOST}:${OLLAMA_PORT}
env_file: .env
environment:
- OLLAMA_HOST=http://ollama-service:11434
- TOKENIZERS_PARALLELISM=${TOKENIZERS_PARALLELISM:?}
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:1337/ || exit 1"]
interval: 5s
interval: 15s
timeout: 30s
retries: 5
retries: 4
start_period: 30s
ports:
- "${DEVIKA_API_PORT:?}:${DEVIKA_API_PORT:?}"
volumes:
- devika-backend-dbstore:/home/nonroot/devika/db
- devika-backend-dbstore:${DEVIKA_APP_ROOT:?}/db
- devika-root-vol:/root
networks:
- devika-subnetwork
devika-subnetwork:
working_dir: ${DEVIKA_APP_ROOT:?}
depends_on:
- devika-ollama-service
tty: true

devika-frontend-app:
build:
context: .
dockerfile: app.dockerfile
args:
- VITE_API_BASE_URL=http://127.0.0.1:1337
- vite_api_base_url=${VITE_API_BASE_URL:?}
depends_on:
- devika-backend-engine
expose:
- 3000
- devika-ollama-service
ports:
- 3000:3000
- ${DEVIKA_UI_PORT:?}:3000
networks:
- devika-subnetwork
devika-subnetwork:
tty: true

networks:
devika-subnetwork:
ipam:
driver: default

volumes:
devika-backend-dbstore:
devika-backend-dbstore:
devika-root-vol:
20 changes: 20 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# Activate Miniconda environment
eval "$(conda shell.bash activate "$VENV_NAME")"

echo current directory is: $(pwd)

if [ -n ${DEBUG} ]; then
set -eux;
nvidia-smi
which python3
pip show transformers
fi

echo "Upating Playwright Chromium browser\nPlease wait..."
# Not found if installed at build stage
playwright install --with-deps chromium > /dev/null

python3 -m devika

Loading