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.
This commit is contained in:
Liam Crilly
2023-05-09 22:53:18 +01:00
parent 8ab16f71fe
commit d481801907
5 changed files with 139 additions and 36 deletions

View File

@@ -25,8 +25,7 @@ if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..."
else
if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
echo "$0: /docker-entrypoint.d/ is not empty, launching Unit daemon to perform initial configuration..."
echo "$0: Launching Unit daemon to perform initial configuration..."
/usr/sbin/$1 --control unix:/var/run/control.unit.sock
for i in $(/usr/bin/seq $WAITLOOPS); do
@@ -39,7 +38,10 @@ if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
done
# even when the control socket exists, it does not mean unit has finished initialisation
# this curl call will get a reply once unit is fully launched
/usr/bin/curl -f -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
/usr/bin/curl -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
echo "$0: /docker-entrypoint.d/ is not empty, applying initial configuration..."
echo "$0: Looking for certificate bundles in /docker-entrypoint.d/..."
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.pem"); do
@@ -69,6 +71,10 @@ if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -not -name "*.sh" -not -name "*.json" -not -name "*.pem" -not -name "*.js"); do
echo "$0: Ignoring $f";
done
else
echo "$0: /docker-entrypoint.d/ is empty, creating 'welcome' configuration..."
curl_put /usr/share/unit/welcome/welcome.json "config"
fi
echo "$0: Stopping Unit daemon after initial configuration..."
kill -TERM $(/bin/cat /var/run/unit.pid)
@@ -89,9 +95,6 @@ if [ "$1" = "unitd" ] || [ "$1" = "unitd-debug" ]; then
echo
echo "$0: Unit initial configuration complete; ready for start up..."
echo
else
echo "$0: /docker-entrypoint.d/ is empty, skipping initial configuration..."
fi
fi
fi

View File

@@ -75,9 +75,10 @@ RUN set -ex \
&& ln -sf /dev/stdout /var/log/unit.log
COPY docker-entrypoint.sh /usr/local/bin/
COPY welcome.* /usr/share/unit/welcome/
STOPSIGNAL SIGTERM
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
EXPOSE 80
CMD ["unitd", "--no-daemon", "--control", "unix:/var/run/control.unit.sock"]

45
pkg/docker/welcome.html Normal file
View File

@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to NGINX Unit</title>
<style type="text/css">
body { background: white; color: black; font-family: sans-serif; margin: 2em; line-height: 1.5; }
h1,h2 { color: #00974d; }
li { margin-bottom: 0.5em; }
pre { background-color: beige; padding: 0.4em; }
hr { margin-top: 2em; border: 1px solid #00974d; }
.indent { margin-left: 1.5em; }
</style>
</head>
<body>
<h1>Welcome to NGINX Unit</h1>
<p>Congratulations! NGINX Unit is installed and running.</p>
<h3>Useful Links</h3>
<ul>
<li><b><a href="https://unit.nginx.org/configuration/?referer=welcome&platform=docker">https://unit.nginx.org/configuration/</a></b><br>
To get started with Unit, see the <em>Configuration</em> docs, starting with
the <em>Quick Start</em> guide.</li>
<li><b><a href="https://unit.nginx.org/howto/docker/?referer=welcome&platform=docker">https://unit.nginx.org/howto/docker/</a></b><br>
For guidance about running <em>Unit in Docker</em> and tips for containerized
applications.
<li><b><a href="https://github.com/nginx/unit">https://github.com/nginx/unit</a></b><br>
See our GitHub repo to browse the code, contribute, or seek help from the
<a href="https://github.com/nginx/unit#community">community</a>.</li>
</ul>
<h2>Next steps</h2>
<h3>Check Current Configuration</h3>
<div class="indent">
<p>Unit's control API is currently listening for configuration changes
on the <a href="https://en.wikipedia.org/wiki/Unix_domain_socket">Unix socket</a> at
<b>/var/run/control.unit.sock</b> inside the container.<br>
To see the current configuration run:</p>
<pre>docker exec -ti <containerID> curl --unix-socket /var/run/control.unit.sock http://localhost/config</pre>
</div>
<hr>
<p><a href="https://unit.nginx.org/?referer=welcome&platform=docker">NGINX Unit &mdash; the universal web app server</a><br>
NGINX, Inc. &copy; 2023</p>
</body>
</html>

25
pkg/docker/welcome.json Normal file
View File

@@ -0,0 +1,25 @@
{
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"headers": {
"accept": "*text/html*"
}
},
"action": {
"share": "/usr/share/unit/welcome/welcome.html"
}
},
{
"action": {
"share": "/usr/share/unit/welcome/welcome.md"
}
}
]
}

29
pkg/docker/welcome.md Normal file
View File

@@ -0,0 +1,29 @@
Welcome to NGINX Unit
=====================
Congratulations! NGINX Unit is installed and running.
Useful Links
------------
* https://unit.nginx.org/
- Get started with the 'Configuration' docs, starting with the 'Quick Start' guide.
* https://unit.nginx.org/howto/docker/
- Guidance for running Unit in a container and tips for containerized applications.
* https://github.com/nginx/unit
- See our GitHub repo to browse the code, contribute, or seek help from the community.
Current Configuration
---------------------
Unit's control API is currently listening for configuration changes on the Unix socket at
`/var/run/control.unit.sock` inside the container.
Read the current configuration with
```
docker exec -ti <containerID> curl --unix-socket /var/run/control.unit.sock http://localhost/config
```
---
NGINX Unit - the universal web app server