Skip to content

Commit 67b667d

Browse files
committed
feat: add --local optional to mount repo to docker
1 parent ef8b4c3 commit 67b667d

File tree

3 files changed

+82
-10
lines changed

3 files changed

+82
-10
lines changed

Dockerfile-runner

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
ARG LOCAL_REPO_PATH
2+
ARG REPO_NAME
3+
14
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
25

36
# Set environment variables to prevent interactive prompts during installation
@@ -59,4 +62,7 @@ RUN chown -R user:user /home/user
5962
USER user
6063

6164
WORKDIR /app
65+
66+
COPY ${LOCAL_REPO_PATH} /app/git/${REPO_NAME}
67+
6268
ENTRYPOINT ["/bin/bash", "-c"]

Dockerfile-runner-mount

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
2+
3+
# Set environment variables to prevent interactive prompts during installation
4+
ENV NVM_DIR=/root/.nvm \
5+
NODE_VERSION=18.17.0 \
6+
DEBIAN_FRONTEND=noninteractive \
7+
NODE_OPTIONS="--max-old-space-size=8192" \
8+
PYTHONUNBUFFERED=1 \
9+
COREPACK_ENABLE_DOWNLOAD_PROMPT=0 \
10+
PYTHONPATH="/usr/local/lib/python3.13/site-packages" \
11+
IS_SANDBOX=True \
12+
HATCH_BUILD_HOOKS_ENABLE=1
13+
14+
# Update packages lists and install git and curl
15+
RUN apt-get update && apt-get install -y \
16+
git \
17+
curl \
18+
gcc \
19+
build-essential \
20+
python3-dev \
21+
# Cleanup apt cache to reduce image size
22+
&& rm -rf /var/lib/apt/lists/*
23+
24+
# Install nvm and Node.js
25+
SHELL ["/bin/bash", "-c"]
26+
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \
27+
&& source $NVM_DIR/nvm.sh \
28+
&& nvm install $NODE_VERSION \
29+
&& nvm use default \
30+
&& npm install -g yarn pnpm \
31+
&& corepack enable \
32+
&& corepack prepare yarn@stable --activate \
33+
&& corepack prepare pnpm@latest --activate \
34+
&& uv pip install --system uvicorn[standard]
35+
36+
# Add node and npm to PATH
37+
ENV PATH=$NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
38+
RUN node --version \
39+
&& corepack --version \
40+
&& npm --version \
41+
&& yarn --version \
42+
&& pnpm --version \
43+
&& python --version
44+
45+
# Build codegen
46+
WORKDIR /codegen-sdk
47+
COPY . .
48+
RUN --mount=type=cache,target=/root/.cache/uv \
49+
uv venv && source .venv/bin/activate \
50+
&& uv sync --frozen --no-dev --all-extras \
51+
&& uv pip install --system -e . --no-deps \
52+
&& uv pip install --system .
53+
RUN codegen --version
54+
55+
# Create a non-root user for local development + debugging
56+
RUN useradd -m -s /bin/bash user
57+
USER root
58+
RUN chown -R user:user /home/user
59+
USER user
60+
61+
WORKDIR /app
62+
ENTRYPOINT ["/bin/bash", "-c"]

src/codegen/cli/commands/start/main.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
@click.option("--platform", "-t", type=click.Choice(["linux/amd64", "linux/arm64", "linux/amd64,linux/arm64"]), default="linux/amd64,linux/arm64", help="Target platform(s) for the Docker image")
1818
@click.option("--port", "-p", type=int, default=None, help="Port to run the server on")
1919
@click.option("--detached", "-d", is_flag=True, default=False, help="Starts up the server as detached background process")
20-
def start_command(port: int | None, platform: str, detached: bool):
20+
@click.option("--local", is_flag=True, default=False, help="If true, interacts with the mounted local repository. If false, interacts with the repository copied into the image.")
21+
def start_command(port: int | None, platform: str, detached: bool, local: bool):
2122
"""Starts a local codegen server"""
2223
codegen_version = version("codegen")
2324
rich.print(f"[bold green]Codegen version:[/bold green] {codegen_version}")
@@ -26,10 +27,12 @@ def start_command(port: int | None, platform: str, detached: bool):
2627
port = get_free_port()
2728

2829
try:
30+
repo_path = Path.cwd().resolve()
31+
repo_config = RepoConfig.from_repo_path(repo_path)
2932
rich.print("[bold blue]Building Docker image...[/bold blue]")
30-
_build_docker_image(codegen_root, platform)
33+
_build_docker_image(repo_config, codegen_root, platform, local)
3134
rich.print("[bold blue]Starting Docker container...[/bold blue]")
32-
_run_docker_container(port, detached)
35+
_run_docker_container(repo_config, port, detached, local)
3336
rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://0.0.0.0:{port}[/bold]", box=ROUNDED, title="Codegen Server"))
3437
except subprocess.CalledProcessError as e:
3538
rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container")
@@ -39,15 +42,18 @@ def start_command(port: int | None, platform: str, detached: bool):
3942
raise click.Abort()
4043

4144

42-
def _build_docker_image(codegen_root: Path, platform: str):
45+
def _build_docker_image(repo_config: RepoConfig, codegen_root: Path, platform: str, local: bool):
46+
build_arg = [] if local else ["--build-arg", f"LOCAL_REPO_PATH={repo_config.repo_path}", "--build-arg", f"REPO_NAME={repo_config.name}"]
47+
dockerfile = "Dockerfile-runner-mount" if local else "Dockerfile-runner"
4348
build_cmd = [
4449
"docker",
4550
"buildx",
4651
"build",
52+
*build_arg,
4753
"--platform",
4854
platform,
4955
"-f",
50-
str(codegen_root / "Dockerfile-runner"),
56+
str(codegen_root / dockerfile),
5157
"-t",
5258
"codegen-runner",
5359
"--load",
@@ -57,18 +63,16 @@ def _build_docker_image(codegen_root: Path, platform: str):
5763
subprocess.run(build_cmd, check=True)
5864

5965

60-
def _run_docker_container(port: int, detached: bool):
61-
repo_path = Path.cwd().resolve()
62-
repo_config = RepoConfig.from_repo_path(repo_path)
66+
def _run_docker_container(repo_config: RepoConfig, port: int, detached: bool, local: bool):
6367
container_repo_path = f"/app/git/{repo_config.name}"
6468
envvars = {
6569
"REPOSITORY_LANGUAGE": repo_config.language.value,
66-
"REPOSITORY_OWNER": LocalGitRepo(repo_path).owner,
70+
"REPOSITORY_OWNER": LocalGitRepo(repo_config.repo_path).owner,
6771
"REPOSITORY_PATH": container_repo_path,
6872
"GITHUB_TOKEN": SecretsConfig().github_token,
6973
}
7074
envvars_args = [arg for k, v in envvars.items() for arg in ("--env", f"{k}={v}")]
71-
mount_args = ["-v", f"{repo_path}:{container_repo_path}"]
75+
mount_args = ["-v", f"{repo_config.repo_path}:{container_repo_path}"] if local else []
7276
run_mode = "-d" if detached else "-it"
7377
entry_point = f"uv run --frozen uvicorn codegen.runner.sandbox.server:app --host 0.0.0.0 --port {port}"
7478
run_cmd = ["docker", "run", run_mode, "-p", f"{port}:{port}", *mount_args, *envvars_args, "codegen-runner", entry_point]

0 commit comments

Comments
 (0)