{"slug": "emacs-in-the-cloud", "title": "Emacs in the Cloud", "summary": "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.", "body_md": "# Emacs in the Cloud\n\nAs 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.\n\nSo 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:\n\n- emacs (of course)\n- ttyd (TTY endpoint in the browser)\n- authelia (managing authentication and session tokens)\n- nginx (proxy to the emacs container and authelia\n\n# The core container: emacs and ttyd.\n\nThe core application is an emacs daemon running in a docker container with ttyd. Each invocation of ttyd launches emacsclient.\n\n```\nFROM debian:bookworm-slim\n\n# Install Emacs (no-X11 version) and minimal dependencies\nRUN apt-get update && apt-get install -y \\\n    emacs-nox \\\n    curl \\\n    ca-certificates \\\n    && rm -rf /var/lib/apt/lists/*\n\n# Download the latest stable pre-compiled ttyd binary\n# (Using x86_64 architecture - adjust to aarch64 if building on Apple Silicon M-chips)\nRUN curl -Lo /usr/local/bin/ttyd https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.x86_64 \\\n    && chmod +x /usr/local/bin/ttyd\n\n# Create a non-root user matching typical host UID/GID (1000) for clean file permissions\nRUN groupadd -g 1000 developer && \\\n    useradd -u 1000 -g developer -m -s /bin/bash developer\n\n# Setup our editing workspace\nRUN mkdir -p /workspace && chown -R developer:developer /workspace\nWORKDIR /workspace\n\n# Copy and configure the entrypoint script\nCOPY entrypoint.sh /usr/local/bin/entrypoint.sh\nRUN chmod +x /usr/local/bin/entrypoint.sh\n\n# Switch to the non-root user\nUSER developer\n\nEXPOSE 8080\n\nENTRYPOINT [\"/usr/local/bin/entrypoint.sh\"]\n```\n\nAnd the entrypoint.sh script:\n\n``` bash\n#!/bin/bash\nset -e\n\necho \"Starting Emacs background daemon...\"\nemacs --daemon\n\n# Wait for the Emacs server socket to become available\necho \"Waiting for Emacs server socket to initialize...\"\nwhile [ ! -S /tmp/emacs1000/server ] && [ ! -S /run/user/1000/emacs/server ]; do\n    # Check fallback standard location if XDG_RUNTIME_DIR isn't strict\n    if emacsclient -a false -e 't' >/dev/null 2>&1; then\n        break\n    fi\n    sleep 0.5\ndone\n\necho \"Emacs daemon is ready!\"\necho \"Launching ttyd web terminal on port 8080...\"\n\n# Execute ttyd. Running via 'exec' makes it PID 1 so it handles OS signals correctly.\nexec ttyd --writable -p 8080 emacsclient -nw /workspace\n```\n\nConveniently, ttyd launches one instance of emacsclient per browser window, so I get multi-window access to my emacs client for free.\n\n# The components\n\nWith that container in place, here is the docker-compose.yml file to stand up the architecture.\n\n```\nservices:\n  nginx-emacs:\n    image: nginx:alpine\n    container_name: nginx-emacs\n    restart: unless-stopped\n    ports:\n      - \"9000:80\"\n    volumes:\n      - ./service-configs/nginx/conf.d:/etc/nginx/conf.d:ro\n    networks:\n      - emacs_auth_net\n    depends_on:\n      - authelia-emacs\n      - emacs-web-ide\n\n  authelia-emacs:\n    image: authelia/authelia:latest\n    container_name: authelia-emacs\n    restart: unless-stopped\n    volumes:\n      - ./service-configs/authelia/config:/config\n    environment:\n      - TZ=America/New_York\n    networks:\n      - emacs_auth_net\n\n  emacs-web-ide:\n    build: .\n    container_name: emacs_web_terminal\n    ports:\n      - \"9001:8080\"\n    volumes:\n      # Maps a local folder named 'project_files' to Emacs' working directory\n      - ./project_files:/workspace\n      # Optional: Mount a local init file or directory if you have an existing config\n      # - ~/.emacs.d:/home/developer/.emacs.d\n    environment:\n      - TERM=xterm-256color\n    restart: unless-stopped\n    networks:\n      - emacs_auth_net\n\nnetworks:\n  emacs_auth_net:\n    driver: bridge\n```\n\nThe emacs / ttyd container we’ve already talked about; let’s talk about the configuration of the other pieces.\n\n# nginx\n\nHere is the config for nginx at `service-configs/nginx/conf.d/default.conf`\n\n```\n# 1. Authelia Login Portal\nserver {\n    listen 80;\n    server_name emacs-auth.fixermark.com;\n\n    location / {\n        proxy_pass http://authelia-emacs:9091;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $http_cf_connecting_ip;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto https;\n        proxy_set_header X-Forwarded-Host $http_host;\n        proxy_set_header X-Forwarded-Uri $request_uri;\n    }\n}\n\n# 2. Protected ttyd + Emacs Instance\nserver {\n    listen 80;\n    server_name emacs.fixermark.com;\n\n    # Internal endpoint for Authelia verification\n    location /internal/authelia/authz {\n        internal;\n        proxy_pass http://authelia-emacs:9091/api/authz/auth-request;\n\n        proxy_set_header X-Original-Method $request_method;\n        proxy_set_header X-Original-URL https://$http_host$request_uri;\n        proxy_set_header X-Forwarded-For $http_cf_connecting_ip;\n        proxy_set_header Content-Length \"\";\n        proxy_pass_request_body off;\n    }\n\n    # Main ttyd + Emacs route\n    location / {\n        auth_request /internal/authelia/authz;\n\n        auth_request_set $user $upstream_http_remote_user;\n        auth_request_set $groups $upstream_http_remote_groups;\n        proxy_set_header Remote-User $user;\n        proxy_set_header Remote-Groups $groups;\n\n        error_page 401 =302 https://emacs-auth.fixermark.com;\n\n        # Proxy to ttyd on container port 8080\n        proxy_pass http://emacs-web-ide:8080;\n\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $http_cf_connecting_ip;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto https;\n\n        # Required for ttyd WebSocket connections\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection \"upgrade\";\n        proxy_read_timeout 86400;\n    }\n}\n```\n\nThis 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.\n\nQ: Don’t you know that means Cloudflare could see all traffic to your server unencrypted?\n\nA: Yes. ;)\n\nThere are a couple interesting pieces worth noting here:\n\n1. 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).\n2. The `auth_request` stanza in the Emacs routing logic makes nginx consult`authelia-emacs:9091/api/authz/auth-request` before vending the emacs server,\nand a 401 from Authelia will cause a 302 redirect into the auth\nendpoint. Otherwise,\n\nThis 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.\n\n# Authelia configuration\n\nAuthelia requires setting up a few magic keys and a user account. We’ll start\nwith the service configuration, at `service-configs/authelia/config/configuration.yml`:\n\n```\ntheme: dark\n\nserver:\n  address: 'tcp://0.0.0.0:9091'\n\nlog:\n  level: info\n\ntotp:\n  disable: false\n  issuer: emacs.fixermark.com\n  period: 30\n  skew: 1\n\nwebauthn:\n  disable: true\n\nidentity_validation:\n  reset_password:\n    jwt_secret: 'key1'\n\nauthentication_backend:\n  file:\n    path: /config/users_database.yml\n    password:\n      algorithm: argon2\n      argon2:\n        variant: argon2id\n        iterations: 3\n        memory: 65536\n        parallelism: 4\n        key_length: 32\n        salt_length: 16\n\naccess_control:\n  default_policy: deny\n  rules:\n    - domain: emacs.fixermark.com\n      policy: two_factor\n\nsession:\n  secret: 'key2'\n  expiration: 1h\n  inactivity: 15m\n  remember_me: 1M\n  cookies:\n    - name: authelia_emacs_session\n      domain: fixermark.com\n      authelia_url: https://emacs-auth.fixermark.com\n      default_redirection_url: https://emacs.fixermark.com\n\nregulation:\n  max_retries: 3\n  find_time: 2m\n  ban_time: 5m\n\nstorage:\n  encryption_key: 'key3'\n  local:\n    path: /config/db.sqlite3\n\nnotifier:\n  disable_startup_check: false\n  filesystem:\n    filename: /config/notification.txt\n```\n\nThe placeholders `key1`, `key2`, and `key3` above are secrets that need to be\nset up (they can be generated with `openssl rand -hex 32`).\n\nI then need a pre-specified user. This goes in the file\n`service-configs/authelia/config/users_database.yml`:\n\n```\nusers:\n  fixermark:\n    displayname: \"Mark Tomczak\"\n    password: \"PASSWORD\"\n    email: fixermark@gmail.com\n    groups:\n      - admins\n```\n\nThe `PASSWORD` is a placeholder for a password hash that we can generate\nout-of-band with Authelia:\n\n`docker run --rm authelia/authelia:latest authelia crypto hash generate argon2 --password 'YOUR_NEW_PASSWORD'`\n\n## First login with Authelia\n\nWith these pieces set up, there is one more bit of out-of-band to handle on\nfirst login: I have turned on one-time passwords (that I load into Google\nAuthenticate). The first time I try to add a device, a confirmation code will be\nneeded that is actually put in\n`service-configs/authelia/config/notification.txt`. I’ll have to get the code\nfrom there to paste into the Authelia UI.\n\nWith all these pieces set up, it is now possible to login via Authelia to reach my emacs session from anywhere on the web.\n\n# Potential improvements\n\n## Connection stability\n\nTimeouts are a bit sloppy; when the session breaks, you end up in an infinite\nloop of “Hit return to reload” prompts from ttyd. This is adjustable in the\n`configuration.yml` for Authelia. *Note*: I have not tried these configurations yet.\n\n```\nsession:\n  secret: '<secret>'\n  expiration: 12h       # Standard login lasts 12 hours (or e.g., 7d for 7 days)\n  inactivity: 0         # Disables idle timeout so sleeping your computer won't log you out\n  remember_me: 1M       # Checking \"Remember me\" keeps you logged in for 1 month\n```\n\n## Mobile support\n\nAs it stands, ttyd strictly works on Chrome / Android but doesn’t *work-*work. The default Android keyboard is ill-suited to emacs and its reliance on alt, meta, control, and arrow keys.\n\nThe easiest solution I found was to install\n[Unexpected keyboard](https://play.google.com/store/apps/details?id=juloo.keyboard2&hl=en_US)\ndirectly on my Android device. It gives me all the fancy input features I want\nto make emacs palatable. I may try in the future hacking some quick-tap macros\ninto the ttyd UI so that common operations are easier to execute.", "url": "https://wpnews.pro/news/emacs-in-the-cloud", "canonical_source": "https://blog.fixermark.com/posts/2026/emacs-in-the-cloud/", "published_at": "2026-12-01 14:11:07+00:00", "updated_at": "2026-12-01 14:17:09.131538+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Emacs", "ttyd", "Authelia", "nginx", "Docker", "Gemini", "Debian"], "also_reported_by": [], "takeaway": "Running ttyd with `--writable` and `exec`-ing it as PID 1 in a Debian bookworm-slim image alongside an Emacs daemon spawns one emacsclient per browser window, and pairing the container with Authelia and nginx adds 2-factor authentication in front of the terminal.", "recommendation": {"advice": "Run the Emacs daemon and ttyd in one container with ttyd exec'd as PID 1, and put Authelia plus nginx in front so browser terminal access requires 2-factor authentication.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/emacs-in-the-cloud", "markdown": "https://wpnews.pro/news/emacs-in-the-cloud.md", "text": "https://wpnews.pro/news/emacs-in-the-cloud.txt", "jsonld": "https://wpnews.pro/news/emacs-in-the-cloud.jsonld"}}