diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..1eb01839 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,60 @@ +# Builder stage: Install dependencies and build the application +FROM python:3.12-slim AS builder + +# Install system dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + gcc \ + g++ \ + && rm -rf /var/lib/apt/lists/* + +# Set environment variable to ensure Python modules are installed in the correct location +ENV PYTHONPATH=/app + +# Install Poetry +RUN pip install poetry==1.8.4 + +# Create a non-root user and switch to it +RUN adduser --system --no-create-home codegate --uid 1000 + +# Set the working directory +WORKDIR /app + +# Copy only the files needed for installing dependencies +COPY pyproject.toml poetry.lock* /app/ + +# Configure Poetry and install dependencies +RUN poetry config virtualenvs.create false && \ + poetry install --no-dev + +# Copy the rest of the application +COPY . /app + +# Runtime stage: Create the final lightweight image +FROM python:3.12-slim AS runtime + +# Install runtime system dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + libgomp1 \ + && rm -rf /var/lib/apt/lists/* + +# Create a non-root user and switch to it +RUN adduser --system --no-create-home codegate --uid 1000 +USER codegate + +# Copy necessary artifacts from the builder stage +COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages +COPY --from=builder /app /app + +# Set the working directory +WORKDIR /app + +# Set the PYTHONPATH environment variable +ENV PYTHONPATH=/app/src + +# Allow to expose weaviate_data volume +VOLUME ["/app/weaviate_data"] + +# Set the container's default entrypoint +EXPOSE 8989 +#ENTRYPOINT ["python", "-m", "src.codegate.cli", "serve", "--port", "8989", "--host", "0.0.0.0"] +CMD ["python", "-m", "src.codegate.cli", "serve", "--port", "8989", "--host", "0.0.0.0"] diff --git a/Makefile b/Makefile index b4743b58..ebb4b265 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ .PHONY: clean install format lint test security build all +CONTAINER_BUILD?=docker buildx build +VER?=0.1.0 clean: rm -rf build/ @@ -27,4 +29,8 @@ security: build: clean test poetry build +image-build: + $(CONTAINER_BUILD) -f Dockerfile -t codegate . -t ghcr.io/stacklok/codegate:$(VER) --load + + all: clean install format lint test security build diff --git a/README.md b/README.md index 718099de..c5509c5f 100644 --- a/README.md +++ b/README.md @@ -108,3 +108,19 @@ pip install -e ".[dev]" ```bash pytest ``` + + +### Running from image + +A docker image can be built just with `make image-build`. That will start a codegate server ready to use. +Then it can be started with: + +```bash +docker run -p 8989:8989 codegate:latest +``` + +Additionally if you want to start with a pre-created database, a volume can be mounted: + +```bash +docker run -p 8989:8989 -v /path/to/volume:/app/weaviate_data codegate:latest +```