Install Sourcegraph with Docker Compose on AWS

This tutorial shows you how to deploy Sourcegraph via Docker Compose to a single EC2 instance on AWS.

Deploy to EC2

  • Click Launch Instance from your EC2 dashboard.
  • Select the Amazon Linux 2 AMI (HVM), SSD Volume Type.
  • Select an appropriate instance size (use the resource estimator to find a good starting point for your deployment), then Next: Configure Instance Details.
  • Ensure the Auto-assign Public IP option is "Enable". This ensures your instance is accessible to the Internet.
  • Place the following script in the User Data text box at the bottom of the Configure Instance Details page

Screen Shot 2021-12-28 at 1 05 07 PM

#!/usr/bin/env bash

set -euxo pipefail



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

# Install git
yum update -y
yum install git -y

# Clone Docker Compose definition

# Format (if necessary) and mount EBS volume
device_fs=$(lsblk "${EBS_VOLUME_DEVICE_NAME}" --noheadings --output fsType)
if [ "${device_fs}" == "" ] ## only format the volume if it isn't already formatted
  mkfs -t xfs "${EBS_VOLUME_DEVICE_NAME}"
mkdir -p "${DOCKER_DATA_ROOT}"

# Mount EBS volume on reboots
EBS_UUID=$(blkid -s UUID -o value "${EBS_VOLUME_DEVICE_NAME}")
echo "UUID=${EBS_UUID}  ${DOCKER_DATA_ROOT}  xfs  defaults,nofail  0  2" >> '/etc/fstab'
umount "${DOCKER_DATA_ROOT}"
mount -a

# Install, configure, and enable Docker
yum update -y
amazon-linux-extras install docker
systemctl enable --now docker
sed -i -e 's/1024/262144/g' /etc/sysconfig/docker
sed -i -e 's/4096/262144/g' /etc/sysconfig/docker
usermod -a -G docker ec2-user

# Install jq for scripting
yum 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
cat "${DOCKER_DAEMON_CONFIG_FILE}" | jq --arg DATA_ROOT "${DOCKER_DATA_ROOT}" '.["data-root"]=$DATA_ROOT' > "${tmp_config}"
cat "${tmp_config}" > "${DOCKER_DAEMON_CONFIG_FILE}"

## finally, restart Docker daemon to pick up our changes
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
  • Select Next: Add Storage

  • Click "Add New Volume" and add an additional volume (for storing Docker data) with the following settings:

    • Volume Type (left-most column): EBS
    • IMPORTANT: Device: /dev/sdb
    • Size (GiB): 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 your volume later on.)
    • Volume Type: General Purpose SSD (gp2)
    • Delete on Termination: Leave this setting unchecked
  • Select Next: ... until you get to the Configure Security Group page. Then add the following rules:

ℹ️ Please note that while the above will work, this provides open access of the ports specified. If possible, replace the IP address ranges specified with the IPs from which you actually want to allow access.

  • Launch your instance, then navigate to its public IP in your browser. (This can be found by navigating to the instance page on EC2 and looking in the "Description" panel for the "IPv4 Public IP" value.) 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 using the following diagnostic commands:
# Follow the status of the user data script you provided earlier
tail -f /var/log/cloud-init-output.log

# (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

To update to the most recent version of Sourcegraph (X.Y.Z), SSH into your instance:

cd /home/ec2-user/deploy-sourcegraph-docker/docker-compose

And refer to the Upgrade guide.

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 EBS volume 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:

  • (recommended) The most straightforward method to backup this data is to snapshot the entire /mnt/docker-data EBS disk on an automatic, scheduled basis.

  • Using an external Postgres instance (see below) lets a service such as AWS RDS for PostgreSQL take care of backing up all of Sourcegraph's user data for you. If the EC2 instance running Sourcegraph ever dies or is destroyed, creating a fresh instance that's connected to that external Postgres will leave Sourcegraph in the same state that it was before.