Last active 1752485886

A script that generates docker-compose.yml and Dockerfile to run Mopidy inside of a container

Revision 894525d9eaa0624ab0b95fcc81ab92e4c4b95986

mopidy-generate-docker.sh Raw
1#!/bin/bash
2
3# This script generates a Dockerfile and a docker-compose.yml file for running
4# Mopidy with various extensions.
5#
6# This is customized for my setup, so you may need to adjust it to fit the
7# extensions that you actually need.
8#
9# You may also want to use a Pulse sink for audio output if you already have
10# PulseAudio running on the host system, otherwise ALSA won't to
11# control the audio output.
12#
13# Also note that the docker-compose.yml will mount the configuration and work
14# directories from the host system ($HOME/.config/mopidy and
15# $HOME/.local/share/mopidy).
16
17# Content of the companion start.sh script:
18#!/usr/bin/dumb-init /bin/sh
19# set -x
20# mopidy --config /mopidy/mopidy.conf
21
22cat <<EOF > Dockerfile
23FROM debian:12-slim
24LABEL Author="Fabio Manganiello"
25
26# Change the architecture if needed, e.g. arm64, armhf, etc.
27ARG ARCH=amd64
28
29ENV DEBIAN_FRONTEND=noninteractive
30ENV PIP_NO_CACHE_DIR=1
31
32RUN apt update && apt upgrade -y \\
33 && apt install -y \\
34 alsa-utils \\
35 cmake \\
36 curl \\
37 dumb-init \\
38 gstreamer1.0-alsa \\
39 gstreamer1.0-pulseaudio \\
40 libcairo2-dev \\
41 mopidy \\
42 pkg-config \\
43 pulseaudio-utils \\
44 python3-pip \\
45 sudo \\
46 && mkdir -p ~/.config/pip && printf "[global]\nbreak-system-packages = true\n" | tee ~/.config/pip/pip.conf \\
47 && pip install --no-cache-dir \\
48 tidalapi==0.8.3 \\
49 mopidy-bandcamp \\
50 mopidy-funkwhale \\
51 mopidy-iris \\
52 mopidy-jellyfin \\
53 mopidy-local \\
54 mopidy-mobile \\
55 mopidy-mpd \\
56 mopidy-podcast \\
57 mopidy-scrobbler \\
58 mopidy-soundcloud \\
59 mopidy-tidal \\
60 mopidy-tunein \\
61 mopidy-youtube \\
62 yt-dlp \\
63 && apt purge --auto-remove -y \\
64 cmake \\
65 gcc \\
66 libcairo2-dev \\
67 pkg-config \\
68 && apt clean \\
69 && apt autoremove -y \\
70 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ~/.cache /root/.cache
71
72# Needed to map the audio group to the host audio group,
73# and that the mopidy user is part of it, and that the user has the same UID as the host user.
74RUN usermod -u $(id -u) mopidy
75RUN groupmod -g $(grep audio /etc/group | sed -r -e 's/^([^:]+:){2}([0-9]+):.*/\2/') audio
76RUN usermod -aG audio mopidy
77RUN sh -c 'echo "mopidy ALL=NOPASSWD: /usr/local/lib/python3.11/dist-packages/mopidy_iris/system.sh" >> /etc/sudoers'
78
79USER mopidy
80
81EXPOSE 6600
82EXPOSE 6680
83EXPOSE 5555/udp
84
85ADD ./start.sh /start.sh
86
87ENTRYPOINT ["dumb-init", "--"]
88CMD ["/start.sh"]
89
90HEALTHCHECK --interval=60s --timeout=5s --retries=3 \\
91 CMD curl --connect-timeout 5 --silent --show-error --fail http://localhost:6680/ || exit 1
92EOF
93
94cat <<EOF > docker-compose.yml
95services:
96 mopidy:
97 build:
98 context: .
99 dockerfile: Dockerfile
100 container_name: mopidy
101 ports:
102 - "6600:6600" # MPD port
103 - "6680:6680" # HTTP port
104 - "5555:5555" # Streaming UDP port
105 devices:
106 - /dev/snd:/dev/snd # Audio device
107 hostname: mopidy
108 environment:
109 PULSE_SERVER: unix:/run/user/$(id -u)/pulse/native # Adjust user ID as needed
110 volumes:
111 - /run/user/$(id -u)/pulse:/run/user/$(id -u)/pulse # Adjust user ID as needed
112 - /run/user/$(id -u)/pulse/native:/run/user/$(id -u)/pulse/native # Adjust user ID as needed
113 # - /mnt/hd/media/music:/mnt/hd/media/music # Music library path
114 - $HOME/.config/mopidy/mopidy.conf:/mopidy/mopidy.conf # Configuration file
115 - $HOME/.local/share/mopidy:/var/lib/mopidy/.local/share/mopidy # Local data storage
116 - ./cache:/var/lib/mopidy/.cache/mopidy
117 restart: unless-stopped
118EOF
119