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 Dockerfile

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



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.

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 Software Consultant having experience of more than 1.5 years . She is a Java enthusiast and has the knowledge of various programming languages. She is familiar with Object Oriented Programming Paradigms. She is always eager to learn new and advance concepts in order to expand her horizon and apply them in project development with her existing knowledge.Her hobbies include Coding , Writing technical blogs , Watching Reality Shows in spare time.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!