Install Sourcegraph with Docker Compose on Google Cloud

This tutorial shows you how to deploy Sourcegraph via Docker Compose to a single node running on Google Cloud.

Determine server and service requirements

Use the resource estimator to determine the resource requirements for your environment. You will use this information to set up the instance and configure the docker-compose YAML file.

Prepare a fork

We strongly recommend that you create and run Sourcegraph from your own fork of the reference repository. You will make changes to the default configuration, for example to the docker-compose YAML file, in your fork. The fork will also enable you to keep track of your customizations when upgrading your fork from the reference repo. Refer to the following steps for preparing a clone, which use GitHub as an example, then return to this page:

  1. Fork the reference repo
  2. Clone your fork
  3. Configure the release branch
  4. Configure the YAML file
  5. Publish changes to your branch

Deploy to Google Cloud VM

  • Open your Google Cloud console to create a new VM instance and click Create Instance
  • Choose an appropriate machine type (use the resource estimator to find a good starting point for your deployment).
  • Under the “Boot Disk” options, select the following:
    • Operating System: Ubuntu
    • Version: Ubuntu 18.04 LTS
    • Boot disk type: SSD persistent disk
  • Check the boxes for Allow HTTP traffic and Allow HTTPS traffic in the Firewall section
  • Open the Management, disks, networking, and SSH keys dropdown section
  • Under the Management section, add the following in the Startup script field:
#!/usr/bin/env bash

set -euxo pipefail



# 🚨 Update these variables with the correct values from your fork!

# Install git
sudo apt-get update -y
sudo apt-get install -y git

# Clone Docker Compose definition

# Format (if necessary) and mount GCP persistent disk
device_fs=$(sudo lsblk "${PERSISTENT_DISK_DEVICE_NAME}" --noheadings --output fsType)
if [ "${device_fs}" == "" ] ## only format the volume if it isn't already formatted
    sudo mkfs.ext4 -m 0 -E lazy_itable_init=0,lazy_journal_init=0,discard "${PERSISTENT_DISK_DEVICE_NAME}"
sudo mkdir -p "${DOCKER_DATA_ROOT}"
sudo mount -o discard,defaults "${PERSISTENT_DISK_DEVICE_NAME}" "${DOCKER_DATA_ROOT}"

# Mount GCP disk on reboots
DISK_UUID=$(sudo blkid -s UUID -o value "${PERSISTENT_DISK_DEVICE_NAME}")
sudo echo "UUID=${DISK_UUID}  ${DOCKER_DATA_ROOT}  ext4  discard,defaults,nofail  0  2" >> '/etc/fstab'
umount "${DOCKER_DATA_ROOT}"
mount -a

# Install Docker
curl -fsSL | sudo apt-key add -
sudo apt-get update -y
sudo apt-get install -y software-properties-common
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
sudo apt-get update -y
apt-cache policy docker-ce
apt-get install -y docker-ce docker-ce-cli

# Install jq for scripting
sudo apt-get update -y
sudo apt-get install -y jq

# Edit Docker storage directory to mounted volume

## initialize the config file with empty json if it doesn't exist
    mkdir -p $(dirname "${DOCKER_DAEMON_CONFIG_FILE}")
    echo '{}' > "${DOCKER_DAEMON_CONFIG_FILE}"

## update Docker's 'data-root' to point to our mounted disk
trap "rm -f ${tmp_config}" EXIT
sudo cat "${DOCKER_DAEMON_CONFIG_FILE}" | sudo jq --arg DATA_ROOT "${DOCKER_DATA_ROOT}" '.["data-root"]=$DATA_ROOT' > "${tmp_config}"
sudo cat "${tmp_config}" > "${DOCKER_DAEMON_CONFIG_FILE}"

## finally, restart Docker daemon to pick up our changes
sudo systemctl restart --now docker

# Install Docker Compose
curl -L "${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
curl -L "${DOCKER_COMPOSE_VERSION}/contrib/completion/bash/docker-compose" -o /etc/bash_completion.d/docker-compose

# Run Sourcegraph. Restart the containers upon reboot.
docker-compose up -d
  • Under the Disks section, click Add new disk and add a disk (for storing Docker data) with the following settings:

    • Type: SSD Persistent Disk
    • Description: “Disk for storing Docker data for Sourcegraph” (or something similarly descriptive)
    • (optional, recommended) Snapshot schedule: The most straightfoward way of automatically backing Sourcegraph’s data is to set up a snapshot schedule for this disk. We strongly recommend that you take the time to do so here.
    • Mode: Read/write
    • Deletion rule: Keep disk
    • Size: 250 GB minimum (As a rule of thumb, Sourcegraph needs at least as much space as all your repositories combined take up. Allocating as much disk space as you can upfront helps you avoid resizing this disk later on.)
  • Create your VM, then navigate to its public IP address.

  • If you have configured a DNS entry for the IP, configure externalURL to reflect that.

  • You may have to wait a minute or two for the instance to finish initializing before Sourcegraph becomes accessible. You can monitor the status by SSHing into the instance and running the following diagnostic commands:

# Follow the status of the user data script you provided earlier
tail -c +0 -f /var/log/syslog | grep startup-script

# (Once the user data script completes) monitor the health of the "sourcegraph-frontend" container
docker ps --filter="name=sourcegraph-frontend-0"

Update your Sourcegraph version

Refer to the Docker Compose upgrade docs.

Storage and Backups

The Sourcegraph Docker Compose definition uses Docker volumes to store its data. The script above configures Docker to store all Docker data on the additional persistent disk that was attached to the instance (mounted at /mnt/docker-data - the volumes themselves are stored under /mnt/docker-data/volumes) There are a few different ways to backup this data: