Yannis blog

Deploy Unifi Controller using docker-compose

17 Oct 2022

Here you can find detailed instructions on how to run a dockerized Unifi controller using docker-compose and create a systemd service for it.

Docker

Make sure to have Docker and Docker-compose installed. For Debian Linux you just need to install the following packages.

apt update && apt install -y docker-ce docker-compose

Docker application location

In order to run the container we need to create a location eg: /opt/containers/unifi and store our configuration in there.

mkdir -p /opt/containers/unifi

Configuration

In order to run the Unifi controller we need to specify the following environment variables

  • UNIFI_DATA_PATH is mandatory and specifies the path in the Host for the controller data
  • UNIFI_LOG_PATH is mandatory and specifies the path in the Host for the controller logs
  • UNIFI_IP is optional and is set when you want the controller to listen on a specific IP. In order for it to work you need to add a semicolon at the end. If left blank it will listen to all interfaces.

/opt/containers/unifi/.env defines all necessary environment variables

# MANDATORY - path in the host that will keep the unifi controller data
UNIFI_DATA_PATH=/opt/containers/unifi/data

# MANDATORY - path in the host that will keep the unifi controller logs
UNIFI_LOG_PATH=/opt/containers/unifi/log

# OPTIONAL - IP of the host that the controller will listen on
#UNIFI_IP=192.168.1.2:

Compose File

/opt/containers/unifi/docker-compose.yml defines the services, networks, and volumes for the Unifi Docker application

version: "3"
services:
  unifi:   
    restart: always   
    image: jacobalberty/unifi:latest
    ports:
      - "${UNIFI_IP}8080:8080"
      - "${UNIFI_IP}443:8443"
      - "${UNIFI_IP}3478:3478/udp"
    volumes:
      - ${UNIFI_DATA_PATH}:/unifi/data
      - ${UNIFI_LOG_PATH}:/unifi/log
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro

Systemd service definition

/etc/systemd/system/docker-compose-unifi.service defines the service that starts and stops the Docker application for the system

[Unit]
Description=Unifi container
Requires=docker.service
After=docker.service

[Service]
Type=simple
WorkingDirectory=/opt/containers/unifi
ExecStart=/usr/bin/docker-compose up --force-recreate --pull always --remove-orphans 
ExecStop=/usr/bin/docker-compose down

[Install]
WantedBy=default.target

Enable and run service

source  /opt/containers/unifi/.env && mkdir -p "${UNIFI_DATA_PATH}" "${UNIFI_LOG_PATH}"
systemctl daemon-reload && \
systemctl enable docker-compose-unifi && \
systemctl start docker-compose-unifi

Visit Unifi Controller UI

You should be able to visit your container at port 443 using the IP you specified in the .env file. eg: https://192.168.1.2