Emacs in the Cloud A developer built a self-hosted cloud Emacs setup that runs an Emacs daemon inside a Docker container and exposes it through the ttyd browser terminal, with Authelia handling authentication and session tokens and nginx acting as the reverse proxy, according to the write-up, which credits Gemini as a heavy consulting aid. The stack, defined in a docker-compose.yml with three services (nginx-emacs on port 9000, authelia-emacs, and emacs-web-ide on port 9001 mapped to ttyd's port 8080), launches one emacsclient per browser window, giving multi-window access for free. The author says the goal was to make home-network notes location-agnostic after consolidating them into a single Dockerized Emacs session. Emacs in the Cloud As of late, I’ve migrated a lot of my home network notes and such into a single emacs session running in Docker. While it’s quite powerful, it lacks the location-agnosticism convenience of keeping them in the cloud in some way. So I set out to fix that. What follows is a built heavily in consultation with Gemini configuration for running an emacs session behind an in-browser terminal with 2-factor authentication, all as a family of Docker containers. We’ll go over each part one at a time. The key technologies used in this stack are: - emacs of course - ttyd TTY endpoint in the browser - authelia managing authentication and session tokens - nginx proxy to the emacs container and authelia The core container: emacs and ttyd. The core application is an emacs daemon running in a docker container with ttyd. Each invocation of ttyd launches emacsclient. FROM debian:bookworm-slim Install Emacs no-X11 version and minimal dependencies RUN apt-get update && apt-get install -y \ emacs-nox \ curl \ ca-certificates \ && rm -rf /var/lib/apt/lists/ Download the latest stable pre-compiled ttyd binary Using x86 64 architecture - adjust to aarch64 if building on Apple Silicon M-chips RUN curl -Lo /usr/local/bin/ttyd https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.x86 64 \ && chmod +x /usr/local/bin/ttyd Create a non-root user matching typical host UID/GID 1000 for clean file permissions RUN groupadd -g 1000 developer && \ useradd -u 1000 -g developer -m -s /bin/bash developer Setup our editing workspace RUN mkdir -p /workspace && chown -R developer:developer /workspace WORKDIR /workspace Copy and configure the entrypoint script COPY entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh Switch to the non-root user USER developer EXPOSE 8080 ENTRYPOINT "/usr/local/bin/entrypoint.sh" And the entrypoint.sh script: bash /bin/bash set -e echo "Starting Emacs background daemon..." emacs --daemon Wait for the Emacs server socket to become available echo "Waiting for Emacs server socket to initialize..." while -S /tmp/emacs1000/server && -S /run/user/1000/emacs/server ; do Check fallback standard location if XDG RUNTIME DIR isn't strict if emacsclient -a false -e 't' /dev/null 2 &1; then break fi sleep 0.5 done echo "Emacs daemon is ready " echo "Launching ttyd web terminal on port 8080..." Execute ttyd. Running via 'exec' makes it PID 1 so it handles OS signals correctly. exec ttyd --writable -p 8080 emacsclient -nw /workspace Conveniently, ttyd launches one instance of emacsclient per browser window, so I get multi-window access to my emacs client for free. The components With that container in place, here is the docker-compose.yml file to stand up the architecture. services: nginx-emacs: image: nginx:alpine container name: nginx-emacs restart: unless-stopped ports: - "9000:80" volumes: - ./service-configs/nginx/conf.d:/etc/nginx/conf.d:ro networks: - emacs auth net depends on: - authelia-emacs - emacs-web-ide authelia-emacs: image: authelia/authelia:latest container name: authelia-emacs restart: unless-stopped volumes: - ./service-configs/authelia/config:/config environment: - TZ=America/New York networks: - emacs auth net emacs-web-ide: build: . container name: emacs web terminal ports: - "9001:8080" volumes: Maps a local folder named 'project files' to Emacs' working directory - ./project files:/workspace Optional: Mount a local init file or directory if you have an existing config - ~/.emacs.d:/home/developer/.emacs.d environment: - TERM=xterm-256color restart: unless-stopped networks: - emacs auth net networks: emacs auth net: driver: bridge The emacs / ttyd container we’ve already talked about; let’s talk about the configuration of the other pieces. nginx Here is the config for nginx at service-configs/nginx/conf.d/default.conf 1. Authelia Login Portal server { listen 80; server name emacs-auth.fixermark.com; location / { proxy pass http://authelia-emacs:9091; proxy set header Host $host; proxy set header X-Real-IP $http cf connecting ip; proxy set header X-Forwarded-For $proxy add x forwarded for; proxy set header X-Forwarded-Proto https; proxy set header X-Forwarded-Host $http host; proxy set header X-Forwarded-Uri $request uri; } } 2. Protected ttyd + Emacs Instance server { listen 80; server name emacs.fixermark.com; Internal endpoint for Authelia verification location /internal/authelia/authz { internal; proxy pass http://authelia-emacs:9091/api/authz/auth-request; proxy set header X-Original-Method $request method; proxy set header X-Original-URL https://$http host$request uri; proxy set header X-Forwarded-For $http cf connecting ip; proxy set header Content-Length ""; proxy pass request body off; } Main ttyd + Emacs route location / { auth request /internal/authelia/authz; auth request set $user $upstream http remote user; auth request set $groups $upstream http remote groups; proxy set header Remote-User $user; proxy set header Remote-Groups $groups; error page 401 =302 https://emacs-auth.fixermark.com; Proxy to ttyd on container port 8080 proxy pass http://emacs-web-ide:8080; proxy set header Host $host; proxy set header X-Real-IP $http cf connecting ip; proxy set header X-Forwarded-For $proxy add x forwarded for; proxy set header X-Forwarded-Proto https; Required for ttyd WebSocket connections proxy http version 1.1; proxy set header Upgrade $http upgrade; proxy set header Connection "upgrade"; proxy read timeout 86400; } } This has configured two servers behind the proxy: my ttyd server and an authelia server, selectd by the domain name we are accessing through. I’m using Cloudflare to provide a tunnel to my server, and it serves one “application” at each endpoint. Q: Don’t you know that means Cloudflare could see all traffic to your server unencrypted? A: Yes. ; There are a couple interesting pieces worth noting here: 1. Authelia is being vended behind the emacs-auth domain and also the emacs.fixermark.com/internal/authelia/authz path internal only, nginx can route that to itself but won’t do special handling for it coming from outside . 2. The auth request stanza in the Emacs routing logic makes nginx consult authelia-emacs:9091/api/authz/auth-request before vending the emacs server, and a 401 from Authelia will cause a 302 redirect into the auth endpoint. Otherwise, This is the glue that allows Authelia to sit “in front” of the emacs server by having nginx consult Authelia for confirmation requests are valid before letting them through. Authelia configuration Authelia requires setting up a few magic keys and a user account. We’ll start with the service configuration, at service-configs/authelia/config/configuration.yml : theme: dark server: address: 'tcp://0.0.0.0:9091' log: level: info totp: disable: false issuer: emacs.fixermark.com period: 30 skew: 1 webauthn: disable: true identity validation: reset password: jwt secret: 'key1' authentication backend: file: path: /config/users database.yml password: algorithm: argon2 argon2: variant: argon2id iterations: 3 memory: 65536 parallelism: 4 key length: 32 salt length: 16 access control: default policy: deny rules: - domain: emacs.fixermark.com policy: two factor session: secret: 'key2' expiration: 1h inactivity: 15m remember me: 1M cookies: - name: authelia emacs session domain: fixermark.com authelia url: https://emacs-auth.fixermark.com default redirection url: https://emacs.fixermark.com regulation: max retries: 3 find time: 2m ban time: 5m storage: encryption key: 'key3' local: path: /config/db.sqlite3 notifier: disable startup check: false filesystem: filename: /config/notification.txt The placeholders key1 , key2 , and key3 above are secrets that need to be set up they can be generated with openssl rand -hex 32 . I then need a pre-specified user. This goes in the file service-configs/authelia/config/users database.yml : users: fixermark: displayname: "Mark Tomczak" password: "PASSWORD" email: fixermark@gmail.com groups: - admins The PASSWORD is a placeholder for a password hash that we can generate out-of-band with Authelia: docker run --rm authelia/authelia:latest authelia crypto hash generate argon2 --password 'YOUR NEW PASSWORD' First login with Authelia With these pieces set up, there is one more bit of out-of-band to handle on first login: I have turned on one-time passwords that I load into Google Authenticate . The first time I try to add a device, a confirmation code will be needed that is actually put in service-configs/authelia/config/notification.txt . I’ll have to get the code from there to paste into the Authelia UI. With all these pieces set up, it is now possible to login via Authelia to reach my emacs session from anywhere on the web. Potential improvements Connection stability Timeouts are a bit sloppy; when the session breaks, you end up in an infinite loop of “Hit return to reload” prompts from ttyd. This is adjustable in the configuration.yml for Authelia. Note : I have not tried these configurations yet. session: secret: '