post

1. Story

  • As a Java developer I want to build a Docker image
  • I want to aquire a Docker feature named multi-stage builds
  • While that process I want to prevent downloading mvn dependecies through building docker image process
  • So I want to manually inject dependencies into Docker image

2. Build Docker image

    1. 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: https://github.com/AmirKeshavarz/hello-docker-java
    1. Create local Maven repo:
In terminal go to the project directory and run below command to download all maven dependencies and put them in a compressed file: * dependecy:go-offline cause all dependencies being downloaded * -Dmaven.repo.local=./repo makes a directory named repo and download all dependencies into it
    1. Create Dockerfile:
In project directory create a file named Dockerfile-injectrepo(Or it exists if you clone from our github) Note that in line 3 ADD command is injecting our pre downloaded maven repo into first stage of our Docker image.
    1. Build docker image:
For this step Docker must be installed on your system In terminal go to root of the project and run below command for building docker image: * Please notice that there is a dot(.) at the end of above command. In terminal run below command for having a test and run the container:
  1. Go to browser and browse into http:\localhost:8080
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 *