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.
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-
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
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.