This documentation provides a detailed guide on configuring and utilizing Docker within the development environment of the sourcegraph/zoekt project. This will focus exclusively on the development aspects, leveraging the Docker setup for building and running zoekt.

Dockerfile Overview

The main entry point for Docker development configuration in the sourcegraph/zoekt project is the Dockerfile. This file outlines the multi-stage build process that consists of various stages to build the Go code, compile Rust components, and set up the final runtime environment.

Step-by-Step Breakdown of the Dockerfile

1. The Go Builder Stage

The first stage of the Dockerfile uses a Golang base image to compile the Go code.

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/...
  • Base Image: The base image used is golang:1.22.2-alpine3.19, which is lightweight and tailored for Go development.

  • Dependencies: The command RUN go mod download caches dependencies listed in go.mod and go.sum. This step is crucial for speeding up the build process because it ensures that dependencies are only downloaded once, unless they change.

  • Build Process: The final command in this stage compiles the Go code located in the cmd directory, utilizing the -ldflags flag to embed the version information.

2. The Rust Builder Stage

In the second stage, Rust is utilized to compile any required components.

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

# Because .cargo/config.toml doesn't support triplet-specific env
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
  • Base Image: This stage uses rust:alpine3.19, ensuring a lightweight Rust environment.

  • Dependency Installation: The command installs additional build tools necessary for compiling Rust projects, such as git, wget, and build-essential packages.

  • Source Downloading and Building: It retrieves a specific version of the Sourcegraph repository, sets up the syntax highlighter, and installs scip-ctags, a binary required by the zoekt component.

3. The Final Stage

The last stage assembles everything into a single runnable image.

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", "--"]
  • Base Image: Uses Alpine to keep the image size minimal.

  • Required Packages: Installs essential packages such as git, ca-certificates, and others that are necessary for the application to run smoothly.

  • Scripts and Binaries: The install-ctags-alpine.sh script is copied and executed, and relevant binaries from earlier stages are transferred into the final image.

  • ENTRYPOINT: Defines the entry point for the Docker image using Tini for proper process handling.

Building and Running the Docker Image

To build and run the Docker image based on the provided Dockerfile, follow these commands:

  1. Build the Docker Image:

    docker build -t zoekt:development --build-arg VERSION=your_version_tag .
    

    Replace your_version_tag with the desired version or commit hash you want to build.

  2. Run the Docker Container:

    docker run -it --rm zoekt:development
    

This command will start the container interactively, allowing you to test and develop zoekt as needed.

Conclusion

This Docker configuration empowers developers to work efficiently within the sourcegraph/zoekt project. By using a multi-stage Dockerfile approach, the build process is optimized for fast dependency resolution, language-specific tooling, and lightweight runtime environments. This setup not only ensures consistency across development environments but also increases productivity by streamlining the build process.

Source: Dockerfile from sourcegraph/zoekt repository