Sign inSign up

giof71/librespot

By giof71

Updated 6 months ago

Run librespot on docker

Image
2

50K+

giof71/librespot repository overview

librespot-docker

A Docker image for librespot

News

The newest latest image is based on the current v0.8.0 version of librespot at this commit, is built using my fork of librespot.
The purpose of that fork is just to set a version for this repository.

Reference

First and foremost, the reference to the awesome project:

Librespot

For a long time, I have also been relying on Raspotify because I could not build images using cargo build for the armhf platfrom. So here is the reference to this other excellent project:

Raspotify

Since the first days of 2025, we are successfully building the images directly from source code.

Support

ko-fi
Please see the Goal.
Please note that support goal is limited to cover running costs for subscriptions to music services.

REPOSITORY TYPELINK
Git RepositoryGitHub
Docker ImagesDockerHub

Why

I prepared this Dockerfile Because I wanted to be able to install librespot easily on any machine (provided the architecture is amd64, armhf or arm64). Also I wanted to be able to configure and govern the parameter easily, maybe through a webapp like Portainer.

Prerequisites

First, you need a Spotify premium account in order to be able to use any version of Librespot.
You need to have Docker up and running on a Linux machine, and the current user must be allowed to run containers (this usually means that the current user belongs to the "docker" group).

You can verify whether your user belongs to the "docker" group with the following command:

getent group | grep docker

This command will output one line if the current user does belong to the "docker" group, otherwise there will be no output.

The Dockerfile and the included scripts have been tested on the following distros:

As I test the Dockerfile on more platforms, I will update this list.

Get the image

Here is the repository on DockerHub.

Getting the image from DockerHub is as simple as typing:

docker pull giof71/librespot:latest

Librespot is gapless

Mr. John Darko would be proud.

Configuration

Environment variables

The following tables reports all the currently supported environment variables.

VARIABLEDEFAULTNOTES
SPOTIFY_USERNAMEYour Spotify username. Required only if you want to disable discovery (DEPRECATED).
SPOTIFY_PASSWORDYour Spotify password. Required only if you want to disable discovery (DEPRECATED).
BITRATE160Bitrate (kbps): 96, 160, 320. Defaults to 160.
BACKENDalsaAudio backend to use. Use ? to list options. Currently possible values are alsa, pulseaudio and pipe.
INITIAL_VOLUMEInitial volume in % from 0-100. Default for softvol: 50. For the alsa mixer: the current volume.
DEVICE_NAMEDevice name (spaces allowed).
DEVICE_TYPEspeakerDisplayed device type: computer, tablet, smartphone, speaker, tv, avr (Audio/Video Receiver), stb (Set-Top Box), audiodongle, gameconsole, castaudio, castvideo, automobile, smartwatch, chromebook, carthing. Defaults to speaker.
DEVICEAudio device to use. Use ? to list options if using alsa, portaudio or rodio. Enter the path to the output when using pipe. Defaults to the backend's default.
FORMATS16Output format: F64, F32, S32, S24, S24_3, S16. Defaults to S16.
ENABLE_CACHEY or y to enable, uses corresponding volume.
ENABLE_SYSTEM_CACHEY or y to enable (recommended), uses corresponding volume (also recommended to use).
CACHE_SIZE_LIMITLimits the size of the cache for audio files. It's possible to use suffixes like K, M or G.
DISABLE_AUDIO_CACHEY or y to disable.
DISABLE_CREDENTIAL_CACHEY or y to disable.
MIXERsoftvolMixer to use: softvol, alsa. Defaults to softvol.
ALSA_MIXER_CONTROLPCMalsa mixer control, e.g. PCM, Master or similar. Defaults to PCM.
ALSA_MIXER_DEVICEalsa mixer device, e.g hw:0 or similar from aplay -l. Defaults to --device if specified, default otherwise.
ALSA_MIXER_INDEX0alsa mixer index, Index of the cards mixer. Defaults to 0.
QUIETOnly log warning and error messages. Y or y to enable
VERBOSEEnable verbose output. Y or y to enable.
PROXYUse a proxy for HTTP requests. Proxy should be an HTTP proxy in the form http://ip:port, and can also be passed using the all-lowercase http_proxy environment variable.
AP_PORTConnect to an AP with a specified port. If no AP with that port is present a fallback AP will be used. Available ports are usually 80, 443 and 4070.
DISABLE_DISCOVERYDisable zeroconf discovery mode. Y or y to disable discovery.
DITHERDither algorithm: none, gpdf, tpdf, tpdf_hp. Defaults to tpdf for formats S16, S24, S24_3 and none for other formats.
ZEROCONF_PORTThe port the internal server advertises over zeroconf: 1 - 65535. Ports <= 1024 may require root privileges.
ZEROCONF_INTERFACEComma-separated interface IP addresses on which zeroconf will bind (Example "192.168.0.10,10.0.0.10"). Defaults to all interfaces. Ignored by DNS-SD.
ZEROCONF_BACKENDSelect the desidered backend, valid values are avahi, libmdns, dns-sd. With the latest builds, I am getting good results with libmdns, so it will be the default if discovery is not disabled
ENABLE_VOLUME_NORMALISATIONEnables volume normalisation for librespot. Y or y to enable.
NORMALISATION_METHODSpecify the normalisation method to use: basic, dynamic. Defaults to dynamic.
NORMALISATION_GAIN_TYPESpecify the normalisation gain type to use: track, album, auto. Defaults to auto.
NORMALISATION_PREGAINPregain (dB) applied by the normalisation. Defaults to 0.
NORMALISATION_THRESHOLDThreshold (dBFS) to prevent clipping. Defaults to -2.0.
NORMALISATION_ATTACKAttack time (ms) in which the dynamic limiter is reducing gain. Defaults to 5.
NORMALISATION_RELEASERelease or decay time (ms) in which the dynamic limiter is restoring gain. Defaults to 100.
NORMALISATION_KNEEKnee steepness of the dynamic limiter. Default is 1.0.
VOLUME_CTRLVolume control type cubic, fixed, linear, log. Defaults to log.
VOLUME_RANGERange of the volume control (dB). Default for softvol: 60. For the alsa mixer: what the control supports.
AUTOPLAYAutoplay similar songs when your music ends. Y or y to enable.
DISABLE_GAPLESSDisables gapless playback by forcing the sink to close between tracks. Y or y to disable gapless mode.
PASSTHROUGHPass a raw stream to the output. works with the pipe and subprocess backends. Y or y to enable.
PUIDSet this value the the user which should run the application, defaults to 1000 if not set when using the pulseaudio backend
PGIDSet this value the the user which should run the application, defaults to 1000 if not set when using the pulseaudio backend
AUDIO_GIDSpecifies the gid for the group audio, it is required if you want to use, e.g., the alsa backend in user mode. Refer to this page from my squeezelite-docker repository for more details.
PARAMETER_PRIORITYWhere to look for a parameter first: env or file. For example, the credentials.txt file compared to SPOTIFY_USERNAME and SPOTIFY_PASSWORD environment variables. Defaults to file, meaning that each file is considered if it exists and if it contains the required values.
ONEVENT_COMMANDSpecifies the name of a user defined script/executable that will be executed whenever a player event occurs. User defined scripts must be mounted to the /userscripts/ folder and be made executable via chmod u+x. Internally maps to the --onevent flag of librespot. More info about usage can be found in librespot's player event handler.
ONEVENT_POST_ENDPOINTSend a POST request with event data to the specified endpoint URL whenever a player event occurs. Request body is json encoded and contains all available fields specified by the librespot's player event handler. Will be ignored if ONEVENT_COMMAND is set.
ENABLE_OAUTHSet to headless to enable OAUTH authentication. You will need to run the container interactively the first time. Recommended to enable when caching is also enabled, otherwise the credentials file will be lost when the container is recreated.
LOG_COMMAND_LINESet to Y or y to enable, N or n to disable. Defaults to Y.
ADDITIONAL_ARGUMENTSUse this to add additional arguments to be appended to the command line.
Librespot arguments can be found here.
Pipe Mode

When using BACKEND=pipe, specify a device (using variable DEVICE) that is mounted to a fifo file. Example:

services:
  librespot:
    image: giof71/librespot:latest
    network_mode: host
    environment:
      - BACKEND=pipe
      - DEVICE=/mnt/pipe/spotipipe
      - BITRATE=320
      - INITIAL_VOLUME=100
      - DEVICE_NAME=SpotiPi
    volumes:
      - /path/to/folder/for/fifo-file:/mnt/pipe

Thank you @marco79cgn for your contributions on issue #111.

Volumes
VolumeDescription
/data/cacheVolume for cache, used by --cache (ENABLE_CACHE)
/data/system-cacheVolume for system-cache (recommended), used by --system-cache (ENABLE_SYSTEM_CACHE).
/user/configVolume for user-provided configuration. Might contain a credentials.txt file.

Note that the volume /data/system-cache will contain the encrypted credentials. Enabling the system cache and using a dedicated volume will help keeping players discoverable by the Spotify web app when you don't provide credentials to LibreSpot.

Examples
Docker-compose

Using docker-compose is preferable for multiple reason, a notable one is the fact that it avoids a few headaches with password escaping, in case of special characters.

Among the docker-compose.yaml files hereby presented, those which use credentials require a .env file at the same level of the docker-compose.yaml file itself. The .env file should have the following format:

SPOTIFY_USERNAME=myusername
SPOTIFY_PASSWORD=mypassword

Note that username and password is deprecated as an authentication method in librespot.

Docker-compose in Alsa mode

Discovery mode:

---
version: "3"

services:
  librespot:
    image: giof71/librespot:latest
    network_mode: host
    devices:
      - /dev/snd:/dev/snd
    environment:
      - DEVICE=hw:x20,0
      - BACKEND=alsa
      - BITRATE=320
      - INITIAL_VOLUME=100
      - DEVICE_NAME=gustard-u12
Docker-compose in PulseAudio mode

Discovery mode:

---
version: "3"

services:
  librespot:
    image: giof71/librespot:latest
    network_mode: host
    environment:
      - BACKEND=pulseaudio
      - BITRATE=320
      - INITIAL_VOLUME=100
      - DEVICE_NAME=manjaro-xeon10-pulse
    volumes:
      - /run/user/1000/pulse:/run/user/1000/pulse
Docker run
Docker run in Alsa mode

Discovery mode:

docker run -d --name librespot \
    --device /dev/snd \
    --network host \
    -e DEVICE_NAME=kodi-living-pi4-tuner \
    -e INITIAL_VOLUME=100 \
    -e BACKEND=alsa \
    -e DEVICE=hw:D10,0 \
    -e FORMAT=S32 \
    -e BITRATE=320 \
    -e INITIAL_VOLUME=100 \
    --restart unless-stopped \
    giof71/librespot:latest

Discovery mode, using docker --user:

See this page for a sample configuration using a specified user (uid) in a docker-compose file.

Note that with this DAC I had to specify S32 as the format. It would not work with the default (which is S32 for librespot).

Docker run in PulseAudio mode

Discovery mode:

docker run -d
    -e PUID=1000 \
    -e PGID=1000 \
    --network host \
    -e BACKEND=pulseaudio \
    -e BITRATE=320 \
    -e DEVICE_NAME=librespot-pulse \
    -v /run/user/1000/pulse:/run/user/1000/pulse \
    --name librespot-pulse \
    giof71/librespot:latest
Run as a user-level systemd

When using a desktop system with PulseAudio, running a docker-compose with a restart=unless-stopped is likely to cause issues to the entire PulseAudio. At least that is what is systematically happening to me on my desktop systems.
You might want to create a user-level systemd unit. In order to do that, move to the pulse directory of this repo, create a valid envfile.txt with your credentials using envfile-sample.txt as a template, then run the following to install the service:

./install.sh

After that, the service can be controlled using ./start.sh, ./stop.sh, ./restart.sh.
You can completely uninstall the service by running:

./uninstall.sh`

Of course, you might simply want run the Spotify binary client or the web player instead of this service, but this alternative will allow you to control the player on your desktop system from e.g. a smartphone or any Spotify client. And it will consume significantly less resources.

Running interactively

If you have set ENABLE_OAUTH to headless, you will need to run your docker-compose.yaml interactively the first time.
In order to do that, run your compose file using the following:

docker-compose run librespot

assuming that librespot is the name of the service. Tune the command if needed.
This command will let you see the container logs. You will have to open your browser at the displayed link, authenticated with Spotify and authorize the device, then paste the redirect URL to the terminal.
After the first start, you can start the container as usual using docker-compose up -d.

Credentials file (DEPRECATED)

Credentials can be stored on a separate file and mounted as /user/config/credentials.txt. The format is the same as the standard .env file.
By defaults, SPOTIFY_USERNAME and SPOTIFY_PASSWORD entries found in this file have the priority against the correspondent environment variables, unless you set the variable PARAMETER_PRIORITY to env.

Known issues

Discovery

For discovery mode to work, you will need to specify network_mode=host on the compose file. Otherwise the player will not be discoverable.
In this mode, authentication is not required on the container itself, but OTOH any premium spotify user on your network will be able to use your Librespot Player.
Note that even when using the "discovery" mode, the premium account is always required for playback.

Build

You can build (or rebuild) the image by opening a terminal from the root of the repository and issuing the following command:

docker build . -t giof71/librespot

Now that we are building from code, please note that the build will take quite a lot of time. When it's finished, you can run the container following the previous instructions.
Just be careful to use the tag you have built.

Change History

Change DateMajor Changes
2026-03-14Fix small issue when displaying ZEROCONF_INTERFACE (see comment)
2025-11-10Build with upstream version 0.8.0 (see #154)
2025-11-08Build with upstream PR #1622 merged to dev (see #152)
2025-11-02Add python3 package (see #149)
2025-08-31Build using latest version (v0.7.1) (see #146)
2025-08-31Build using latest version (v0.7.0) (see #144)
2025-08-12Build using current dev branch at this commit, using my fork
2025-03-27Fix autoplay (see #122)
2025-01-27Build latest tag v0.6.0 instead of default branch
2025-01-26Added curl to the runtime dependencies (see #113)
2025-01-03Restored arm/v7 build
2025-01-03Build using cargo (see #103)
2024-12-29First release including Raspotify 0.4.6 (see #101)
2024-11-22Add support for OAUTH authentication (see #96)
2024-11-17Fix docker warning (see #94)
2024-11-16Add support for -onevent (see #91), thanks to @QuadratClown
2024-09-21Use exec instead of eval
2024-09-05Handle non-writable volumes more gracefully
2024-09-05Fix user and group management
2024-03-07Fix switch for normalisation pregain (see #81)
2023-12-20Support docker --user mode (see #77)
2023-10-06Change ownership of volumes (see #75)
2023-09-05Clean Dockerfile (see #73)
2023-06-23Pass device name in quotes (see #67)
2023-06-23Daily builds update latest images
2023-06-23Add support for bookworm
2023-05-13Routine rebuild
2022-10-28Credentials are not exposed with the command line output
2022-10-28Enabled reading credentials from file
2022-10-28Allowed configurability over command line being logged
2022-10-27Updated github action versions
2022-10-20Quotes on a few environment variables
2022-10-08PulseAudio user-level systemd service introduced
2022-10-04Feature complete (2022-10-04.1)
2022-10-04Documentation enrichment and cleanup
2022-10-04Support for cache and system-cache
2022-10-04Initial Release (2022-10-04)

Tag summary

Content type

Image

Digest

sha256:0c7cd19a4

Size

201.5 MB

Last updated

6 months ago

docker pull giof71/librespot