编程知识 cdmana.com

Deploying springboot application with docker Maven plugin

Recently, I found another easy to use Maven plug-in unit ,fabric8io Produced docker-maven-plugin, You can put SpringBoot The application is easy to deploy to Docker In the container . The plug-in can implement packaging image 、 Push to image warehouse 、 Running an application, etc , This article will introduce its usage in detail , I hope that's helpful !

Install private mirror Repository Because then we need to push it to the private image warehouse , We pre installed , It uses Docker Private image Warehouse developed by the company Registry.

download Registry Of Docker Mirror image ; docker pull registry:2 Use Docker Container operation Registry service , You need to add environment variables REGISTRY_STORAGE_DELETE_ENABLED=true Turn on the delete image function ; docker run -p 5000:5000 --name registry2
--restart=always
-e REGISTRY_STORAGE_DELETE_ENABLED="true"
-d registry:2 modify Docker Daemon Configuration file for , The file location is /etc/docker/daemon.json, because Docker By default HTTPS Push the mirror , And our mirror repository doesn't support , So you need to add the following configuration , Change to use HTTP push ; { "insecure-registries": ["192.168.3.101:5000"] } Finally use the following command to restart Docker service . systemctl daemon-reload && systemctl restart docker Image warehouse Visualization Because private mirror warehouse management is more troublesome , and docker-registry-ui There are special pages to manage images easily , So we installed it to manage the private mirror Repository .

download docker-registry-ui Of Docker Mirror image ; docker pull joxit/docker-registry-ui:static Use Docker Container operation docker-registry-ui service ; docker run -p 8280:80 --name registry-ui
--link registry2:registry2
-e REGISTRY_URL="http://registry2:5000"
-e DELETE_IMAGES="true"
-e REGISTRY_TITLE="Registry2"
-d joxit/docker-registry-ui:static Let's try to see if private mirror repositories are available , First download a test image busybox; docker pull busybox To mirror busybox Label it a private warehouse , And set the version to v1.0; docker tag busybox 192.168.3.101:5000/busybox:v1.0 Then push it to the private image warehouse ; docker push 192.168.3.101:5000/busybox:v1.0 visit docker-registry-ui Management interface , You can see busybox Mirror image , Address :http://192.168.3.101:8280

The plug-in USES fabric8io Produced docker-maven-plugin It's a collection Docker Image management and container management in one plug-in , You can take our SpringBoot Application deployment to Docker In the container , Very easy to use , Let's talk about its usage .

stay IDEA Use... Correctly in Maven plug-in unit Generally, if we don't use IDEA, It's all hand tapping Maven Command to execute , stay IDEA We just double-click on the right panel Maven Orders can be executed , Very convenient .

If you want to use custom commands , have access to Execute Maven Goal This function , What I'm using here is mvn clean package command .

Build a mirror image Want to use docker-maven-plugin, Need to be in pom.xml Add the plug-in in ; <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.33.0</version> <configuration> <!-- Docker Remote management address --> <dockerHost>http://192.168.3.101:2375</dockerHost> <!-- Docker Push image warehouse address --> <pushRegistry>http://192.168.3.101:5000</pushRegistry> <images> <image> <!-- Due to push to private image warehouse , Image name needs to add warehouse address --> <name>192.168.3.101:5000/mall-tiny/${project.name}:${project.version}</name> <!-- Define mirror building behavior --> <build> <!-- Define the base image --> <from>java:8</from> <args> <JAR_FILE>${project.build.finalName}.jar</JAR_FILE> </args> <!-- Define which files are copied to the container --> <assembly> <!-- Define the directory to copy to the container --> <targetDir>/</targetDir> <!-- Only copy generated jar package --> <descriptorRef>artifact</descriptorRef> </assembly> <!-- Define the container start command --> <entryPoint>["java", "-jar","/${project.build.finalName}.jar"]</entryPoint> <!-- Define the maintainer --> <maintainer>macrozheng</maintainer> </build> </image> </images> </configuration> </plugin> </plugins> </build> We need to package the project before we build the image , And then build , Otherwise it will go wrong , Use the following command directly ; mvn package docker:build After packaging, we can see this image on our server ; [root@linux-local mydata]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.3.101:5000/mall-tiny/mall-tiny-fabric 0.0.1-SNAPSHOT 6b8bc6faeb0b 9 seconds ago 680MB Of course, we can also set up to use package Package the image directly when you command it , modify pom.xml, stay <plugin> Add under node <executions> The configuration can be ; <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.33.0</version> <executions> <!-- If you want to build an image when the project is packaged, add --> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin> Use different Maven Plug in build Docker Mirror images are often different , In this case, use it directly Dockerfile It would be better to build , Let's start with Dockerfile File and put it in the project root directory ;

This image needs a dependent base image

FROM java:8

Will the current maven Directory generated files are copied to docker Container of / Under the table of contents

COPY maven /

Declare that the service is running at 8080 port

EXPOSE 8080

Appoint docker Run when container starts jar package

ENTRYPOINT ["java", "-jar","/mall-tiny-fabric-0.0.1-SNAPSHOT.jar"]

Specify the name of the maintainer

MAINTAINER macrozheng And then modify pom.xml file , take <build> The node configuration is replaced with the following , Just configure Dockerfile It's in the directory . <build> <dockerFileDir>${project.basedir}</dockerFileDir> </build> Push to image warehouse So let's use docker:push The command can push the image to the private image warehouse ; mvn docker:push After that, we can see the image in our private image warehouse ;

Container operation docker-maven-plugin Not only can you manipulate mirrors , You can also operate containers , For example, we used to use the following Docker Command to run the container ; docker run -p 8080:8080 --name mall-tiny-fabric
--link mysql:db
-v /etc/localtime:/etc/localtime
-v /mydata/app/mall-tiny-fabric/logs:/var/logs
-d 192.168.3.101:5000/mall-tiny/mall-tiny-fabric:0.0.1-SNAPSHOT Now we just need to configure it in the plug-in , stay <image> Add under node <run> Nodes can define the behavior of container initiation :

<!-- Define container startup behavior -->

<run> <!-- Set container name , You can use wildcards --> <containerNamePattern>${project.artifactId}</containerNamePattern> <!-- Set port mapping --> <ports> <port>8080:8080</port> </ports> <!-- Set up connections between containers --> <links> <link>mysql:db</link> </links> <!-- Set up container and Host Directory mount --> <volumes> <bind> <volume>/etc/localtime:/etc/localtime</volume> <volume>/mydata/app/${project.artifactId}/logs:/var/logs</volume> </bind> </volumes> </run> Then use it directly docker:start The command will start ; mvn docker:start [root@linux-local mydata]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95ce77c0394b 192.168.3.101:5000/mall-tiny/mall-tiny-fabric:0.0.1-SNAPSHOT "java -jar /mall-tin…" 32 seconds ago Up 31 seconds 0.0.0.0:8080->8080/tcp mall-tiny-fabric Stop using the container docker:stop Command is enough ; mvn docker:stop Delete container use docker:remove command , Is it convenient ! mvn docker:remove Project source address https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-fabric

版权声明
本文为[North of the Arctic]所创,转载请带上原文链接,感谢

Scroll to Top