With the release of Docker CE 17.05 (EE 17.06), it introduced the world of developers to help create thin docker images by diving the image building process into multiple stages, or what you can call it - multi-stage builds.
Using the Docker multi-stage build you can now reuse the artifacts that are produced in one stage by another stage. The ultimate benefit of this feature is that it helps to create smaller images. Previously, building a Docker image for a Java app was difficult as it involved building the entire application and then package the generated artifact into an image.
The java developers have to compulsorily use Maven or Gradle to build a JAR or WAR file. Using the Maven base image, it requires to download the dependencies from the configured repositories to keep them in the image for building the application. The number of JARs in the local repository mainly depends on the number of pom.xml dependencies.
Using the old way of building a Dockerfile created several issues:
• By using Maven you can restrict on what functionality is available in the image. To do so, you are required to download the WildFly and configure it explicitly.
download the WildFly and configure it explicitly.download the WildFly and configure it explicitly.
• All maven dependencies are downloaded when you build the artifact that stays in the image and causes unnecessary bloat in the image size during runtime.
• After the image is created, unit tests run before packaging the artifact and integration tests which do not require test dependencies to live in the production image.
• Multiple Dockerfiles need to maintained separately when you split the main Dockerfile into two different ones.
Let us look at how these issues can be resolved using the multi-stage build.
What is Docker Multi-Stage Build?
First, Recall what is a Docker?
A Docker is a containerization technology that allows to create and utilize Linux containers. It allows you to package and isolate applications with their complete runtime environment as well as containerized applications are a lot easier to move from one environment to another along with retaining all of their functions. Such containerized applications can be transformed into a docker image for easy sharing.
Apart from this, the multi-stage build allows multiple FROM statements in a Dockerfile as the instructions follow each FROM statement to create an intermediate image. The last or final FROM statement is the final base image. You can copy the artifacts from the intermediate stages starting from 0 for the first base image as - COPY --from=. The artifacts which re left uncovered gets discarded.
Advantages of Docker Multi-Stage Build:
• Just one Dockerfile is needed to build the entire process and does not require to have a separate Dockerfiles to coordinate the transfer os artifact between 'build' and 'run' using volume mapping.
• To meet the runtime needs, the base image for the final image needs to be chosen appropriately which helps to reduce the overall size of the runtime image.
• Instead of downloading and configuring the distribution manually, the standard WildFly base image is used to make it a lot easier to update the image every time a newer tag is released.
Before you go…
In this article, we have summarized a quick guide on the Docker multi-stage build using Java development services. It allows producing a production-grade Docker image by simply using a single Dockerfile and also lets you know how to start an ephemeral Postgres container. Lastly, you need to call the docker build command using all the required arguments for Makefile. You can easily get the Dockerfile and Makefile for any application on GitHub. Happy coding!
Top comments (0)