Skip to content

fix: Dockerfile support for both published and dev version #685

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

Merged
merged 2 commits into from
Feb 27, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ RUN apt-get update && apt-get install -y \
# Cleanup apt cache to reduce image size
&& rm -rf /var/lib/apt/lists/*

# Set git config
RUN git config --global user.email "[email protected]" \
&& git config --global user.name "codegen-bot"

# Install nvm and Node.js
SHELL ["/bin/bash", "-c"]
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \
Expand All @@ -46,14 +42,23 @@ RUN node --version \
&& pnpm --version \
&& python --version

# Build codegen
WORKDIR /codegen-sdk
COPY . .
# Add build argument for codegen version and build type
ARG CODEGEN_VERSION
ARG BUILD_TYPE="release" # Can be "release" or "dev"

# Install codegen based on BUILD_TYPE
RUN --mount=type=cache,target=/root/.cache/uv \
uv venv && source .venv/bin/activate \
&& uv sync --frozen --no-dev --all-extras \
&& uv pip install --system -e . --no-deps \
&& uv pip install --system .
--mount=type=bind,source=.,target=/codegen-sdk,rw \
if [ "$BUILD_TYPE" = "release" ]; then \
uv pip install --system codegen==${CODEGEN_VERSION}; \
else \
cd /codegen-sdk \
&& uv venv && source .venv/bin/activate \
&& uv sync --frozen --no-dev --all-extras \
&& uv pip install --system -e . --no-deps \
&& uv pip install --system .; \
fi

RUN codegen --version

# Create a non-root user for local development + debugging
Expand Down
37 changes: 25 additions & 12 deletions src/codegen/cli/commands/start/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ def start_command(port: int | None, detached: bool = False, skip_build: bool = F
else:
return _handle_existing_container(repo_config, container, force)

codegen_version = version("codegen")
rich.print(f"[bold green]Codegen version:[/bold green] {codegen_version}")
codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent
if port is None:
port = get_free_port()

try:
if not skip_build:
_build_docker_image(codegen_root)
codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent
codegen_version = version("codegen")
_build_docker_image(codegen_root=codegen_root, codegen_version=codegen_version)
_run_docker_container(repo_config, port, detached)
rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://{_default_host}:{port}[/bold]", box=ROUNDED, title="Codegen Server"))
# TODO: memory snapshot here
Expand All @@ -51,7 +50,7 @@ def start_command(port: int | None, detached: bool = False, skip_build: bool = F
raise click.Abort()


def _handle_existing_container(repo_config: RepoConfig, container: DockerContainer, force: bool) -> None:
def _handle_existing_container(repo_config: RepoConfig, container: DockerContainer) -> None:
if container.is_running():
rich.print(
Panel(
Expand All @@ -70,34 +69,48 @@ def _handle_existing_container(repo_config: RepoConfig, container: DockerContain
click.Abort()


def _build_docker_image(codegen_root: Path) -> None:
platform = _get_platform()
dockerfile_path = Path(__file__).parent / "Dockerfile-runner"
def _build_docker_image(codegen_root: Path, codegen_version: str) -> None:
build_type = _get_build_type(codegen_version)
build_cmd = [
"docker",
"buildx",
"build",
"--platform",
platform,
_get_platform(),
"-f",
str(dockerfile_path),
str(Path(__file__).parent / "Dockerfile"),
"-t",
"codegen-runner",
"--build-arg",
f"CODEGEN_VERSION={codegen_version}",
"--build-arg",
f"BUILD_TYPE={build_type}",
"--load",
str(codegen_root),
]

# Only add the context path if we're doing a local build
if build_type == "dev":
build_cmd.append(str(codegen_root))
else:
build_cmd.append(".") # Minimal context when installing from PyPI

rich.print(
Panel(
f"{str.join(' ', build_cmd)}",
box=ROUNDED,
title="Running Build Command",
title=f"Running Build Command ({build_type})",
style="blue",
padding=(1, 1),
)
)
subprocess.run(build_cmd, check=True)


def _get_build_type(version: str) -> str:
"""Get the build type based on the version string."""
return "dev" if "dev" in version or "+" in version else "release"


def _get_platform() -> str:
machine = py_platform.machine().lower()
if machine in ("x86_64", "amd64"):
Expand Down
Loading