Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

feat: create Dockerfile for shipping codegate as image #113

Merged
merged 1 commit into from
Nov 28, 2024
Merged
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
60 changes: 60 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +22 to +30
Copy link
Contributor

@aponcedeleonch aponcedeleonch Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a 100% sure. But I think that if we copy the complete application into the app directory and then run poetry install --no-dev it should install codegate module. In other words, doing

COPY . /app

# Configure Poetry and install dependencies
RUN poetry config virtualenvs.create false && \
    poetry install --no-dev

Would already enable us to do

from codegate. # import whatever submodule

This has the benefit that we would avoid changing the PYTHONPATH further down the file

# Set the PYTHONPATH environment variable
ENV PYTHONPATH=/app/src

It's just a nit I noticed. I think as it is it should do the trick


# 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"]
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```