Flatpaking a web server

Someone asked in #flatpak whether it was possible to package a web server.

I knew it was possible, but was curious as to how exactly.

So I wrote a trivial "web server":

#!/bin/bash

/usr/bin/python3 -m http.server 8000

And a Flatpak manifest to package that script:

{
  "id": "fr.daitauha.WebServer",
  "runtime": "org.freedesktop.Platform",
  "sdk": "org.freedesktop.Sdk",
  "runtime-version": "1.6",
  "command": "httpd",
  "finish-args": [
    "--share=network"
  ],
  "modules": [
    {
      "name": "httpd",
      "buildsystem": "simple",
      "build-commands": [
        "install -m755 -D webserver /app/bin/httpd"
      ],
      "sources": [
        {
          "type": "file",
          "path": "webserver"
        }
      ]
    }
  ]
}

The important part above is the --share=network argument. By default, the Flatpak sandbox prevents apps from accessing the network, whether as clients or servers. This option gives them full network access.

Let's build it:

$ flatpak-builder --install --user app fr.daitauha.WebServer.json 
Emptying app dir 'app'
Downloading sources
Initializing build dir
Committing stage init to cache
Starting build of fr.daitauha.WebServer
========================================================================
Building module httpd in /home/mathieu/.cache/flatpak-builder/build/httpd-1
========================================================================
Running: install -m755 -D webserver /app/bin/httpd
Committing stage build-httpd to cache
Cleaning up
Committing stage cleanup to cache
Finishing app
Please review the exported files and the metadata
Committing stage finish to cache
Exporting fr.daitauha.WebServer to repo
Commit: a639e450e42d3938d00d00be9d87bfc6e15a0fac1cea6dcabaaace5b91d7436c
Metadata Total: 9
Metadata Written: 1
Content Total: 3
Content Written: 0
Content Bytes Written: 0 (0 bytes)
Installing in user:
fr.daitauha.WebServer/x86_64/master fr.daitauha.WebServer-origin a639e450e42d
  permissions: network
Installing for user: fr.daitauha.WebServer/x86_64/master from fr.daitauha.WebServer-origin
[####################] 3 metadata, 3 content objects imported
Now at a639e450e42d.
Pruning cache

We can now run it:

flatpak run fr.daitauha.WebServer
Serving HTTP on 0.0.0.0 port 8000 ...

And voilĂ :

Browsing the web server

This is probably not very useful though. Flatpak focuses on desktop apps, and that's where it really shines.

For services such as a web server, other container technologies like Docker are doing a better job.