Skip to main content

Self-Hosting docs.rs for a Private Cargo Registry

Self-Hosting docs.rs for a Private Cargo Registry

Documentation for Private Crates

Every crate on crates.io gets its documentation on docs.rs a few minutes after release. Our team also publishes internal crates to a private Cargo registry. Initially I tried Kellnr, a private registry that also builds documentation. Kellnr failed to build the documentation of many of our internal crates, and we did not find the cause.

Because docs.rs is open source, we decided to run our own instance.

Upstream Support

In August 2022, issue #1791 reported that the commands in the self-hosting guide on the Rust Forge did not work. @syphar agreed that the guide was out of date. They recommended the development environment from the docs.rs README, which can build and serve documentation. The reporter decided to use the development environment and closed the issue.

In August 2023, issue #2203 asked how to run docs.rs for a private registry, because the guide did not mention private registries. @syphar answered that two settings configure the registry:

  • REGISTRY_URL: the git repository of the registry index.
  • DOCSRS_REGISTRY_API_HOST: the HTTP API of the registry.

In January 2026, @syphar closed issue #2203. The docs.rs team focuses on crates.io, and with its current capacity it cannot also support private deployments. For registries that include documentation, @syphar suggested Kellnr, the registry that did not work for us.

Registries Under a Path

Our registry runs on Gitea, which serves its Cargo API under a path, such as https://gitea.example.com/api/packages/<owner>/cargo/. Our docs.rs instance built the documentation, but search did not work.

In October 2024, most registry requests of docs.rs went through one client, RegistryApi. RegistryApi kept the full path of the API host. Only two parts of the search code still sent their own requests: the first search, and each further page of results. These parts joined the absolute path /api/v1/crates to the API host with Url::join. An absolute path replaces the whole path of the base URL:

DOCSRS_REGISTRY_API_HOST=https://host/api/packages/docsrs/cargo/

Expected: https://host/api/packages/docsrs/cargo/api/v1/crates
Actual:   https://host/api/v1/crates

Because the fixes were small, I opened two pull requests. PR #2642 changed the join so that it kept the full path. In the review, @syphar suggested that search also belongs in RegistryApi. PR #2649 then moved the two search requests into RegistryApi. This completed the move. Now docs.rs builds the URLs of all registry requests in one place. @syphar reviewed and merged both pull requests within a week, in October 2024.

With these changes, most features of docs.rs work with a private registry. Some pages still refer to crates.io. Even so, self-hosting works well, and we have used it internally since the pull requests merged.

In September 2026, PR #3489 changed docs.rs to get release data from the sparse index. Now docs.rs reads the API and download URLs from the config.json file of the sparse index, and it ignores DOCSRS_REGISTRY_API_HOST. RegistryApi still adds api/v1/crates to the path of the API URL, so registries under a path continue to work.

Self-Hosting Tutorial

This tutorial follows the advice from issue #1791 and uses the Dockerfile of the docs.rs development environment. It runs docs.rs and an example Gitea registry with a single Docker Compose file, compose.yml. A simple production environment has the same parts: the docs.rs services, a PostgreSQL instance and an S3 bucket.

The registry watcher follows the git index of the Cargo registry and adds new releases to the build queue in PostgreSQL. The builder takes releases from the queue, downloads crates through the sparse index and stores the documentation in S3. The web server reads from PostgreSQL and S3.

A docs.rs deployment has these parts:

  • registry-watcher follows the git index of the registry. It adds each new release to the build queue.
  • builder takes releases from the queue. It downloads each crate through the sparse index and builds the documentation in a sandbox container.
  • web serves the documentation.
  • PostgreSQL holds the releases and the build queue.
  • S3 holds the documentation files.

Requirements

  • Docker with Docker Compose, on an x86_64 Linux host. The sandbox images for builds are available only for x86_64.
  • A Cargo registry with a sparse index and a git index that both allow anonymous read access. Gitea and Forgejo provide both indexes.
  • A master branch in the git index, because the registry watcher follows only master. Gitea creates the index on its default branch, which is main unless you change it.

The docs.rs services do not support registries that require authentication. They send no credentials when it reads the indexes or downloads crates. We keep our registry private with a network policy instead. The registry allows anonymous read access, but only our internal network can reach it. The docs.rs web server has no login either, so keep it behind the same network policy.

Support for registry authentication would be an easy contribution to docs.rs. When @syphar closed issue #2203, they wrote that more help would let the team also support private registries. The developer guide explains how to start.

Configuration

Docker Compose builds the docs.rs images from the docs.rs repository at a pinned commit. The Dockerfile in the repository has one target for each service. All docs.rs services share one set of environment variables:

# 230a612 is the docs.rs commit that this tutorial was tested with
x-docsrs-build: &docsrs-build
  context: https://github.com/rust-lang/docs.rs.git#230a612ec0eb271590cd46c7993e82c0f842006c
  dockerfile: dockerfiles/Dockerfile

x-docsrs: &docsrs
  depends_on:
    migrate:
      condition: service_completed_successfully
  environment: &docsrs-environment
    DOCSRS_PREFIX: /opt/docsrs/prefix
    DOCSRS_DATABASE_URL: postgresql://docsrs:docsrs@db
    S3_ENDPOINT: http://s3:9000
    AWS_ACCESS_KEY_ID: docsrs
    AWS_SECRET_ACCESS_KEY: docsrs-secret
    DOCSRS_SPARSE_INDEX_HOST: sparse+http://gitea:3000/api/packages/docsrs/cargo/
    REGISTRY_URL: http://gitea:3000/docsrs/_cargo-index.git

Two settings connect docs.rs to the registry:

  • DOCSRS_SPARSE_INDEX_HOST: the sparse index, for crate metadata, downloads and search.
  • REGISTRY_URL: the git index, which the registry watcher clones and follows.

PostgreSQL and RustFS, an S3-compatible object store, keep the data. RustFS starts without buckets, so s3-init creates the default docs.rs bucket, rust-docs-rs.

migrate creates the database schema before the other services start. Each docs.rs service reads the config.json file of the sparse index when it starts, so the registry must be available first:

services:
  db:
    image: postgres:19-alpine
    environment:
      POSTGRES_USER: docsrs
      POSTGRES_PASSWORD: docsrs
    volumes:
      - postgres-data:/var/lib/postgresql
    healthcheck:
      test: pg_isready --username docsrs
      start_period: 60s

  s3:
    image: rustfs/rustfs:latest
    command: /data
    environment:
      RUSTFS_ACCESS_KEY: docsrs
      RUSTFS_SECRET_KEY: docsrs-secret
    volumes:
      - s3-data:/data
    healthcheck:
      test: curl --fail http://localhost:9000/health
      start_period: 60s

  s3-init:
    image: amazon/aws-cli:latest
    depends_on:
      s3:
        condition: service_healthy
    environment:
      AWS_ACCESS_KEY_ID: docsrs
      AWS_SECRET_ACCESS_KEY: docsrs-secret
      AWS_DEFAULT_REGION: us-east-1
      AWS_ENDPOINT_URL: http://s3:9000
    entrypoint: [sh, -c, "aws s3api head-bucket --bucket rust-docs-rs || aws s3 mb s3://rust-docs-rs"]

  migrate:
    build:
      <<: *docsrs-build
      target: cli
    depends_on:
      db:
        condition: service_healthy
      s3-init:
        condition: service_completed_successfully
      # docs.rs reads the registry configuration when it starts
      publish-crate:
        condition: service_completed_successfully
    environment: *docsrs-environment
    command: database migrate

The three docs.rs services use the shared configuration. The builder runs each build in a sandbox container on the Docker engine of the host. Thus it needs the Docker socket, and its workspace must be on a volume:

  web:
    <<: *docsrs
    build:
      <<: *docsrs-build
      target: web-server
    ports:
      - "3000:3000"

  registry-watcher:
    <<: *docsrs
    build:
      <<: *docsrs-build
      target: registry-watcher

  builder:
    <<: *docsrs
    build:
      <<: *docsrs-build
      target: build-server
    environment:
      <<: *docsrs-environment
      DOCSRS_DOCKER: "true"
      DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/docsrs/rustwide

Example Registry

To try the setup, add a Gitea registry and a service that publishes one crate to it. The service creates the git index through the Gitea API, on a master branch. Because the first commit of the index comes before the first release, the command in the next section can queue that release:

  gitea:
    image: gitea/gitea:1.24
    environment:
      GITEA__security__INSTALL_LOCK: "true"
      GITEA__server__ROOT_URL: http://gitea:3000/
    healthcheck:
      test: curl --fail http://localhost:3000/api/healthz
      interval: 2s
      start_period: 60s

  publish-crate:
    image: gitea/gitea:1.24
    depends_on:
      gitea:
        condition: service_healthy
    volumes_from:
      - gitea
    entrypoint: [sh, -ec]
    command:
      - |
        # Docker Compose runs this service again on each start, so skip a published crate
        curl --fail --silent http://gitea:3000/api/packages/docsrs/cargo/he/ll/hello-docs && exit
        apk add cargo
        su-exec git gitea admin user create --username docsrs --password docsrs-password --email docsrs@example.com
        TOKEN=$$(su-exec git gitea admin user generate-access-token --username docsrs --scopes all --raw)
        curl --fail -H "Authorization: token $$TOKEN" --json '{"name": "_cargo-index", "auto_init": true, "default_branch": "master"}' http://gitea:3000/api/v1/user/repos
        cargo new --lib --vcs none hello-docs && cd hello-docs
        cargo publish --index sparse+http://gitea:3000/api/packages/docsrs/cargo/ --token "Bearer $$TOKEN"

volumes:
  postgres-data: {}
  s3-data: {}

Testing

To test locally, start all services with one command:

docker compose up -d

On an empty database, the registry watcher queues only the releases that come after its first check of the index. To build the releases that are already in the registry, set its last-seen commit to the first commit of the index:

docker compose exec registry-watcher sh -c \
  'docs_rs_watcher queue set-last-seen-reference $(git -C "$DOCSRS_PREFIX/crates.io-index" rev-list --max-parents=0 origin/master)'

When the builder finishes the build, the home page at http://localhost:3000 lists hello-docs in its recent releases, and the documentation is available at http://localhost:3000/hello-docs.

The home page of the self-hosted docs.rs, with a search box and hello-docs 0.1.0 in the list of recent releases.

To use your own registry set DOCSRS_SPARSE_INDEX_HOST and REGISTRY_URL to the indexes of your registry and host your own PostgreSQL instance and S3 bucket.

The sandbox containers download the dependencies of each crate. If a crate depends on other private crates, the sandbox containers must reach your registry through the host network.

Conclusion

With two registry settings, docs.rs documents a private registry. The URL fixes from 2024 made registries under a path work, and the sparse index now removes the need for a separate API setting.

Back to top