Skip to content

Commit

Permalink
Merge pull request #37 from ks6088ts-labs/feature/issue-36_support-do…
Browse files Browse the repository at this point in the history
…cker

support docker container for backend
  • Loading branch information
ks6088ts committed May 7, 2024
2 parents a45ea8b + 0601339 commit 78a078e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ ci-test: install-deps-dev format-check lint test ## run CI tests
DOCKER_REPO_NAME ?= ks6088ts
DOCKER_IMAGE_NAME ?= azure-ai-services-solutions
DOCKER_COMMAND ?= python main.py --help
DOCKER_TAG ?= local
DOCKERFILE ?= dockerfiles/backend.Dockerfile

# Tools
TOOLS_DIR ?= $(HOME)/.local/bin
Expand All @@ -60,24 +62,35 @@ TRIVY_VERSION ?= 0.49.1
.PHONY: docker-build
docker-build: ## build Docker image
docker build \
-t $(DOCKER_REPO_NAME)/$(DOCKER_IMAGE_NAME):$(GIT_TAG) \
--tag $(DOCKER_REPO_NAME)/$(DOCKER_IMAGE_NAME):$(DOCKER_TAG) \
--file $(DOCKERFILE) \
--build-arg GIT_REVISION=$(GIT_REVISION) \
--build-arg GIT_TAG=$(GIT_TAG) \
.

.PHONY: docker-run
docker-run: ## run Docker container
docker run --rm $(DOCKER_REPO_NAME)/$(DOCKER_IMAGE_NAME):$(GIT_TAG) $(DOCKER_COMMAND)
docker run --rm \
--publish 8888:8888 \
--volume $(PWD)/azure_ai_speech.env.sample:/app/azure_ai_speech.env \
--volume $(PWD)/azure_ai_vision.env.sample:/app/azure_ai_vision.env \
--volume $(PWD)/azure_event_grid.env.sample:/app/azure_event_grid.env \
--volume $(PWD)/azure_openai.env.sample:/app/azure_openai.env \
--volume $(PWD)/azure_storage.env.sample:/app/azure_storage.env \
--volume $(PWD)/azure_storage_queue.env.sample:/app/azure_storage_queue.env \
--volume $(PWD)/document_intelligence.env.sample:/app/document_intelligence.env \
$(DOCKER_REPO_NAME)/$(DOCKER_IMAGE_NAME):$(DOCKER_TAG) \
$(DOCKER_COMMAND)

.PHONY: docker-lint
docker-lint: ## lint Dockerfile
docker run --rm -i hadolint/hadolint < Dockerfile
docker run --rm -i hadolint/hadolint < $(DOCKERFILE)

.PHONY: docker-scan
docker-scan: ## scan Docker image
@# https://aquasecurity.github.io/trivy/v0.18.3/installation/#install-script
@which trivy || curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b $(TOOLS_DIR) v$(TRIVY_VERSION)
trivy image $(DOCKER_REPO_NAME)/$(DOCKER_IMAGE_NAME):$(GIT_TAG)
trivy image $(DOCKER_REPO_NAME)/$(DOCKER_IMAGE_NAME):$(DOCKER_TAG)

.PHONY: ci-test-docker
ci-test-docker: docker-lint docker-build docker-scan docker-run ## run CI test for Docker
Expand Down
2 changes: 1 addition & 1 deletion azure_storage_queue.env.sample
Original file line number Diff line number Diff line change
@@ -1 +1 @@
AZURE_STORAGE_QUEUE_CONNECTION_STRING = "<connection-string>"
AZURE_STORAGE_QUEUE_CONNECTION_STRING = "DefaultEndpointsProtocol=https;AccountName=<AccountName>;AccountKey=<AccountKey>;EndpointSuffix=core.windows.net"
5 changes: 0 additions & 5 deletions backend/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ def custom_openapi():
version="0.0.1",
description="This contains a collection of solutions that leverage Azure AI services.",
routes=app.routes,
servers=[
{
"url": "http://localhost:8000",
}
],
)
openapi_schema["info"]["x-logo"] = {
"url": "https://news.microsoft.com/wp-content/uploads/prod/2022/05/Microsoft-logo_rgb_c-gray-1024x459.png"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile → dockerfiles/backend.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN pip install --no-cache-dir poetry==1.8.2

COPY ./pyproject.toml ./poetry.lock* /tmp/

RUN poetry export --without=dev -f requirements.txt --output requirements.txt --without-hashes
RUN poetry export --with=backend -f requirements.txt --output requirements.txt --without-hashes

FROM python:3.11.8-slim-bookworm

Expand Down
12 changes: 12 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# How to run

## Docker

```shell
# Build the Docker image
make docker-build

# Dry run the Docker container with default settings
make --dry-run docker-run DOCKER_COMMAND="python main.py backend --port 8888 --debug"
```

# References

## Common
Expand Down
31 changes: 21 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import typer
from dotenv import load_dotenv

debug = os.environ.get("DEBUG", "false").lower() not in ["false", "no", "0"]
log_level = logging.DEBUG if debug else logging.INFO

logging.basicConfig(
format="[%(asctime)s] %(levelname)7s from %(name)s in %(pathname)s:%(lineno)d: " "%(message)s",
level=log_level,
force=True,
)
def get_log_level(debug: bool) -> int:
return logging.DEBUG if debug else logging.INFO


def setup_logging(debug: bool = False):
logging.basicConfig(
format="[%(asctime)s] %(levelname)7s from %(name)s in %(pathname)s:%(lineno)d: " "%(message)s",
level=get_log_level(debug),
force=True,
)


app = typer.Typer()

Expand All @@ -22,13 +26,15 @@ def backend(
host="0.0.0.0",
port: Annotated[int, typer.Option(help="Port number")] = 8000,
reload: Annotated[bool, typer.Option(help="Enable auto-reload")] = False,
debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False,
):
from backend.entrypoint import start

setup_logging(debug)
start(
host=host,
port=port,
log_level=log_level,
log_level=get_log_level(debug),
reload=reload,
)

Expand All @@ -37,11 +43,13 @@ def backend(
def frontend(
solution_name: Annotated[str, typer.Option(help="Solution name")] = os.getenv("SOLUTION_NAME"),
backend_url: Annotated[str, typer.Option(help="Backend URL")] = os.getenv("BACKEND_URL", "http://localhost:8000/"),
debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False,
):
from frontend.entrypoint import start
from frontend.solutions.types import SolutionType

# convert solution_name to SolutionType
setup_logging(debug)

try:
solution_type = SolutionType(solution_name.upper())
except ValueError:
Expand All @@ -51,18 +59,21 @@ def frontend(
start(
solution_type=solution_type,
backend_url=backend_url,
log_level=log_level,
log_level=get_log_level(debug),
)


@app.command()
def generate_openapi_spec(
path: Annotated[str, typer.Option(help="Output file path")] = "./specs/openapi.json",
debug: Annotated[bool, typer.Option(help="Enable debug mode")] = False,
):
import json

from backend.fastapi import app

setup_logging(debug)

# create directory if not exists
os.makedirs(os.path.dirname(path), exist_ok=True)

Expand Down

0 comments on commit 78a078e

Please sign in to comment.