Introduction
Docker is a container platform to build and run applications within containers. These docker containers are very light weight compared to virtual machines and are standalone software packages which can run anywhere where docker engine installed. For detailed instructions on how to install docker see here.
To deploy a RESTful Java service onto a docker image we need to build an image with the following layers from bottom up.
Here are the required software packges need to be installed
- Operating System for docker container
- Java
- RESTful Service Application
Create Image
Now lets create an image with the above mentioned software.
Create a folder named docker-rest-image
. Under this folder create a file named Dockerfile
. Docker image builder tool (docker build
) will read this file to build the image. See here for more details about building docker images.
Operating System
For containers, the OS needs to be vary small in disk foot print and should be able to start very quickly. Alpine linux is a kind of that so lets use that for our demonstration.
Now add the following line to created Dockerfile
FROM alpine
Java Installlation
Now we have the base image so lets add Java to it so that our application can run using the installed Java.
Add the following statement to Dockerfile
.
RUN apk --update add openjdk8-jre
CMD ["export JAVA_HOME=`which java`"]
The RUN command will install the openjdk8-jre
onto this image and CMD sets the path.
Add RESTful Service
Until now we have the required software to run a java application, so lets add the RESTful java service to the image.
Im using a RESTful service developed using Dropwizard as its fully contained service jar which can run without any additional servlet containers.
Before adding a RESTful service we also need to expose the service port from the container. The EXPOSE command will expose the specified port.
EXPOSE 8080
Now lets add the service jar and/or any required configuration files on to the image.
The COPY commnd will copy the specified files from the Dockerfile
folder onto the image.
COPY configuration.yml /opt/app/
COPY rest-dropwizard-1.0-SNAPSHOT.jar /opt/app/
We need to set the /opt/app/
as the working directory so any ENTRYPOINT
commands will use that directory to execute OS commands.
WORKDIR /opt/app/
We have everything what we need to start the RESTful service. The ENTRYPOINT commands allows to execute the specified commands when a docker container starts like s startup application. So lets add the RESTful service as a startup application.
ENTRYPOINT exec java -jar rest-dropwizard-1.0-SNAPSHOT.jar server configuration.yml
Build Image
Lets build an image to make sure the image configuration in Dockerfile
looks good.
Run the docker build command to build the image. Before building make sure the RESTful service jars and configurations in the same folder of Dockerfile
or configure Dockerfile
to get them from a remote repository using RUN wget
command.
docker build -t docker-rest-image .
After a successful run we will see a message like the following and an image is created with specified name docker-rest-image
.
Step 7/8 : EXPOSE 8080
---> Running in 2c153d49565f
Removing intermediate container 2c153d49565f
---> ecd51f3f9a6a
Step 8/8 : ENTRYPOINT exec java -jar rest-dropwizard-1.0-SNAPSHOT.jar server configuration.yml
---> Running in bdac565e3756
Removing intermediate container bdac565e3756
---> b6e3b102dda8
Successfully built b6e3b102dda8
The docker images
will show the images created and we should see the one with docker-rest-image
.
docker-rest-image latest c99000cb6460 42 seconds ago 115MB
Run Image
The image is ready now. Lets run it. The docker run will start the container along with any optionally specified commands.
docker run -p 8080:8080 docker-rest-image
The above command will start the container and forward the port 8080 from container onto the host OS so you can hit the services using http://localhost:8080
.
References
- The source code can be found here
- Docker
- Dropwizard