I have a docker container running uhttpd and serving a static HTML page. How can I dynamically insert the hostname of the container into the static HTML page? I want to keep the container as lightweight as possible.
4 Answers
Why not just have a command that runs as part of the container's startup that generates the static HTML page with the hostname within it.
$ cat <<EOF > /path/to/var/www/hostname.html
<html>
<body>
<p>hostname is: $(hostname)</p>
</body>
</html>
EOF
This command can be placed in /etc/rc.d/rc.local assuming you're using a SysV style of startup scripts. If you're using systemd you can also do the same, but you'll need to enable the service:
$ sudo service rc-local start
This will mark it to run, to make it run per startup:
$ sudo systemctl enable rc-local
If you're using something else, such as Upstart, there are equivalent methods for doing the same things above.
References
- 363,520
- 117
- 767
- 871
-
Even though I ended up using Apache and PHP, if I had stuck with uhttpd and no PHP I would have done exactly what you suggested. Thanks – Nov 06 '14 at 04:04
If you are using PHP, you can use the following code:
echo system('hostname')
This will echo the output of the command hostname. Please note that the command system is disabled on many shared hosts for security reasons.
Alternatively, you can use:
echo gethostname();
-or-
echo php_uname('n')
For a static page, the easiest way would be to generate the served page from a template replacing a placeholder with whatever the hostname command gives you. Execute that code in the entrypoint of your image.
- 129
- 2
-
Welcome to the U&L Stack Exchange. The current phrasing of the answer is too vague. A fuller explanation on how to configure uHTTPd to achieve solution suggested would improve the answer considerably. See also the [help center article on how to write good answers](https://unix.stackexchange.com/help/how-to-answer). – Thomas Nyman Sep 21 '14 at 07:53
The following is a webpage that would display the hostname. This shows the function 'inline' for web programming.
<html>
<body>
<?php echo gethostname(); ?>
</body>
</html>
- 11
- 1