1. Introduction

As a Java developer I want to build a lean Docker image for my java app. A Docker feature named "multi-stage builds" make this process so easy and straight forward.

2. Sample Project

As a simple sample project we have a Spring-Boot hello world project which responses on http://localhost:8080 You can provide your desired project or clone/download the github repository from here:

3. Dockerfile

In the root of the project there would be a file named Dockerfile containing below listing:

4. How to run

  1. Docker must be installed on your system
  2. In terminal go to root of the project and run below command for building docker image:
  3. * Please notice that there is a dot(.) at the end of above command.
  4. In terminal run below commands for running the container:
  5. Check your application in browser: Go to browser and browse into http:\\localhost:8080
  6. Check your docker container: Go to terminal and run below command:

5. Next

Below, a couple of points are mentioned which are not described here to keep this article simple.

  • Use -v option of docker for mounting your local maven repository and prevent downloading maven dependencies in build repetitive phase.
  • Use Maven plugins for building images instead of direct docker CLI commands.
  • Push docker image into a docker hub to make it accessible from hosts outer of your local.

Leave a Reply

Your email address will not be published. Required fields are marked *