Skip to content

Run XeFM in Docker

craftware edited this page Nov 2, 2026 · 5 revisions

The terminal app runs in a Docker container like in any terminal. This recipe builds an image with XeFM and the programs some of its features call (ffmpeg and fpcalc for Find Similar Files on videos and audio, libarchive for .7z, .rar and .iso), then starts XeFM in it with the folders you name mounted. Use it to try XeFM without installing Python, or to keep those tools off the host.

Only the folders you mount are visible inside the container; the rest of the host's files are not.

Running it

Download the script and run it, with the folders to open:

curl -fsSLO https://raw.githubusercontent.com/wiki/crftwr/xefm/xefm-docker.sh && chmod +x xefm-docker.sh && ./xefm-docker.sh ~/Videos ~/Music

After that, ./xefm-docker.sh starts it again; put it in a folder on your PATH to run it from anywhere. The script is downloaded rather than piped into sh because XeFM needs the terminal's keyboard on its input, which a pipe would take.

The script

This is what xefm-docker.sh holds. Read it before you run it.

#!/bin/sh
# Run XeFM in Docker with the given folders mounted.
# Usage: xefm-docker.sh [--update | --rebuild] [DIR[:PATH] ...]
# DIR is mounted at PATH in the container, or at its own path without one.
# The first DIR opens in the left pane, the second in the right.
# --update installs the latest XeFM in the image before starting.
# --rebuild builds the whole image again, system packages included.
set -e

IMAGE=xefm

# Folders mounted on every run, one HOST:CONTAINER per line. A missing
# HOST folder is created. The container's home folder is /home/xefm.
ALWAYS_MOUNT="
  $HOME/.xefm-docker:/home/xefm/.xefm
  $HOME/.cache/similar-files:/home/xefm/.cache/similar-files
"

build=
case $1 in
  --update)  build="--build-arg XEFM_BUILD=$(date +%s)"; shift ;;
  --rebuild) build="--pull --no-cache"; shift ;;
esac

# Mount each folder at the path given after its colon, or at the same path
# it has on the host. With none given, nothing of the host is mounted and
# XeFM opens in the container's home.
mounts= left= right= panes=
for arg in "$@"; do
  case $arg in
    *:*) dir=${arg%%:*} path=${arg#*:} ;;
    *)   dir=$arg path= ;;
  esac
  dir=$(cd "$dir" && pwd)
  path=${path:-$dir}
  case $path in
    /*) ;;
    *) echo "xefm-docker.sh: $path: the container path must start with /" >&2
       exit 2 ;;
  esac
  mounts="$mounts -v $dir:$path"
  if [ -z "$left" ]; then left=$path
  elif [ -z "$right" ]; then right=$path
  fi
done
[ -n "$left" ] && panes="--left $left --right ${right:-$left}"

# Build the image the first time, or on --update or --rebuild. The build
# prints its progress as it goes; installing ffmpeg and XeFM takes a few
# minutes. --update gives XEFM_BUILD a new value, and Docker then runs again
# only the steps after it: the system packages are reused as they are.
if [ -n "$build" ] || ! docker image inspect "$IMAGE" >/dev/null 2>&1; then
  echo "Building the $IMAGE image..." >&2
  # shellcheck disable=SC2086
  docker build $build -t "$IMAGE" - >&2 <<'EOF'
FROM python:3.12-slim
RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      ffmpeg libchromaprint-tools libarchive-tools \
 && rm -rf /var/lib/apt/lists/* \
 && mkdir -p /home/xefm && chmod 1777 /home/xefm
ARG XEFM_BUILD=
RUN pip install --no-cache-dir xefm
ENV HOME=/home/xefm LANG=C.UTF-8
ENTRYPOINT ["xefm"]
EOF
fi

for m in $ALWAYS_MOUNT; do
  mkdir -p "${m%%:*}"
  mounts="$mounts -v $m"
done

# Runs in the container in place of xefm. A Find Similar Files scan goes on
# after XeFM quits, but the container ends when this script does, and every
# process left in it is killed. So wait for the scans to finish; Ctrl-C
# stops them the way they stop themselves, keeping the groups found so far.
RUN_XEFM='
scans() {
  for p in /proc/[0-9]*; do
    case $(cat "$p/comm" 2>/dev/null) in
      python*) grep -qa similar_files "$p/cmdline" 2>/dev/null && echo "${p#/proc/}" ;;
    esac
  done
}
xefm "$@"; status=$?
if [ -n "$(scans)" ]; then
  echo "Waiting for Find Similar Files to finish. Ctrl-C stops it and keeps what it has found."
  trap "kill -INT \$(scans) 2>/dev/null; echo Stopping..." INT
  while [ -n "$(scans)" ]; do sleep 1; done
fi
exit $status
'

# $mounts and $panes are unquoted on purpose: they split into words.
# shellcheck disable=SC2086
exec docker run -it --rm --detach-keys=ctrl-^ \
  --user "$(id -u):$(id -g)" \
  -e TERM -e COLORTERM -e TERM_PROGRAM -e KITTY_WINDOW_ID \
  -e KONSOLE_VERSION -e TZ \
  $mounts \
  --entrypoint sh "$IMAGE" -c "$RUN_XEFM" xefm $panes

What it does:

  • Each folder is mounted at the same path it has on the host, so the paths XeFM shows and copies are ones you can paste into a host terminal. To mount one somewhere else, add the container path after a colon, as with docker run -v: ./xefm-docker.sh ~/Videos:/videos /volume1/music:/music.
  • It builds the image only when there is none, and shows the build's progress while it does; later runs start XeFM straight away.
  • Only the folders you name are mounted, besides the ones in ALWAYS_MOUNT. With no arguments none of your files are, which is safe for a first look; run ./xefm-docker.sh . to work on the current folder.
  • XeFM runs as your user and group, so new files in the mounted folders are yours, not root's.
  • --detach-keys=ctrl-^ gives Ctrl-P back to XeFM. Docker otherwise holds it as the first half of its own detach key, and XeFM binds it to copying paths.
  • ALWAYS_MOUNT at the top of the script lists the folders mounted on every run, so what XeFM keeps there outlasts the container. Edit the list to add your own. It starts with two:
    • ~/.xefm-docker, XeFM's settings, where you can edit config.py with your own editor. It is kept apart from ~/.xefm, which a desktop or terminal XeFM on the host uses with its own platform's settings.
    • ~/.cache/similar-files, the cache Find Similar Files keeps of what it has already measured, so a second search over the same files is fast.
  • A Find Similar Files scan still running when you quit XeFM is not cut off. The script waits for it to finish before the container ends, and Ctrl-C while it waits stops the scan the way its own Stop does: the groups found so far are kept, and its cache is closed properly.

A folder whose path contains a space or a colon cannot be mounted by this script; mount it through a symlink or a path without one.

Run it as yourself, not with sudo. Under sudo XeFM runs as root, its new files in the mounted folders are root's, and the settings go to root's home folder. If docker itself needs sudo on Linux, add yourself to the docker group instead (sudo usermod -aG docker $USER, then log in again).

Updating XeFM

The image keeps the XeFM it was built with. To get the latest release, run the script with --update:

./xefm-docker.sh --update ~/Videos ~/Music

This installs XeFM again and reuses the rest of the image (ffmpeg and the other system packages), so it takes about as long as pip install alone. To update those as well, from a newer Python base image, use --rebuild, which builds the whole image again as the first run did.

After downloading a new version of the script, run it once with --update so the image follows it. The first --update after a change to the script's image steps rebuilds the whole image; the ones after it are quick again. Each update leaves the previous image behind, unnamed; docker image prune removes those.

Notes

  • Keys and mouse work as in any terminal, since the terminal you run Docker in is the one XeFM draws in.
  • Pictures in the image viewer need a terminal that draws them (kitty, iTerm2, WezTerm, Konsole, or one with sixel). XeFM tells which one it is from TERM_PROGRAM and similar variables, which is why the script passes them into the container.
  • Copy Paths and the viewers' copy reach the host's clipboard through the terminal (the OSC 52 escape, which most terminals support).
  • Only programs in the image can be run from XeFM. Add the ones you want to the apt-get install line.
  • Times are shown in UTC unless TZ is set (for example TZ=Asia/Tokyo ./xefm-docker.sh …).

See also

Clone this wiki locally