setting up plex in a linux container

In case anybody out there is running a recent Debian (jessie or later), and has ever wondered how much of a hassle it would be to install Plex into a container to segregate it from the rest of your system: It turns out, it's not much of a hassle at all!

You can do something like I did here:

# as root
cd /var/lib/container/
mkdir plex
debootstrap --arch amd64 vivid plex

# Set the root password for later
systemd-nspawn --directory plex

# Now you should be at a root prompt inside the container!
# and you can enter a new password.

Now you hold Ctrl and hit ] three times, and it'll kill the container host process.

# assuming you already have a bridge set up for your NIC and it's named lxcbr0:
systemd-nspawn --network-bridge=lxcbr0 --boot --directory plex

That'll get you a fresh minimal Ubuntu Vivid installation, perfect for installing Plex. You can safely ignore the systemd-journald errors on the first boot; updating to the latest packages will fix the errors.

You'll need to bring up the network inside the VM, so log in as root using the password you just set, and run:

dhclient host0

Then add the vivid-updates repo, and upgrade to the latest packages to fix the journald errors from vivid's starting state:

echo 'deb vivid-updates main' > /etc/apt/sources.list.d/vivid-updates.list
apt-get update && apt-get dist-upgrade

Now you're up to date. Let's add the PlexPass repo and install Plex:

echo 'deb plexpass main' > /etc/apt/sources.list.d/plexmediaserver.list
apt-get update && apt-get install plexmediaserver

(If you don't have a Plex Pass, you can use the regular non-PlexPass repo, just substitute wheezy for plexpass in the above echo command. The wheezy name appears to be hilariously inaccurate, because it doesn't seem to even work on jessie anymore, but I digress.)

Now, hit Ctrl+]]] again, and stop the Plex server in the host, assuming it's on the same machine. You'll want to copy the Application Support folder from your existing Plex server (using these packages, the default location is in /var/lib/plexmediaserver/Library, but your OS may have it in a different place). Just make sure that you copy the Application Support folder into the /var/lib/container/plex/var/lib/plexmediaserver/Library on the host.

Look up the ID of the plex user inside the container. Mine didn't match between host and guest, so I ran:

grep plex plex/etc/passwd | sed -re 's;^plex:x:([0-9]+):([0-9]+):.*$;PLEXUID=\1 PLEXGID=\2;g'
chown -R {PLEXUID}:{PLEXGID} plex/var/lib/plexmediaserver

Now, you'll want to add some more arguments to your systemd-nspawn so that it can access your media library. My media is on ZFS in /tank/tv and /tank/movies so I used this command:

# assuming you're back in /var/lib/container on the host:
systemd-nspawn --network-bridge=lxcbr0 -bD plex --bind-ro=/tank/tv --bind-ro=/tank/movies

# once the container boots, log in as root again, and set up networking
# I did:
echo "auto host0\niface host0 inet dhcp" >> /etc/network/interfaces

# and then:
systemctl restart networking

Remember to add --bind-ro (or --bind, if you want Plex to be able to write to these files) for each separate filesystem you want visible inside the container.

And now you should have a working Plex running inside a container, on a separate IP address on your local network.

TL;DR Setting up Plex in a container doesn't take long at all, and separates Plex from the resources of the rest of your machine with almost no overhead.

PS: If you haven't already got a bridge for your main interface on the host machine, you can set one up like this, but be careful if you're doing it over SSH!

# as root on the host:
brctl create lxcbr0

# change eth0 to eth1 or em0 or whatever your NIC is called.
brctl addif lxcbr0 eth0 && dhclient lxcbr0

If you have a static IP for your NIC, then you should definitely use ifconfig or ip addr add to configure lxcbr0 in the second part above using the same configuration you previously had on your NIC. You'll want to update your host's /etc/network/interfaces if you're on Debian or Ubuntu on the host system. (Basically just s/eth0/lxcbr0/g on the whole file, and add a new line under the iface lxcbr0 inet <> line that looks like:

        bridge_ports eth0 # or whatever your NIC is