This documentation provides an in-depth step-by-step guide to deploying the Sourcegraph Zoekt project in a production environment.

Prerequisites

Ensure you have the following prerequisites installed:

  • Docker: For building and running containers.
  • Go (version 1.22.2): Required for building the Go components.
  • Rust: To enable building the Rust-based syntax highlighter.

Building the Docker Image

The deployment of Sourcegraph Zoekt involves building a Docker image that packages all necessary components.

Dockerfile Breakdown

The Dockerfile outlines the multi-stage build process used to create the production-ready image:

FROM golang:1.22.2-alpine3.19 AS builder

RUN apk add --no-cache ca-certificates

ENV CGO_ENABLED=0
WORKDIR /go/src/github.com/sourcegraph/zoekt

# Cache dependencies
COPY go.mod go.sum ./
RUN go mod download

COPY . ./
ARG VERSION
RUN go install -ldflags "-X github.com/sourcegraph/zoekt.Version=$VERSION" ./cmd/...
  1. Base Image: The build begins with a base image of golang:1.22.2-alpine3.19.

  2. Certificates: The ca-certificates package is installed to facilitate secure communications.

  3. Build Environment: By setting CGO_ENABLED=0, you ensure that the binary is statically linked.

  4. Working Directory: The working directory is set to /go/src/github.com/sourcegraph/zoekt, which aligns with the Golang module path.

  5. Dependency Management: The go.mod and go.sum files are copied and dependencies are downloaded, thus caching them to avoid unnecessary downloads.

  6. Building: The project source code is then copied into the container, followed by invoking the Go install command. The -ldflags flag is used to inject the version number into the binary.

FROM rust:alpine3.19 AS rust-builder

RUN apk add --no-cache git wget musl-dev build-base

RUN wget -qO- https://github.com/sourcegraph/sourcegraph/archive/0c8aa18eece45922a2b56dc0f94e21b1bb533e7d.tar.gz | tar xz && mv sourcegraph-* sourcegraph

ARG TARGETARCH

# Build the syntax highlighter
RUN cd sourcegraph/docker-images/syntax-highlighter && /sourcegraph/cmd/symbols/cargo-config.sh && cd /

RUN cargo install --path sourcegraph/docker-images/syntax-highlighter --root /syntect_server --bin scip-ctags
  1. Rust Base Image: The second stage uses rust:alpine3.19 as the base image to accommodate the Rust syntax highlighter.

  2. Install Dependencies: The necessary dependencies for building the Rust application are installed.

  3. Download Sourcegraph: The Sourcegraph repository is downloaded and extracted.

  4. Build Syntax Highlighter: Change into the directory containing the syntax highlighter, running a configuration script, and subsequently installing the scip-ctags binary.

FROM alpine:3.19 AS zoekt

RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget

COPY install-ctags-alpine.sh .
RUN ./install-ctags-alpine.sh && rm install-ctags-alpine.sh

COPY --from=builder /go/bin/* /usr/local/bin/
COPY --from=rust-builder /syntect_server/bin/scip-ctags /usr/local/bin/scip-ctags

ENTRYPOINT ["/sbin/tini", "--"]
  1. Final Production Image: The third stage is based on alpine:3.19, which results in a lightweight image for production.

  2. Install Utilities: Required utilities are installed: git, ca-certificates, bind-tools, tini, and jansson.

  3. Install Ctags: The custom script install-ctags-alpine.sh is executed to install ctags.

  4. Copy Binaries: The final binaries from both the Go builder and Rust builder stages are copied into the final production image.

  5. Entry Point: The application starts with tini to manage process shutdown signals gracefully.

Building the Docker Image

To build the Docker image, execute the following command in the directory containing the Dockerfile:

docker build -t zoekt:latest --build-arg VERSION=your_version_here .

Replace your_version_here with the desired version string.

Running the Docker Container

Once the image is built, run the container using:

docker run -d --name zoekt -p 80:80 zoekt:latest

This command will start the container in detached mode and map port 80 from the container to port 80 on the host.

Conclusion

Following these steps provides a straightforward approach to deploying the Sourcegraph Zoekt project into a production environment, leveraging Docker for isolated and reproducible builds.

For additional context or changes, please see the relevant sections within the sourcegraph/zoekt repository.