Dockerize Your Java Application

Container Technologies (especially Docker) are one of the hottest trends for deploying apps right now. This story will explain how to deploy Java application using Docker, in a very simple case. For those who are not familiar with container technologies, you can look at this story for introduction.

Without any further ado, let me explain my 5 simple steps 🙂.

1. Install Your Docker

Install your favorite version of docker (if you don’t have it yet), you can follow this instruction.

2. Coding Your Application

Next, you can just coding, test, and build your application as usual. For example, I made a simple http API with Spring Boot.

Actually for Spring Application, start from Boot 2.3.0, this is official best practice building Docker Images for minimizing trade off. But I will still use Spring Boot for this example to make it simple.

For example, I made a simple http API with spring boot.

3. Write Your Dockerfile

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. [source]

Create a file (name it Dockerfile) in the root project directory, for example, if you use maven, you can put the file together with pom.xml.

Next, write some code in your Dockerfile

First line, FROM openjdk:8u181 means that our image will be built on the top of openjdk image version 8u181. There are various version provided, you can check Docker Hub, or you can use Oracle Server JRE (with smaller footprint).

Second line, ADD target/docker-example-0.0.1-SNAPSHOT.jar /opt/docker-example-0.0.1-SNAPSHOT.jar means we take the docker-example-0.0.1-SNAPSHOT.jar (generated file from the maven build process) and put it in the container image in /opt directory.

Third line, ENTRYPOINT set the commands and parameters that will be executed first when a container is run. If there are other options (e.g Xms, Xmx), we can add here.

For more info about Dockerfile please see official docs.

4. Build Your Image

If you don’t have a docker machine, you need to install docker-machine first. Actually you can just simply download the latest binary release and move it to bin directory (for unix-like system). And next create new machine, if you use virtualbox driver, you can run this command below.

docker-machine create --driver virtualbox docker-vbox && docker-machine env docker-vbox

Next, you can build your image using docker build command.

docker build -t YOURIMAGENAME:VERSION .

For example…

docker build -t ard333.com/docker-example:0.0.1-SNAPSHOT .

Before you run the command above, you must be in the root project directory, and don’t forget the dot at the end of the command, that means the build process is done in the current directory, the directory with Dockerfile in it. After the process is complete, you can see your image in your local repository.

docker images

You can push your image to remote repository like Docker Hub, Public Cloud Container Registry (e.g. GCP, AWS), or your own Private Docker Registry using docker push command.

5. Run Your Container

Next, you can run your container by running the command below.

docker run --name my-docker-example -p 9999:8080 -d ard333.com/docker-example

-p option means you publish a container port (mine is 8080) to the host (I chose 9999). -d means you are running container in background. For more info about docker run command, please see official docs.

You can see a list of running container with the docker ps command.

docker ps

If your container STATUS is up, you can check whether the application is running properly.

Done 👍

I hope this story can be a good introduction for those who just starting out using Java and Container Technology. Thanks for reading (Sorry For My Bad English 😅) and feel free to comment.

In the next story, I will write about Spring Cloud Kubernetes, because a few weeks ago that project has been graduated from the incubation process.

Are you looking for information about remote work?
or have a cool resource about remote work?
remotework.FYI is all you need to know about remote work, find and share cool resources right now.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store