Skip to content

Commit f9790e0

Browse files
authored
Merge pull request #2 from Ferruck/build-systemd-image
container/lighttpd: Add image build option
2 parents d30064e + ddd59b7 commit f9790e0

File tree

8 files changed

+138
-9
lines changed

8 files changed

+138
-9
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,41 @@ example](https://github.com/BobBuildTool/bob-example-embedded).
2727

2828
# How to build
2929

30-
Clone the recipes and build them with Bob:
30+
First of all you have to clone the recipes and change to the checked-out
31+
directory:
3132

3233
$ git clone https://github.com/BobBuildTool/bob-example-containers.git \
3334
--recurse-submodules
3435
$ cd bob-example-containers
36+
37+
The next steps depend on what you want to build.
38+
39+
## Docker Container
40+
41+
These recipes can build a minimal container image that has solely lighttpd and
42+
the required dependencies installed by running the following command:
43+
44+
$ # Still in the bob-example-containers directory
3545
$ bob build containers::lighttpd
3646

37-
These recipes build a minimal container image that has solely lighttpd and the
38-
required dependencies installed.
47+
## Systemd Portable Service
48+
49+
These recipes can also provide a systemd portable service that can be attached
50+
with `portablectl` by issuing the following command:
51+
52+
$ # Still in the bob-example-containers directory
53+
$ bob build containers::lighttpd-image
3954

4055
# How to use
4156

57+
Again, usage of the build products depends on the chosen target.
58+
59+
## Docker Container
60+
4261
To use it you have to import it in Docker:
4362

44-
$ # Still in the bob-example-containers directory
63+
$ # Still in the bob-example-containers directory after building
64+
$ # containers::lighttpd
4565
$ tar -C $(bob query-path -f '{dist}' --release containers::lighttpd) -c . \
4666
| docker import - lighttpd
4767

@@ -53,6 +73,20 @@ Now you can serve any host directory with the lighttpd in the image:
5373

5474
This will expose the lighttpd at port 8080 on your host.
5575

76+
## Systemd Portable Service
77+
78+
First you have to import the portable service:
79+
80+
$ # Still in the bob-example-containers directory after building
81+
$ # containers::lighttpd-image
82+
$ portablectl attach $(bob query-path -f '{dist}' --release containers::lighttpd-image)/lighttpd.raw
83+
84+
Now you can start the lighttpd as any other systemd service on your host:
85+
86+
$ systemctl start lighttpd.service
87+
88+
lighttpd should now listen on port 80.
89+
5690
# Contributions
5791

5892
Contributions are welcome in form of feedback, bug reports and code. If you want

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bobMinimumVersion: "0.15"
1+
bobMinimumVersion: "0.16rc1"
22
layers:
33
- basement

recipes/containers/lighttpd.yaml

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,44 @@ buildScript: |
3939
lighttpd:x:33:
4040
EOF
4141
42-
packageScript: |
43-
cp -a $1/* .
42+
# Default landing page
43+
mkdir -p srv/www
44+
install -m 644 $<<lighttpd/index.html>> srv/www/index.html
45+
46+
multiPackage:
47+
"":
48+
buildScript: |
49+
# PID file
50+
mkdir -p run/lighttpd
51+
touch run/lighttpd/lighttpd.pid
52+
53+
packageScript: |
54+
cp -a $1/* .
55+
56+
image:
57+
buildTools: [squashfs-tools]
58+
buildScript: |
59+
# Remove username and group because the systemd.service already
60+
# takes care of this.
61+
sed -i -e '/server.username/d' -e '/server.groupname/d' \
62+
etc/lighttpd/lighttpd.conf
63+
64+
# See https://systemd.io/PORTABLE_SERVICES.html
65+
mkdir -p etc/systemd/system
66+
install -m 644 $<<lighttpd/lighttpd.service>> \
67+
etc/systemd/system/lighttpd.service
68+
install -m 644 $<<lighttpd/lighttpd.socket>> \
69+
etc/systemd/system/lighttpd.socket
70+
touch etc/machine-id
71+
ln -s ../usr/lib/os-release etc/os-release
72+
touch etc/resolv.conf
73+
mkdir -p proc sys dev run tmp usr/lib var/tmp
74+
cat >usr/lib/os-release <<EOF
75+
NAME="lighttpd"
76+
PRETTY_NAME="Bob lighttpd container example"
77+
ID=linux
78+
HOME_URL="https://github.com/BobBuildTool/bob-example-containers"
79+
EOF
80+
81+
packageScript: |
82+
mksquashfs $1 lighttpd.raw
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>lighttpd works</title>
5+
</head>
6+
<body>
7+
<h1>It works!</h1>
8+
Looks like the lighttpd service is working.
9+
<h2>Configuration needed</h2>
10+
<h3>Systemd portable service</h3>
11+
In order to serve something useful with the portable lighttpd service
12+
you must bind the document root directory to <code>/srv/www</code>. This
13+
done by adding a drop-in with a <code>BindReadOnlyPaths=</code>, e.g.:
14+
<pre>
15+
# systemctl edit lighttpd.service
16+
[Service]
17+
BindReadOnlyPaths=&lt;your-html-dir&gt;:/srv/www
18+
</pre>
19+
Then restart the service and you should see your documents.
20+
<h3>Docker container</h3>
21+
When running the docker container you should add the proper
22+
<code>-v</code> option to the command line, e.g.:
23+
<pre>
24+
$ docker run -v &lt;your-html-dir&gt;:/srv/www lighttpd ...
25+
</pre>
26+
</body>
27+
</html>

recipes/containers/lighttpd/lighttpd.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var.log_root = "/var/log/lighttpd"
44
var.server_root = "/srv/www"
5-
var.state_dir = "/var/run"
5+
var.state_dir = "/run/lighttpd"
66
var.home_dir = "/var/lib/lighttpd"
77
var.conf_dir = "/etc/lighttpd"
88

@@ -14,6 +14,7 @@ server.username = "lighttpd"
1414
server.groupname = "lighttpd"
1515
server.document-root = server_root
1616
server.pid-file = state_dir + "/lighttpd.pid"
17+
server.systemd-socket-activation = "enable"
1718

1819
server.errorlog = log_root + "/error.log"
1920

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[Unit]
2+
Description=A secure, fast, compliant and very flexible web-server
3+
After=syslog.target network.target lighttpd.socket
4+
Requires=lighttpd.socket
5+
6+
7+
[Service]
8+
Type=simple
9+
PIDFile=/run/lighttpd/lighttpd.pid
10+
ExecStartPre=/usr/sbin/lighttpd -tt -f /etc/lighttpd/lighttpd.conf
11+
ExecStart=/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
12+
ExecReload=/bin/kill -USR1 $MAINPID
13+
Restart=on-failure
14+
15+
User=lighttpd
16+
Group=lighttpd
17+
DynamicUser=yes
18+
19+
RuntimeDirectory=lighttpd
20+
LogsDirectory=lighttpd
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Unit]
2+
Description=lighttpd socket
3+
4+
[Socket]
5+
ListenStream=80
6+
7+
[Install]
8+
WantedBy=sockets.target

0 commit comments

Comments
 (0)