↓Skip to main content

Running GoToSocial on CoreOS

·10 mins

In a previous post I talked about installing CoreOS on my home server and promised to follow up with a post about deploying services. Here it is!

I read a recent post by @technomancy where he discussed running his own GoToSocial instance and as that was something I’d been planning to do for a while, I thought I’d use that as an example - although my approach is quite different from his, as you shall see.

Many projects (GoToSocial included) give instructions for running in a Docker container and often include a Docker compose file, but I haven’t found so many examples of podman configurations out there: this post is an attempt to plug that gap.

Goal #

My objective is to get a publicly-accessible instance of GoToSocial running on my home server. I will configure GoToSocial to run in a podman container and configure a Caddy reverse proxy in another container to forward requests to the GoToSocial container. I’ll do this implementing ansible playbooks to install the required configuration files and create a user account once the container is running.

If you read Technomancy’s post, you’ll see he configured GoToSocial to serve directly (without a reverse proxy) but as I plan to run several services on the same server, I’ll use a reverse proxy to handle the TLS and SNI and forward requests to the appropriate container.

Running services on CoreOS #

CoreOS is optimized for running services in containers and comes with podman ready to go. Podman is tightly integrated with systemd and uses something called quadlets to generate systemd units. See Make systemd better for Podman with Quadlet for a brief introduction, or podman-systemd.unit(5) for the full details.

You can define quadlets to configure pods (sets of related containers), networks, volumes, and containers. They look a lot like systemd unit files, but with a [Pod], [Network], [Volume], or [Container] block that is specific to podman. These files are installed into /etc/containers/systemd/ and when you run systemctl daemon-reload a generator runs and writes the corresponding systemd unit files to /run/systemd/generator/.

Prerequisites #

Some one-off setup on the CoreOS server will make life easier when it comes to deploying services.

Configure subordinate uids and gids for containers #

Podman has a feature for launching each container with a unique range of uids, using the --userns=auto option. This improves security as the root user (uid=0) in the container will map to a completely different uid on the host system. In order to use this, the documentation suggests allocating the top 2 billion UIDs and GIDs to podman. You do this by adding configuration for the containers username to /etc/subuid and /etc/subgid. The ansible tasks for this look like:

- name: Configure subuid for containers
  ansible.builtin.lineinfile:
    path: /etc/subuid
    line: "containers:2147483647:2147483648"
    regexp: "^containers:"

- name: Configure subgid for containers
  ansible.builtin.lineinfile:
    path: /etc/subgid
    line: "containers:2147483647:2147483648"
    regexp: "^containers:"

Configure a bridge network for services #

The default network created by podman does not have DNS enabled. You cannot change this setting but must instead create a new network with DNS enabled; services deployed to this network will be able to communicate via their host names. We create a bridge network for the services by configuring a network quadlet:

- name: Configure services network
  ansible.builtin.copy:
    dest: /etc/containers/systemd/services.network
    content: |
      [Unit]
      Description = Podman network for local services

      [Network]
      Driver = bridge

      [Install]
      # Start by default on boot
      WantedBy = multi-user.target default.target

Configure UDP buffer sizes #

HTTP/3 is enabled by default in Caddy. This uses the QUIC protocol over UDP, and Caddy will complain if it cannot increase the size of its UDP send and receive buffers. You need to increase some kernel parameters to allow this:

- name: Configure UDP receive buffer size
  ansible.builtin.sysctl:
    name: net.core.rmem_max
    value: 7500000
- name: Configure UDP send buffer size
  ansible.builtin.sysctl:
    name: net.core.wmem_max
    value: 7500000

That’s it for the prerequisites. Download the full playbook: prereq.yml.

Deploy GoToSocial #

To keep things simple I’ll use SQLite as the database - it will be perfectly adequate for this small instance. The GoToSocial documentation suggests creating two volumes when running the service in a container: one for the data, and one for caching:

- name: Create data volume quadlet
  ansible.builtin.copy:
    dest: /etc/containers/systemd/gotosocial-data.volume
    content: |
      [Unit]
      Description = GoToSocial data volume

      [Volume]
  notify:
    - Reload systemd
- name: Create cache volume quadlet
  ansible.builtin.copy:
    dest: /etc/containers/systemd/gotosocial-cache.volume
    content: |
      [Unit]
      Description = GoToSocial cache volume

      [Volume]
  notify:
    - Reload systemd

In case you’re not familiar with ansible, these notify settings tell it to run the named handler(s) if the resource changes. In this case, we want to reload systemd to generate the volume units. The handler will only be run once, even if both files change. The handlers are defined in a handlers section of the playbook:

handlers:
  - name: Reload systemd
    ansible.builtin.systemd_service:
      daemon_reload: true
  - name: Start container
    ansible.builtin.systemd_service:
      name: gotosocial.service
      state: started

The container itself is another quadlet. All of the configuration is through environment variables that we specify in the [Container] block:

- name: Create service quadlet
  ansible.builtin.copy:
    dest: /etc/containers/systemd/gotosocial.container
    content: |
      [Unit]
      Description = GoToSocial container

      [Container]
      Image = docker.io/superseriousbusiness/gotosocial:latest
      Volume = gotosocial-data.volume:/gotosocial/storage:U,Z
      Volume = gotosocial-cache.volume:/gotosocial/.cache:U,Z
      Network = services.network
      UserNS = auto
      Environment = GTS_HOST=social.1729.org.uk
      Environment = GTS_DB_TYPE=sqlite
      Environment = GTS_DB_ADDRESS=/gotosocial/storage/sqlite.db
      Environment = GTS_LETSENCRYPT_ENABLED=false
      Environment = GTS_WAZERO_COMPILATION_CACHE=/gotosocial/.cache
      Environment = GTS_TRUSTED_PROXIES=10.89.0.0/24
      Environment = TZ=Europe/London

      [Service]
      TimeoutStartSec = 900

      [Install]
      WantedBy = multi-user.target default.target
  notify:
    - Reload systemd
    - Start container

Note that the Volume configuration references the volume quadlets created by this playbook, and the Network refers to the network quadlet created by the prerequisites playbook. The systemd generator will make sure the units specify the required dependencies to start in the right order, and it will replace each quadlet reference with the name of the corresponding volume (or network) in the command that launches the container.

The U option on the volume tells podman to change ownership of the volume to match the user namespace of the container, and the Z option configures an SE Linux label making it private to this container.

The TimeoutStartSec is set ridiculously high to allow time for pulling in the container the first time it is run.

There is no need to publish any ports to the host as the proxy will be communicating with this container over the bridge network.

Download the playbook: gotosocial.yml.

Deploy Caddy #

In order for Caddy to retrieve Let’s Encrypt certificates for the server, I had to add the host name social.1729.org.uk as an A record with the address of my home broadband connection (I have a static IP address) and configure my broadband router to forward the HTTP and HTTPS ports to my home server.

The configuration for the Caddy container is very similar to that of GoToSocial: a couple of volumes and a container quadlet. The playbook first creates a directory on the host for the Caddy configuration and installs the configuration file there:

- name: Create configuration directory
  ansible.builtin.file:
    path: /etc/caddy
    state: directory
- name: Install Caddyfile
  ansible.builtin.copy:
    dest: /etc/caddy/Caddyfile
    content: |
      social.1729.org.uk {
         encode zstd gzip
         reverse_proxy * systemd-gotosocial:8080 {
           flush_interval -1
         }
      }

Note the naming conventions here: the gotosocial.container quadlet becomes a unit called gotosocial.service, and the podman container is named after the unit, systemd-gotosocial. Because we created a network with DNS support, the Caddy container can refer to the GoToSocial container by its host name.

The playbook next creates configuration and data volume quadlets for Caddy. Caddy stores TLS certificates, private keys, OCSP staples, and other necessary information to the data directory.

- name: Create data volume quadlet
  ansible.builtin.copy:
    dest: /etc/containers/systemd/caddy-data.volume
    content: |
      [Unit]
      Description = Create caddy data volume

      [Volume]
    notify:
      - Reload systemd
- name: Create config volume quadlet
  ansible.builtin.copy:
    dest: /etc/containers/systemd/caddy-config.volume
    content: |
      [Unit]
      Description = Create caddy config volume

      [Volume]
  notify:
    - Reload systemd

Finally it creates the container quadlet:

- name: Create container quadlet
  ansible.builtin.copy:
    dest: /etc/containers/systemd/caddy.container
    content: |
      [Unit]
      Description = Caddy Reverse Proxy Service

      [Container]
      Image = docker.io/library/caddy:2.8-alpine
      HostName = caddy
      Network = services.network
      UserNS = auto
      Volume = caddy-data.volume:/data:U,Z
      Volume = caddy-config.volume:/config:U,Z
      Volume = /etc/caddy:/etc/caddy:ro
      PublishPort = 443:443
      PublishPort = 443:443/udp
      PublishPort = 80:80
      AddCapability = net_admin

      [Service]
      TimeoutStartSec = 900

      [Install]
      # Start by default on boot
      WantedBy = multi-user.target default.target
  notify:
    - Reload systemd
    - Start container

Note that the third Volume is mounting the Caddy configuration from the host into the container read-only. We should be able to update the configuration file and trigger Caddy to reload it without needing to restart the container.

This container does publish its HTTP and HTTPS listening ports to the host.

That’s it! Here’s the playbook for Caddy: caddy.yml.

Running the playbooks #

I usually run the playbooks on my local workstation rather than on the server itself, so there’s no need to install ansible on the server (although you do need to install Python there).

An inventory.yml file tells ansible how to connect to the server:

---
ungrouped:
  hosts:
    limiting-factor:
      ansible_user: core
      ansible_host: 192.168.1.110

The become: true in the playbooks tells ansible to become root on the server before running tasks (the core user has passwordless sudo access), so I can simply run ansible-playbook to deploy these services, for example:

export ANSIBLE_VAULT_PASSWORD_FILE=/path/to/some/file/outside/the/git/repository
ansible-playbook -i inventory.yml prereq.yml

I need to export the password file location here so that ansible can decrypt the host variables. (You’ll see them in use in the next section.) In the ansible git repository I have a file host_vars/limiting-factor.yml that defines variables specific to the host. These include database passwords and other confidential information so this file is encrypted using ansible-vault.

Creating a user #

GoToSocial doesn’t (yet) have a way to create users through the web UI so you have to do this by running a command in the container (see Creating users).

Rather than running these commands directly on the server, I used ansible for this too: that gives me a record of exactly what was done and an easy way to repeat it if it needs doing again. Ansible provides the podman_container_exec module for running commands in an existing container:

- name: Create user
  containers.podman.podman_container_exec:
    name: systemd-gotosocial
    command: >
      /gotosocial/gotosocial admin account create
      --username {{ goto_social_username }}
      --email {{ goto_social_email }}
      --password {{ goto_social_password }}
- name: Promote user to admin
  containers.podman.podman_container_exec:
    name: systemd-gotosocial
    command: >
      /gotosocial/gotosocial admin account promote
      --username {{ goto_social_username }}
- name: Restart the container
  ansible.builtin.systemd_service:
    name: gotosocial.service
    state: restarted

The values enclosed in {{ }} are ansible variables; in this case they are defined in the host_vars/limiting-factor.yml file, but ansible variables can be defined in many places: see Where to set variables for details.

The final action of restarting the container is needed because of the way caching works in GoToSocial: the server must be restarted after certain admin commands (like promoting a user) to force it to re-read values from the database.

Download the playbook: create-admin-user.yml.

Up and running #

That’s it! My instance is now running at https://social.1729.org.uk/ and you can follow me @ray@social.1729.org.uk.

Recap #

I implemented four ansible playbooks:

  • prereq.yml prepares the server to run the container workloads.
  • gotosocial.yml creates data and cache volumes for GoToSocial and a container to run the GoToSocial server, listening on the private bridge network.
  • caddy.yml creates configuration and cache volumes for Caddy and a container to run the Caddy reverse proxy that listens on the public HTTP and HTTPS ports and forwards requests to the GoToSocial container.
  • create-admin-user.yml creates a GoToSocial user account and promotes the user to an admin.

These playbooks are all stored in a git repository and, as well as being a record of how the server is configured, they make it easy to repeat the process if ever I need to do it again.

Learning more #

The online documentation for both podman and ansible is comprehensive, but if you prefer to learn from a book, I found Podman in Action by Daniel Walsh a great introduction. It’s a little bit out of date now and doesn’t cover the latest features, but it still provides a good grounding in the basics. For ansible, I recommend Ansible Up and Running by Lorin Hochstein et al.