Serve Static Content on a Jetty Web Server using Docker Container

Reading Time: 2 minutes

Docker is well known to containerize the application with all the necessary dependencies in order to make the application portable and deployable in a platform independent manner. And, on the other hand Jetty Server is widely used with Docker to deploy applications using war files.
But, recently I came across a requirement to expose static files and jars on a given endpoint using a Jetty Web Server within a Docker Container.
So, in this blog we’ll explore how to serve the static content on a Jetty web server from a Docker Container in few simple steps:

Copy all the required static files(static content) to a directory.

// Create a directory.
mkdir static_content
// Copy all the files to this directory.
cp hello.html static_content/
cp hello.tar.gz static_content/
view raw copy.sh hosted with ❤ by GitHub


Create a Dockerfile
FROM alpine:latest
MAINTAINER NEHA BHARDWAJ
ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/jre \
PATH=$PATH:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin \
JAVA_VERSION=8u191
EXPOSE 8080
RUN apk add --update bash wget tar openjdk8-jre && rm -rf /var/cache/apk/*
# Copy Jars
COPY . /
RUN wget -q -O /jetty.tar.gz "https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.15.v20190215/jetty-distribution-9.4.15.v20190215.tar.gz"
# Install Jetty
RUN tar -xvf /jetty.tar.gz && rm /jetty.tar.gz && mv jetty-distribution-9.4.15.v20190215 /jetty
# Clean-Up
RUN chmod +x entrypoint.sh && apk del wget tar
ENTRYPOINT /entrypoint.sh
view raw Dockerfile hosted with ❤ by GitHub

Here, We've used apline:latest as the base image to minimize the size of the resultant docker image, but you can use any other image of your choice.

Define entrypoint.sh

#!/bin/sh
cp expose_static_content.xml /jetty/webapps/
# Optionally, copy or move the static content directory inside jetty to prevent any data loss
mv /static_content /jetty/static_content/
/jetty/bin/jetty.sh run
view raw entrypoint.sh hosted with ❤ by GitHub


Choose what to expose
Create an XML file to define the end point where the files will be served on the server and the Resources location where the files reside.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"&gt;
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="resourceBase">/jetty/static_content</Set>
<Set name="directoriesListed">true</Set>
</New>
</Set>
</Configure>

In the file above-
– The Context Path has been set to root, which results in serving all the files to the default path i.e, localhost:8080/
To set a custom path, Modify <Set name=”contextPath”>/</Set> with the required value.
Ex: <Set name=”contextPath”>/static/</Set>
– The tag <Set name=”resourceBase”>/jetty/static_content</Set> captures the resource location of the static content.

Ensure that this file is copied to the /jetty/webapps directory. Check entrypoint.sh for reference.
Once all the files are created, Verify that the parent directory structure is-
├── Dockerfile
├── entrypoint.sh
├── expose_static_content.xml
└── static_content
├── hello.html
└── hello.tar.gz

Build the docker image
docker build -t “jetty-server-docker” .

Initiate a docker container
docker run -p 8080:8080 jetty-server-docker:latest

Verify the server is running
Check localhost:8080/

All the static files must be present in the context path that you have specified in the exposeJar.xml file.

You can find the source code at https://github.com/nehabhardwaj01/http-server-docker
That’s all for this quick session of exposing files using Jetty Server within Docker Container. Hope you liked the blog, Please leave comments for any discussion.
Thank you!!

Written by 

Neha is a Senior Software Consultant with an experience of more than 3 years. She is a Big data enthusiast and knows various programming languages including Scala and Java. She is always eager to learn new and advance concepts to expand her horizons and apply them in the project development.