Skip to content

Commit d481801

Browse files
committed
Docker: show welcome page on port 80 when entrypoint.d is empty.
The entrypoint script now performs a default configuration when no useful files are found in /docker-entrypoint.d/ The default configuration serves a welcome page in response to all requests, using Markdown unless text/html is sent in the Accept header. This provides a useful 'hello world' experience when running a Unit container for the first time.
1 parent 8ab16f7 commit d481801

File tree

5 files changed

+136
-33
lines changed

5 files changed

+136
-33
lines changed

pkg/docker/docker-entrypoint.sh

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@ if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
2525
if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
2626
echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..."
2727
else
28-
if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
29-
echo "$0: /docker-entrypoint.d/ is not empty, launching Unit daemon to perform initial configuration..."
30-
/usr/sbin/$1 --control unix:/var/run/control.unit.sock
28+
echo "$0: Launching Unit daemon to perform initial configuration..."
29+
/usr/sbin/$1 --control unix:/var/run/control.unit.sock
3130

32-
for i in $(/usr/bin/seq $WAITLOOPS); do
33-
if [ ! -S /var/run/control.unit.sock ]; then
34-
echo "$0: Waiting for control socket to be created..."
35-
/bin/sleep $SLEEPSEC
36-
else
37-
break
38-
fi
39-
done
40-
# even when the control socket exists, it does not mean unit has finished initialisation
41-
# this curl call will get a reply once unit is fully launched
42-
/usr/bin/curl -f -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
31+
for i in $(/usr/bin/seq $WAITLOOPS); do
32+
if [ ! -S /var/run/control.unit.sock ]; then
33+
echo "$0: Waiting for control socket to be created..."
34+
/bin/sleep $SLEEPSEC
35+
else
36+
break
37+
fi
38+
done
39+
# even when the control socket exists, it does not mean unit has finished initialisation
40+
# this curl call will get a reply once unit is fully launched
41+
/usr/bin/curl -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
42+
43+
if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
44+
echo "$0: /docker-entrypoint.d/ is not empty, applying initial configuration..."
4345

4446
echo "$0: Looking for certificate bundles in /docker-entrypoint.d/..."
4547
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.pem"); do
@@ -69,29 +71,30 @@ if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
6971
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -not -name "*.sh" -not -name "*.json" -not -name "*.pem" -not -name "*.js"); do
7072
echo "$0: Ignoring $f";
7173
done
74+
else
75+
echo "$0: /docker-entrypoint.d/ is empty, creating 'welcome' configuration..."
76+
curl_put /usr/share/unit/welcome/welcome.json "config"
77+
fi
7278

73-
echo "$0: Stopping Unit daemon after initial configuration..."
74-
kill -TERM $(/bin/cat /var/run/unit.pid)
79+
echo "$0: Stopping Unit daemon after initial configuration..."
80+
kill -TERM $(/bin/cat /var/run/unit.pid)
7581

76-
for i in $(/usr/bin/seq $WAITLOOPS); do
77-
if [ -S /var/run/control.unit.sock ]; then
78-
echo "$0: Waiting for control socket to be removed..."
79-
/bin/sleep $SLEEPSEC
80-
else
81-
break
82-
fi
83-
done
82+
for i in $(/usr/bin/seq $WAITLOOPS); do
8483
if [ -S /var/run/control.unit.sock ]; then
85-
kill -KILL $(/bin/cat /var/run/unit.pid)
86-
rm -f /var/run/control.unit.sock
84+
echo "$0: Waiting for control socket to be removed..."
85+
/bin/sleep $SLEEPSEC
86+
else
87+
break
8788
fi
88-
89-
echo
90-
echo "$0: Unit initial configuration complete; ready for start up..."
91-
echo
92-
else
93-
echo "$0: /docker-entrypoint.d/ is empty, skipping initial configuration..."
89+
done
90+
if [ -S /var/run/control.unit.sock ]; then
91+
kill -KILL $(/bin/cat /var/run/unit.pid)
92+
rm -f /var/run/control.unit.sock
9493
fi
94+
95+
echo
96+
echo "$0: Unit initial configuration complete; ready for start up..."
97+
echo
9598
fi
9699
fi
97100

pkg/docker/template.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ RUN set -ex \
7575
&& ln -sf /dev/stdout /var/log/unit.log
7676

7777
COPY docker-entrypoint.sh /usr/local/bin/
78+
COPY welcome.* /usr/share/unit/welcome/
7879

7980
STOPSIGNAL SIGTERM
8081

8182
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
82-
83+
EXPOSE 80
8384
CMD ["unitd", "--no-daemon", "--control", "unix:/var/run/control.unit.sock"]

pkg/docker/welcome.html

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Welcome to NGINX Unit</title>
5+
<style type="text/css">
6+
body { background: white; color: black; font-family: sans-serif; margin: 2em; line-height: 1.5; }
7+
h1,h2 { color: #00974d; }
8+
li { margin-bottom: 0.5em; }
9+
pre { background-color: beige; padding: 0.4em; }
10+
hr { margin-top: 2em; border: 1px solid #00974d; }
11+
.indent { margin-left: 1.5em; }
12+
</style>
13+
</head>
14+
<body>
15+
<h1>Welcome to NGINX Unit</h1>
16+
<p>Congratulations! NGINX Unit is installed and running.</p>
17+
<h3>Useful Links</h3>
18+
<ul>
19+
<li><b><a href="https://unit.nginx.org/configuration/?referer=welcome&platform=docker">https://unit.nginx.org/configuration/</a></b><br>
20+
To get started with Unit, see the <em>Configuration</em> docs, starting with
21+
the <em>Quick Start</em> guide.</li>
22+
<li><b><a href="https://unit.nginx.org/howto/docker/?referer=welcome&platform=docker">https://unit.nginx.org/howto/docker/</a></b><br>
23+
For guidance about running <em>Unit in Docker</em> and tips for containerized
24+
applications.
25+
<li><b><a href="https://github.com/nginx/unit">https://github.com/nginx/unit</a></b><br>
26+
See our GitHub repo to browse the code, contribute, or seek help from the
27+
<a href="https://github.com/nginx/unit#community">community</a>.</li>
28+
</ul>
29+
30+
<h2>Next steps</h2>
31+
32+
<h3>Check Current Configuration</h3>
33+
<div class="indent">
34+
<p>Unit's control API is currently listening for configuration changes
35+
on the <a href="https://en.wikipedia.org/wiki/Unix_domain_socket">Unix socket</a> at
36+
<b>/var/run/control.unit.sock</b> inside the container.<br>
37+
To see the current configuration run:</p>
38+
<pre>docker exec -ti <containerID> curl --unix-socket /var/run/control.unit.sock http://localhost/config</pre>
39+
</div>
40+
41+
<hr>
42+
<p><a href="https://unit.nginx.org/?referer=welcome&platform=docker">NGINX Unit &mdash; the universal web app server</a><br>
43+
NGINX, Inc. &copy; 2023</p>
44+
</body>
45+
</html>

pkg/docker/welcome.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"listeners": {
3+
"*:80": {
4+
"pass": "routes"
5+
}
6+
},
7+
8+
"routes": [
9+
{
10+
"match": {
11+
"headers": {
12+
"accept": "*text/html*"
13+
}
14+
},
15+
"action": {
16+
"share": "/usr/share/unit/welcome/welcome.html"
17+
}
18+
},
19+
{
20+
"action": {
21+
"share": "/usr/share/unit/welcome/welcome.md"
22+
}
23+
}
24+
]
25+
}

pkg/docker/welcome.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Welcome to NGINX Unit
2+
=====================
3+
4+
Congratulations! NGINX Unit is installed and running.
5+
6+
Useful Links
7+
------------
8+
9+
* https://unit.nginx.org/
10+
- Get started with the 'Configuration' docs, starting with the 'Quick Start' guide.
11+
12+
* https://unit.nginx.org/howto/docker/
13+
- Guidance for running Unit in a container and tips for containerized applications.
14+
15+
* https://github.com/nginx/unit
16+
- See our GitHub repo to browse the code, contribute, or seek help from the community.
17+
18+
Current Configuration
19+
---------------------
20+
Unit's control API is currently listening for configuration changes on the Unix socket at
21+
`/var/run/control.unit.sock` inside the container.
22+
23+
Read the current configuration with
24+
```
25+
docker exec -ti <containerID> curl --unix-socket /var/run/control.unit.sock http://localhost/config
26+
```
27+
28+
---
29+
NGINX Unit - the universal web app server

0 commit comments

Comments
 (0)