librespot: headless Spotify Connect client in docker

Since I am also using my small homeserver as a Media Player for my TV with Kodi I always wanted it be able to act as an Spotify Connect client. This way I could just use any device (smartphone, notebook, browser,...) to control my music. The server is running 24h a day either way.

My first try was to get ahold of a Spotify Media Source plugin for Kodi. Since Kodi is always running, this would have been the easiest solution. The only plugin which I found was kind of working (playing music) was the following:

https://github.com/kodi-community-addons/plugin.audio.spotify

But a day after using it I found quite a lot of flaws with this. Spotify Connect wouldn't reliably start the choosen song, and mostt imes was feeling slow. Also the plugin most of the time did not automatically jump to the next song in the playlist/album. After researching I found that the last commit was done in the beginning of 2020 and there were a lot of open issues still unresolved. The plugin is currently only barely maintaned.

Help needed with maintaining !

Enter librespot! (https://github.com/librespot-org/librespot)

librespot is an open source client library for Spotify. It enables applications to use Spotify's service to control and play music via various backends, and to act as a Spotify Connect receiver.

Librespot sounded quite perfect for my simple usecase: an always on headless Spotify Connect client.

But since I dont want to clutter my base system with running service and dependiencies I am trying to run all my services inside their own docker containers. Luckily while researching what would be needed to create a container with a running librespot instance I found the following issue.

https://github.com/librespot-org/librespot/issues/292

Github User dubo-dubon-duponey did already create a ready container and published it to github and Docker Hub. For me it left nothing to be desired!

Setup was quite easy and I did not have any interruptions what so ever since using it. It only uses around 15Mb of ram and has no real impact on CPU usage. Also it is quite secure, since it is completely read_only and does not use root processes. Also all capabilities are dropped!

Precondition is the availability of ALSA on the host:

sudo apt-get install alsa-base alsa-utils

The standard config includes --bitrate 320kbps and --device-type speaker which should be finde in most cases. Network discovery was also no problem, since it uses network_mode: host.

This is the docker compose file I am using for my setup. NAME= specifies the Spotify Connect device name. Happy listening!

version: "2"
services:
  librespot:
    image: dubodubonduponey/librespot:latest
    container_name: librespot
    restart: unless-stopped
    read_only: true
    cap_drop: 
      - ALL
    network_mode: host
    volumes:
      - /tmp:/tmp
    environment:
      - NAME=Server
    devices:
      - "/dev/snd:/dev/snd"
    group_add:
      - audio
      

Edit: When starting some media playback in kodi while this container is running, kodi will get priority until you pause/play Spotify. So pulseaudio is always just playing the last active source, which is quite convienient I think.