Running Spotify on Docker

Spotify in Docker Container

Many people don’t know that running a graphical application inside a Docker Container, such as Spotify, is possible. Maybe they never think about it or need to test one application without installing it or have more than one installed on the computer. Let’s learn how to run Spotify in Docker Container.

Why Run Spotify on Docker?

Sometimes, I don’t want to install several dependencies on my operation system. So, I love running applications with Docker, so I don’t need to install most of my applications even if I format my computer. And if I need to test a new application, it’s pretty easy.

How it works

However, if you have decided to run by ordinary docker command, then it won’t work. And the reason is that the Docker container is just CLI-based and requires a physical screen to run a GUI application. So for running a graphical application inside a container, we demand to share the base host’s display with the container.

And it is pretty straightforward to do; nevertheless, to understand that command, you first need to understand two things:

  1. X server or X11
  2. The DISPLAY Environment Variable
Running Graphical Application on Docker
Running Graphical Application on Docker

DISPLAY – Environment Variable

The DISPLAY Environment Variable is the most crucial environment variable for X Window System clients is DISPLAY. When you log in at an X terminal, the DISPLAY environment variable in each xterm window is configured to your X terminal’s hostname followed by: 0.0. 

An example of the value of the DISPLAY environment variable:

hostname: N.D.

where:

  • the hostname is the name of the computer where the X server runs. If this value is omitted, it means localhost.
  • N is a sequence number (typically 0). It can be different if there are many displays attached to one computer.
  • D is the screen number. A display can have many screens. Typically, there’s only one screen, though, where 0 is the default.

What Does X Server Mean?

The x server program connects X terminals running on the X Window System, whether locally or in a distributed network. The X server is installed with the X Window System, a cross-platform and complete client-server system for controlling graphical user interfaces on individual computers or networked ones. The X server handles the X clients and manages input and display devices, and performs requested operations. This simplifies programming, as the application programs do not need to be aware of the hardware details and rely entirely on the X server.

Presently that we understand both theories, it’s time to execute them. And we will do a fantastic project here. We will run a Spotify application inside the container. Isn’t it amazing?


Dockerfile for Spotify

To run Spotify in Docker Container, we need a Dockerfile to build an image first. So below is the Dockerfile to generate an image that you can copy, which is pretty easy to understand.

FROM ubuntu:18.04
LABEL maintainer "Cleber Rodrigues "
RUN apt-get update && apt-get install -y \
	dirmngr \
	gnupg \
	--no-install-recommends \
	&& apt-get update && apt-get install -y \
	apulse \
	libasound2 \
	libgl1-mesa-dri \
	libgl1-mesa-glx \
	libpulse0 \
	sudo curl ca-certificates \
	--no-install-recommends
RUN curl -sS https://download.spotify.com/debian/pubkey.gpg | sudo apt-key add - 
RUN echo "deb http://repository.spotify.com stable non-free" | sudo tee /etc/apt/sources.list.d/spotify.list
RUN sudo apt-get update && sudo apt-get -y install spotify-client && rm -rf /var/lib/apt/lists/*
ENV LANG en-US
COPY local.conf /etc/fonts/local.conf
COPY entrypoint.sh /usr/bin/startspotify
ENTRYPOINT [ "startspotify" ]

If you don’t want to build your image, you can use my image available on the Docker Hub.

docker pull bitslovers/spotify:v1

Run Spotify in Docker using x11docker

x11docker allows running graphical desktop applications (and whole desktops) in Linux containers.

You may need to deploy desktop applications with Docker, but it does not typically have a display server for running desktop applications. Also, the X11 Docker allows running desktop applications and even entire desktops in Docker Linux containers. It runs an X display server on the host system and provides it to Docker containers. It additionally does some security setup, which enhances container isolation and avoids X security leaks.

To run Spotify in Docker Container, it’s not required to use the x11docker. However, x11docker makes this process easier.

Installing x11docker on Linux

x11docker requires bashDocker, and an X server, possibly already installed in your system. This usually is Xorg or Xwayland. You can install a supported base for your distribution, which is intended to give you excellent usage of x11docker.

Note: x11docker works on Linux and (with some setup and restrictions) on M.S. Windows. x11docker does not run on macOS saving in Linux using a Virtual Machine.

Installing x11docker on Linux

Install x11docker dependencies on Debian|Linux Mint|Ubuntu:

sudo apt-get update
sudo apt-get -y install xpra xserver-xephyr xinit xauth xclip x11-xserver-utils x11-utils

# Using sudo

curl -fsSL https://raw.githubusercontent.com/mviereck/x11docker/master/x11docker | sudo bash -s -- --update

or

# Using root

curl -fsSL https://raw.githubusercontent.com/mviereck/x11docker/master/x11docker | bash -s -- --update

Running Spotify in Docker Container


sudo x11docker --home=/data/spotify --pulseaudio --gpu -- --hostname=spotify spotify:v1

where

  • –home, it’s the location where I decide to store all data for Spotify, like a cache. For example, if I download music, all files are saved on the /data/spotify. So, when you execute Spotify on Docker again, you will keep your data and not require logging in again.
  • –pulseaudio. PulseAudio is a general-purpose sound server designed to run as middleware within your applications and hardware devices, either ALSA or OSS. So, it enables the Audio to run Spotify on Docker.
  • –gpu. Enable Hardware acceleration for OpenGL to run Spotify on Docker

So finally, when you run the above command, you will launch your container and run the Spotify application.

Run Spotify Quickly from Terminal

To be faster to open Spotify on Docker, I have created Alias on my ~/.bashrc, like this one:

dspotify='sudo x11docker --home=/data/spotify --pulseaudio --gpu -- --hostname=spotify spotify:v1'

Now relax, have a cup of coffee, and appreciate your favorite music.

Conclusion and Goal

It’s not just about Spotify. The great thing about this approach is that it forces us to understand important features of Linux or Unix and how the operating system works behind the scene.

During this process, I have learned a lot about Docker too. After this article, you will notice that it’s possible to run any application inside Docker, with no limitations.

This technic is very useful for Web Developers to run different web browsers, for example, Google Chrome inside Docker. For example, something needs to test our application on a different browser, so with Docker, it’s easy to switch between different versions of Google Chrome without installing it on your computer.

I hope that you enjoy this article and that you have learned a lot. Until next time bitslovers!

Please, follow us on social media!

Visit our other posts:

Maven and Node.js

What is AWS KMS?

7 thoughts on “Spotify in Docker Container”

  1. Loryan Strant

    Thanks for sharing this, but do you have any screenshots of what the Spotify UI looks like from the user perspective?

  2. Step 8/10 : COPY local.conf /etc/fonts/local.conf
    COPY failed: file not found in build context or excluded by .dockerignore: stat local.conf: file does not exist

  3. How heavy is this in terms of CPU and memory usage? Will it be much more than installing Spotify desktop directly in windows?

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.