Previously, we used Docker to build services related to the development environment, and also to build dependency services in the development and testing environment, and then also used Nexus to build Docker’s self-service, so this time we came together to deploy SpringBoot applications with Docker. Step by step to all the Dockerization march.
Build SpringBoot project
The project is relatively simple, no data interaction, no complex business, just a print statement.
Using any method you are familiar with, create a Maven project, modify the pom.xml file and add SpringBoot dependencies.
|
|
The following is the content of the Application file, including the Controller together with the definition.
|
|
At this point, execute the application, visit http://localhost:8080/
through your browser, and you will see the following sentence printed on the screen.
|
|
Using Docker to package images
Add Maven plug-ins to package Docker images through Maven commands, currently used more, there are three plug-ins.
- spotify/docker-maven-plugin: an early Maven plugin, you can not have DockerFile, all the parameters are configured in pom.xml, and then the author felt that this is not good, and wrote a new one, but also we choose this
dockerfile-maven-plugin
. - fabric8io/docker-maven-plugin: super powerful, but the use, but also some trouble, interested in trying.
- spotify/dockerfile-maven-plugin: new version of spotify/docker-maven-plugin, easier to use, write your own DockerFile, after that you can automatically package and publish.
Add Dockerfile
file to the project root directory
The content is relatively simple.
- Use OpenJdk as the base image
- Copy the jar package under Target as app.jar
- Execute the command
- Expose the port
Modify the plugins
section of the pom.xml configuration file
|
|
Introduce the new plugin in, the specific configuration can be found in https://github.com/spotify/dockerfile-maven
, where.
- username and password are the account and password of your Docker self-service, which can be ignored if your self-service is allowing anyone to upload
- repository is the address prefix of your server, plus the name of your Docker image
- tag is latest by default if not specified
After adding the above plugins, you can automatically generate the Docker image when you execute the package command again. If you don’t want this effect, you can comment out goal
.
|
|
According to the official statement, with this plugin, you don’t need to execute it step by step, just execute deploy
and you can publish it with one click, but I have some problems here, if it doesn’t work once, then execute it multiple times.
This will allow us to publish our SpringBoot application to the self-service repository. Pull this image from another environment or repull it yourself, run it and visit http://localhost:8080
and you will see the following.
|
|
Reasons to recommend
- Use a unified environment, packaged and ready to execute in any Docker
- Testers no longer need to install JDK and other such dependencies, get the image can be deployed directly, reducing the problems caused by the environment
- Combined with Jenkins for automatic deployment, making automatic build more convenient