The goal of this post is to show how to get a Java RESTful API application (based on Jersey framework) into a Docker container. This guide assumes you have Docker, Java and Maven installed.
Create Jersey application
We’ll use Maven archetype to create demo project using Jersey RESTful framework. This project uses Grizzly HTTP server, so this is lightweight alternative to running full container.
After that we need to install dependencies and try to run this demo app.
Let’s test how it works by sending GET request with curl
. If everything works
fine server should respond with “Got it!” text message.
Create Dockerfile
File called Dockerfile
works as an instruction for building and running your Docker
image. For our demo project it will:
- define base image (with maven and jdk on-board)
- create application directory
- install dependencies using Maven
- expose local container port 8080
- run application
Full Dockerfile content (also available on github).
Update server address
Another step is necessary before we can start building and running Docker
containers. By default Jersey application listens for incoming request on the
“localhost”. This is defined by BASE_URI
in
jersey-docker-demo/src/main/java/com/dekses/jersey/docker/demo/Main.java file
on line 16.
This is not going to work well in Docker container because network interface created inside container and available for external requests is different. That’s why we need to instruct Jersey to listen on all available addresses, to do that replace “localhost” with “0.0.0.0”.
Build docker image
This done with docker build
command. Run it in your project directory.
Additional -t
flag allows to tag your image so it’s easier to find and run
later.
Run the image
Running your image with -t -i
options allows executed process to run
attached to your terminal. This is development mode options, they allow you to
see output from your image immediately.
Option -p
publishes or maps host system port 18080 to container port 8080,
otherwise you can not access your API service from the host system.
Image name is myapp
assigned during build phase.
Okay we can now test docker container with the same curl command but directed to port 18080 instead of 8080 for local Java app.
Hope this tutorial helped you get up and running a simple Java API as a Docker container. Full source code is uploaded to Github jersey-docker-demo project.