Skip to content

Instantly share code, notes, and snippets.

@lightrush
Last active July 24, 2023 11:53
Show Gist options
  • Save lightrush/c08b5e7749ba9ca20c3c8bdc5a66acef to your computer and use it in GitHub Desktop.
Save lightrush/c08b5e7749ba9ca20c3c8bdc5a66acef to your computer and use it in GitHub Desktop.
Setup Plex with Docker

Setup Plex with Docker

Docker run script and Systemd unit files for setting up Plex using the official image by Plex Inc

Fill in the blanks in plex-docker-run.sh, plex-docker.service, optionally mount-remote-volume.service and save them. Then:

sudo [Path to]/plex-docker-run.sh
sudo cp [Path to]/plex-docker.service /etc/systemd/system/
sudo cp [Path to]/network-internet.service /etc/systemd/system/
# Optionally
# sudo cp [Path to]/mount-remote-volume.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable network-internet.service
sudo systemctl enable plex-docker.service
# Optionally
# sudo systemctl enable mount-remote-volume.service
# This Systemd unit waits indefinitely for a remote volume to mount
## It's useful for mounting remote volumes and starting other services after mounting succeeds
[Unit]
Description=Mount remote volume
After=network-online.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'while ! [Your mount command here. E.g. mount -t cifs //someserver/someshare /media/someshare] &> /dev/null ; do echo Cannot mount share. Retrying... ; sleep 10 ; done'
RemainAfterExit=yes
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
# This Systemd unit waits indefinitely for internet connection
## It's useful for starting other services after internet connection is established.
## Internet connection being established means being able to reach 8.8.8.8 and
## being able to resolve google.com using 8.8.8.8 as DNS.
[Unit]
Description=Wait for internet connection
After=network-online.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'while ! nslookup google.com 8.8.8.8 &> /dev/null ; do echo No internet connection. Waiting... ; sleep 10 ; done'
RemainAfterExit=yes
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
#!/bin/bash
docker run \
-d \
--name plex \
--net=host \
-e TZ="US/Eastern" \
`# Go to https://www.plex.tv/claim/ and copy a token from there.` \
`# Do this last since you have 4 minutes to use each token and container creation can take time.` \
-e PLEX_CLAIM="[Plex claim token]" \
-e PLEX_GID="[The GID that can access your media]" \
-e PLEX_UID="[The UID that can access your media]" \
-v "[Path to your Plex database on host machine. This is specified so that your Plex database survives container recreation. E.g. /opt/plex/config]:/config" \
`# Make sure the locations below are mounted before the container starts. Putting them in /etc/fstab works well.` \
`# There's a mount dependency in the Systemd unit file where you should probably add them.` \
`# Repeat for all the media locations you have.` \
-v "[Path to your media on host machine. E.g. /media/somedisk/music]:[Path to your media in container. E.g. /media/somedisk/music]" \
`# This is the image used for your container.` \
`# If you don't have plexpass, you should change the image tag from ":plexpass" to ":latest".` \
plexinc/pms-docker:plexpass
[Unit]
Description=Plex
Requires=docker.service
After=network-internet.service docker.service
# Put any local media locations below. If you put remote media volumes that may fail to mount before this service
# starts, those will become a failed dependency for the service and Plex won't start. Therefore remote
# media volumes are better mounted with oneshot services like mount-remote-volume.service provided in this gist.
# If you use one or more mount-remote-volume.service units, you should add them to the After directive above.
# RequiresMountsFor=[Path to your media on host machine e.g. /media/somedisk/music] [Path to your media on host machine e.g. /media/someotherdisk/music]
[Service]
Restart=always
ExecStart=/usr/bin/docker start -a plex
ExecStop=/usr/bin/docker stop -t 2 plex
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment