Introduction
Static Content
Static content refers to digital material that remains consistent for all users, regardless of their geographic location or the time they access it. This type of content is characterized by its unchanging nature, displaying identical information to every visitor without adapting to individual circumstances or environmental factors.
A key attribute of static content is its stability across various viewing contexts. Unlike dynamic content, which may adjust based on user preferences, time zones, or other variables, static content maintains a uniform presentation.
An illustrative example of static content is the website https://example.com. This site serves as a standard reference point, providing the same information to all visitors, irrespective of when or where they access it from.
Web Server for Static Content
So, to get static content accessible from the public web we'll need a web server or it can also be made accessible from S3 Object Storage Platforms like AWS S3, DigitalOcean Spaces, Google Cloud Object storage, etc. If you already have a server with Public IPV4/6 address then it's better to setup your own Web Server and host it.
Normally webservers like nginx, httpd, caddy, etc come bundled with certain features that are not required when serving static content. They come ready with all features neccessary to setup a stable production site but because we only need the features that allows us to host static content their resource footprint can be reduced extensively. For example:
Although 5 MB seems low we can reduce the size further down by creating a Web Server image from scratch with only the necessary features for serving static content. With this we can save a lot on image pull/push bandwidth as well as the storage space for said docker images. The little gains that the low image size provides will add up to a lot in the long run.
Now let's get started building it.
Pre-Requisites
- A machine capable of building docker images
- Docker Daemon
Building it
To do this, let's understand the process we'll go through first. Here's an image showing the basic constituents of our final container image.
The major component of our image will be the scratch image which is a vaccum image that we can use as our base image. Busybox is very essential to our build here because it houses some of the commonly used unix tools and fortunately includes httpd as well. So we'll simply enable httpd and required features in busybox and use the compiled binary as our webserver.
Our final build will consist of the binary as well as an example static file which will be displayed upon visiting the Web Server's address.
Configuring Busybox
First configure busybox, follow the steps below.
git clone git://busybox.net/busybox.git
cd busybox/
touch .config
make allnoconfig
A config file will now be generated, looks something like this.
This file is around 1200 lines long and adding it all here would be redundant, so I have only kept the parts important to us in this article.
Now, let's compile the binary,
make && make install
make -j && make install -j
This binary will be placed by default at _install/bin/busybox
Now test it by running the command below,
Check if the server is actually running by checking if the ports in use with the netstat -tulnp
command. If you run the busybox command again you will be greeted with a bind address already in use error.
If you try to access the Web Server at http://localhost:8980
a 404 page
will be displayed.
Creating the Docker Image
Now, let's create a Dockerfile
using the the steps we used above. We'll also require a httpd.conf
file, create an empty file in the directory.
touch httpd.conf
httpd.conf
file the container image will exit silently without during runtime without any errors or warning.Finally, build the static content server with the command below,
docker build -t static-image-server:v1 ./ -f Dockerfile
The final image size has come around to 199 KB
.
Also Read: Build your own Nginx Web Server
Serving Static Content
Now, to serve our static content we'll use this Docker image as our base image and copy our static files inside the container. After that we're good to go.
For this test create a example index.html
file.
Now, let's build it using the command,
Now, we run it using the following command,
Once the container is started, it should be accessible at http://localhost:8980
. One thing to note is that this container cannot be killed with the CTRL+C
TERM signal, as it does not handle the signal properly. You'll have to manually stop it using either docker stop container_id
or if you want it gone instantly, use docker kill container_id
.
Conclusion
In this article we've learned about static content, where it can be hosted, and how we can build our own content server with a very small storage footprint. It will definitely help conserve container registry traffic bandwidth in the long run.
Thank you for reading.