Simple Alpine & Komga server inside lxc container
Launch an lxc container
Launch an lxc instance with an Image from default image hosts with an container name container_name.
$ lxc launch images:alpine/edge/amd64 container_name
creating container_name
starting container_name
$
If you receive an error in this step, consider check your user permission or troubleshoot it. Internet is your friend, and error logs are your keywords.
Get container's shell
Get into container's shell by executing sh command into the container. Whichever the way is fine, with alpine's default shell is sh.
$ lxc exec container_name sh
~ #
$ lxc exec container_name -- sh --login
container_name:~#
Download libraries and the app
Komga requires java runtime, and as of this writing, openjdk11 is the latest package available to download, with all other versions above ( openjdk12, etc... ) it is still in testing branch. Consider check those packages first here.
apk is the default Alpine package manager, much like apt or dnf in Ubuntu and CentOS. To add the package, we need to update it first and then download the package:
~$ apk update
~$ apk add openjdk11
Since we'll write a script to run it on boot, we need a text editor to do it. vi is the default text editor, but you can use nano if you prefer a quick, user-friendly one instead.
We'll use nano for the next following steps.
~$ apk add nano
since Komga is not available in alpine yet, we could grab one from their page instead. Grab the latest .jar file available in their Release page and use wget command do download it to your container. It will be saved on the current working directory ( in this case, /root/)
~$ wget https://github.com/gotson/komga/releases/download/v0.59.0/komga-0.59.0.jar
Installing the app and writing a script.
This step is adapted from Arch Linux's PKGBUILD file and optional. You can skip it if you want.
Create the directory /usr/share/java/komga/ (if it doesn't exist) and move the downloaded file there as komga.jar
~$ mkdir -p /usr/share/java/komga/
~$ mv komga-0.59.0.jar /usr/share/java/komga/komga.jar
Then, Write an executable script into /usr/bin directory to quickly run the jar fire using text editors.
~$ nano /usr/bin/komga
Copy the following script into the file
#!/bin/sh
exec /usr/bin/java -jar '/usr/share/java/komga/komga.jar'
and save it by pressing CTRL+S, then exit by pressing CTRL+X. Don't forget to make it executable by allowing execution permission flag
~$ chmod +x /usr/bin/komga
Setting up the service
Alpine Linux uses the OpenRC init system to start services. Refer to their documentation for more information about it.
All OpenRC services must be placed on /etc/init.d/ directory.
One main advantage is that it uses little-to-no boilerplate code to write the service. In this case, We just need to populate name, command, and command_args to be able to run it properly.
~$ nano /etc/init.d/komga
#!/sbin/openrc-run
name="Komga server"
command="/usr/bin/komga"
start_stop_daemon_args="--background"
~$ chmod +x /etc/init.d/komga
If you skip the previous step, then you need to tweak the script a little bit:
#!/sbin/openrc-run
name="Komga server"
command="/usr/bin/java"
command_args="-jar /root/komga.jar"
start_stop_daemon_args="--background"
OpenRC takes advantage of start-stop-daemon to control the creation and termination of system-level processes. since Komga does not have an option to run as a daemon, we tell start-stop-daemon to fork before starting the process, and force it into the background by passing --background parameter on start_stop_daemon_args variable.
Final step
The last step would be enable and start komga service.
~$ rc-update add komga
~$ rc-service komga start
This way next time when you start the container, Komga will automatically be started too.
Proceed to exit the container and access the page with an IP address assigned for the container by using lxc ls command
$ lxc ls
+----------------+---------+---------------------+------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+----------------+---------+---------------------+------+-----------+-----------+
| container_name | RUNNING | 10.0.0.40 (eth0) | | CONTAINER | 0 |
+----------------+---------+---------------------+------+-----------+-----------+
Komga listen to port 8080 by default. Therefore, you can access it by putting http://10.0.0.40:8080 in your browser, for example.
Refer to their page for further configuration, e.g. change default port, schedule database backup, set periodic directory scan, etc.
Additional steps
Bind host directory to the container
$ lxc config device add container_name disk_name disk source=/host/directory path=/container/directory
FAQs
Q: Why use LXC instead of Proxmox or Docker?
A: I need flexibility, resource-light, and lack of proper docs. LXD is the one that matches my need the most. Another reason is to give you a different view of setting up a container rather than blindly firing up a docker.
Q: I found a package with newer version available in their site, but not when I try to install it.
A: You might see that package under `testing` branch. It is not listed in your system by default, because it IS still not production-ready and might prone to break your system.
Consider check the wiki page to enable testing branch in your system. Use at your own risk:
https://wiki.alpinelinux.org/wiki/Enable_Community_Repository#Using_packages_from_testing_.28edge.29