Building Nightscout with Docker


The most complete guide for building your site step by step is the one used for Oracle Cloud.
If you use another hosting provider, skip Oracle specific instructions and follow the flow.

Original documentation from Jason.
More from Andries and Peter (traefik as below), justmara and Ireneusz.

A comprehensive description of the yaml file for mongo and Nightscout setup here.

If you don’t own a domain name, get a DDNS name now: see here.

Update your system

Update your Ubuntu system (you should type this is a command every 3 or 6 months to keep it updated).

sudo apt update
sudo apt upgrade -y
sudo apt clean

You will see it’s completed when the terminal stops scrolling text and you’re returned to the prompt.

If you see the following message: *** System restart required *** you will need to reboot. Do not skip this step: reboot and wait a minute before opening again the console.

sudo reboot

You must probably already have nano installed. If necessary:

sudo apt install nano -y

Deploy a single instance

Step 1 - Install Docker

Note: instructions from the vendor

a) Install keys

sudo apt-get -y install ca-certificates curl gnupg lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg --yes

b) Install Docker

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli docker-compose-plugin

Step 2 - Configure Nightscout

a) If you’re running your VPS as root: Create a new user. Else go to d)
Don’t try to run Nightscout as root, it will not work.
Replace mainuser with a name of your choice, like your name, only lowercase letters.

sudo adduser mainuser

You need to invent a password for your new user.
Confirm it then just hit Enter for the following lines, when done key Y then Enter.

b) Make your user an administrator.

Replace mainuser with your new user name.

sudo usermod -aG sudo mainuser

c) Enable current user and make docker startup automatically.
Replace mainuser with your new user name.

sudo groupadd docker
sudo usermod -aG docker mainuser
sudo systemctl enable docker

d) Login to the VPS using the account we just created.

!!!warning “Do not run Nightscout as root”
From now on use your Nightscout user.

Replace mainuser with your new user name.

sudo -u mainuser -s

Log out (Ctrl-D or ⌘-D) or type logout, and log in with with your new user.

e) Customize your Docker Compose configuration file

You can use the default docker-compose.yml file from the Nightscout repository or download this one.

There are few information you need to update:

image: nightscout/cgm-remote-monitor:latest
  • Your Nightscout site API_SECRET password.


Note: now you’ve also seen where your Nightscout variables are defined.

  • You need to update your Nightscout site URL (your DDNS name looking like name.domain.tld)

- 'traefik.http.routers.nightscout.rule=Host(`YOUR_PUBLIC_HOST_URL`)'
  • You need to put a valid email too for important certificate emails (i.e. probably never)

- ''

Step 3 - Build and run Nightscout

To start your Nightscout:

sudo docker compose up -d

Or, if you want to start it in background (once everything is working as you won’t see the log):

nohup sudo docker compose up -d &

To stop it (if you didn’t select always restart for your containers):

sudo docker compose down

Note that you need to start it if you modify your docker-compose.yml configuration (to validate the changes).

Deploy multiple instances

Hints from justmara

Portainer configuration used in docker-compose:

    image: portainer/portainer-ee:latest
    container_name: portainer
    restart: unless-stopped
      - ./portainer-data:/data
      - /var/run/docker.sock:/var/run/docker.sock
      - 'traefik.enable=true'
      - 'traefik.http.routers.portainer.rule=Host(`portainer.<MYDOMAIN>`)'
      - 'traefik.http.routers.portainer.entrypoints=web'
      - 'traefik.http.routers.portainer.service=portainer-service'
      - ''
      - "8000:8000"
      - "9443:9443"
      - "9000:9000"
      - dia

Portainer custom template used to start new Nightscout instances:

version: '3.9'

x-ns-common-env: &ns-common-env
  NODE_ENV: production
  TZ: Europe/Moscow
  ENABLE: basal iob cob boluscalc cage sage iage bage pump openaps pushover bgi food rawbg
  SHOW_FORECAST: openaps
  PUMP_FIELDS: clock reservoir

    image: nightscout/cgm-remote-monitor:latest
    container_name: dia-ns-{{ns-name}}
    restart: always
      - mongo
      - 'traefik.enable=true'
      - 'traefik.http.routers.ns-{{ns-name}}.rule=Host(`{{ns-name}}.<MYDOMAIN>`)'
      - 'traefik.http.routers.ns-{{ns-name}}.entrypoints=web'
      <<: *ns-common-env
      MONGO_CONNECTION: mongodb://mongo:27017/ns-{{ns-name}}
      API_SECRET: {{ns-secret}}
      - dia

    external: true