From 2804fef3c622916b87adc2e412bafe4f20ad22a7 Mon Sep 17 00:00:00 2001 From: Alexander Druz Date: Thu, 27 Jun 2024 13:02:17 +0200 Subject: [PATCH 1/3] Add dockerfile and commands to deploy images on Azure --- .dockerignore | 0 Dockerfile | 20 ++++++++++++++++ Makefile | 26 +++++++++++++++++++-- README.dev.md | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 README.dev.md diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e69de29 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e3463a8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +FROM python:3.11 + +ENV STREAMLIT_SERVER_ADDRESS=0.0.0.0 \ + STREAMLIT_SERVER_PORT=8000 + +WORKDIR /app + +RUN pip install -IU pip setuptools wheel && pip install poetry==1.8.3 + +COPY pyproject.toml poetry.lock README.md ./ +COPY ./assistant/__init__.py ./assistant/ + +RUN poetry install --extras=openai --sync --without=dev + +COPY ./assistant/ ./assistant/ +COPY settings.yaml ./ + +COPY ./db-docs/ ./db-docs/ + +CMD ["poetry", "run", "streamlit", "run", "assistant/app.py"] diff --git a/Makefile b/Makefile index b3640a3..1ed1e8b 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,28 @@ lint: ## Lint all source files run: ## Run web app poetry run streamlit run assistant/app.py -.PHONY: build -build: ## Build package +.PHONY: build-wheel +build-wheel: ## Build package poetry build -f wheel + +.PHONY: build-image +build-image: ## Build docker image + docker build -t renumics-rag -f Dockerfile . + +.PHONY: run-image +run-image: ## Build docker image +run-image: build-image + docker run -it --rm -e OPENAI_API_KEY=$$OPENAI_API_KEY -p 8000:8000 renumics-rag + +.PHONY: docker-login +docker-login: ## Log in to Azure registry + docker login -u "$$AZURE_REGISTRY_USERNAME" -p "$$AZURE_REGISTRY_PASSWORD" "$$AZURE_REGISTRY" + +.PHONY: release-image +release-image: ## Tag and push image to Azure registry +release-image: docker-login build-image + TIMESTAMP="$(shell date '+%Y-%m-%d_%H-%M-%S')" + docker tag renumics-rag "$${AZURE_REGISTRY}/renumics-rag:$$TIMESTAMP" + docker push "$${AZURE_REGISTRY}/renumics-rag:$$TIMESTAMP" + docker tag renumics-rag "$${AZURE_REGISTRY}/renumics-rag:latest" + docker push "$${AZURE_REGISTRY}/renumics-rag:latest" diff --git a/README.dev.md b/README.dev.md new file mode 100644 index 0000000..7d0f685 --- /dev/null +++ b/README.dev.md @@ -0,0 +1,65 @@ +```shell +AZURE_RESOURCE_GROUP="VDISeminar" +AZURE_REGISTRY_NAME="${AZURE_RESOURCE_GROUP,,}acr" +AZURE_REGISTRY="${AZURE_REGISTRY_NAME}.azurecr.io" +``` + +**Create a new resource group:** + +```shell +az group create --location germanywestcentral --resource-group "$AZURE_RESOURCE_GROUP" +``` + +**Create a new container registry:** + +```shell +az acr create --resource-group "$AZURE_RESOURCE_GROUP" --sku Basic --location germanywestcentral --admin-enabled true --name "$AZURE_REGISTRY_NAME" +``` + +**Get username and password for the newly created container registry:** + +```shell +AZURE_REGISTRY_CREDENTIALS="$(az acr credential show --name "$AZURE_REGISTRY_NAME")" +AZURE_REGISTRY_USERNAME="$(jq -n "$AZURE_REGISTRY_CREDENTIALS" | jq -r '.username')" +AZURE_REGISTRY_PASSWORD="$(jq -n "$AZURE_REGISTRY_CREDENTIALS" | jq -r '[.passwords | .[] | select(.name=="password")][0] | .value')" +``` + +**Log in to the newly created container registry:** + +```shell +docker login -u "$AZURE_REGISTRY_USERNAME" -p "$AZURE_REGISTRY_PASSWORD" "$AZURE_REGISTRY" +``` + +**Build local image:** + +```shell +docker build -t renumics-rag -f Dockerfile . +``` + +**Tag and push local image to registry, with timestamp and as latest:** + +```shell +TIMESTAMP="$(shell date '+%Y-%m-%d_%H-%M-%S')" +docker tag renumics-rag "${AZURE_REGISTRY}/renumics-rag:$TIMESTAMP" +docker push "${AZURE_REGISTRY}/renumics-rag:$TIMESTAMP" +docker tag renumics-rag "${AZURE_REGISTRY}/renumics-rag:latest" +docker push "${AZURE_REGISTRY}/renumics-rag:latest" +``` + +**Create Azure app service plan with Linux and chosen subscription (1 CPU, 1.75 GB memory, 10 GB storage):** + +```shell +az appservice plan create --resource-group "$AZURE_RESOURCE_GROUP" --location germanywestcentral --sku B1 --is-linux --name "$AZURE_RESOURCE_GROUP" +``` + +**Create environment:** + +```shell +az containerapp env create --resource-group "$AZURE_RESOURCE_GROUP" --location germanywestcentral --name "${AZURE_RESOURCE_GROUP,,}-env" +``` + +**Create container app:** + +```shell +az containerapp create --resource-group "$AZURE_RESOURCE_GROUP" --environment "${AZURE_RESOURCE_GROUP,,}-env" --allow-insecure false --image "${AZURE_REGISTRY}/renumics-rag" --registry-server "$AZURE_REGISTRY" --registry-username "$AZURE_REGISTRY_USERNAME" --registry-password "$AZURE_REGISTRY_PASSWORD" --secrets "openai-api-key=$OPENAI_API_KEY" --env-vars "OPENAI_API_KEY=secretref:openai-api-key" --ingress external --target-port 8000 --name "${AZURE_RESOURCE_GROUP,,}-1" +``` From fbf67f3f7ac7ffb1c258a7f7cb4b36194fc941ec Mon Sep 17 00:00:00 2001 From: Alexander Druz Date: Thu, 27 Jun 2024 16:53:42 +0200 Subject: [PATCH 2/3] Update Makefile and DOckerfile --- Dockerfile | 2 +- Makefile | 15 +++++++++--- README.dev.md | 65 --------------------------------------------------- 3 files changed, 13 insertions(+), 69 deletions(-) delete mode 100644 README.dev.md diff --git a/Dockerfile b/Dockerfile index e3463a8..472f61b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,4 +17,4 @@ COPY settings.yaml ./ COPY ./db-docs/ ./db-docs/ -CMD ["poetry", "run", "streamlit", "run", "assistant/app.py"] +CMD ["poetry", "run", "app"] diff --git a/Makefile b/Makefile index 1ed1e8b..5ae7c3b 100644 --- a/Makefile +++ b/Makefile @@ -55,11 +55,20 @@ build-wheel: ## Build package build-image: ## Build docker image docker build -t renumics-rag -f Dockerfile . -.PHONY: run-image -run-image: ## Build docker image -run-image: build-image +.PHONY: run-image-openai +run-image-openai: ## Build docker image +run-image-openai: build-image docker run -it --rm -e OPENAI_API_KEY=$$OPENAI_API_KEY -p 8000:8000 renumics-rag +.PHONY: run-image-azure +run-image-azure: ## Build docker image +run-image-azure: build-image + docker run -it --rm \ + -e OPENAI_API_TYPE=$$OPENAI_API_TYPE \ + -e OPENAI_API_VERSION=$$OPENAI_API_VERSION \ + -e AZURE_OPENAI_API_KEY=$$AZURE_OPENAI_API_KEY \ + -e AZURE_OPENAI_ENDPOINT=$$AZURE_OPENAI_ENDPOINT -p 8000:8000 renumics-rag + .PHONY: docker-login docker-login: ## Log in to Azure registry docker login -u "$$AZURE_REGISTRY_USERNAME" -p "$$AZURE_REGISTRY_PASSWORD" "$$AZURE_REGISTRY" diff --git a/README.dev.md b/README.dev.md deleted file mode 100644 index 7d0f685..0000000 --- a/README.dev.md +++ /dev/null @@ -1,65 +0,0 @@ -```shell -AZURE_RESOURCE_GROUP="VDISeminar" -AZURE_REGISTRY_NAME="${AZURE_RESOURCE_GROUP,,}acr" -AZURE_REGISTRY="${AZURE_REGISTRY_NAME}.azurecr.io" -``` - -**Create a new resource group:** - -```shell -az group create --location germanywestcentral --resource-group "$AZURE_RESOURCE_GROUP" -``` - -**Create a new container registry:** - -```shell -az acr create --resource-group "$AZURE_RESOURCE_GROUP" --sku Basic --location germanywestcentral --admin-enabled true --name "$AZURE_REGISTRY_NAME" -``` - -**Get username and password for the newly created container registry:** - -```shell -AZURE_REGISTRY_CREDENTIALS="$(az acr credential show --name "$AZURE_REGISTRY_NAME")" -AZURE_REGISTRY_USERNAME="$(jq -n "$AZURE_REGISTRY_CREDENTIALS" | jq -r '.username')" -AZURE_REGISTRY_PASSWORD="$(jq -n "$AZURE_REGISTRY_CREDENTIALS" | jq -r '[.passwords | .[] | select(.name=="password")][0] | .value')" -``` - -**Log in to the newly created container registry:** - -```shell -docker login -u "$AZURE_REGISTRY_USERNAME" -p "$AZURE_REGISTRY_PASSWORD" "$AZURE_REGISTRY" -``` - -**Build local image:** - -```shell -docker build -t renumics-rag -f Dockerfile . -``` - -**Tag and push local image to registry, with timestamp and as latest:** - -```shell -TIMESTAMP="$(shell date '+%Y-%m-%d_%H-%M-%S')" -docker tag renumics-rag "${AZURE_REGISTRY}/renumics-rag:$TIMESTAMP" -docker push "${AZURE_REGISTRY}/renumics-rag:$TIMESTAMP" -docker tag renumics-rag "${AZURE_REGISTRY}/renumics-rag:latest" -docker push "${AZURE_REGISTRY}/renumics-rag:latest" -``` - -**Create Azure app service plan with Linux and chosen subscription (1 CPU, 1.75 GB memory, 10 GB storage):** - -```shell -az appservice plan create --resource-group "$AZURE_RESOURCE_GROUP" --location germanywestcentral --sku B1 --is-linux --name "$AZURE_RESOURCE_GROUP" -``` - -**Create environment:** - -```shell -az containerapp env create --resource-group "$AZURE_RESOURCE_GROUP" --location germanywestcentral --name "${AZURE_RESOURCE_GROUP,,}-env" -``` - -**Create container app:** - -```shell -az containerapp create --resource-group "$AZURE_RESOURCE_GROUP" --environment "${AZURE_RESOURCE_GROUP,,}-env" --allow-insecure false --image "${AZURE_REGISTRY}/renumics-rag" --registry-server "$AZURE_REGISTRY" --registry-username "$AZURE_REGISTRY_USERNAME" --registry-password "$AZURE_REGISTRY_PASSWORD" --secrets "openai-api-key=$OPENAI_API_KEY" --env-vars "OPENAI_API_KEY=secretref:openai-api-key" --ingress external --target-port 8000 --name "${AZURE_RESOURCE_GROUP,,}-1" -``` From b5caf611df103e0ca9d74e7fadc35892f8e2424b Mon Sep 17 00:00:00 2001 From: Alexander Druz Date: Thu, 27 Jun 2024 16:55:05 +0200 Subject: [PATCH 3/3] Add content to .dckerignore --- .dockerignore | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/.dockerignore b/.dockerignore index e69de29..c72284a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -0,0 +1,21 @@ +.envrc +.gitignore +.pre-commit-config.yaml +.prettierrc.yaml +LICENSE +Makefile +assets/ +notebooks/ + +.env +data/ +dev/ +dist/ + +.vscode/ +.idea/ + +.DS_Store +__pycache__/ +.mypy_cache/ +.ruff_cache/