In the daily development process, the project will be packaged and run on the server. In this article, the method of using docker is introduced to push the local jar package to the docker private server and run it on the remote server.
step
- Create a new maven project, the directory structure is as follows:
- Introduce the maven compilation and packaging plugin and docker packaging plugin into the pom file
2.1.
The profile.name under the profile in the pom file of the multi-environment configuration must be consistent with the **{env}** in the application-{env}
2.2. Maven compilation Package the plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${
maven-compiler-plugin.version}</version>
<configuration>
<compilerArguments>
<bootclasspath>${
env.JAVA_HOME}\jre\lib\rt.jar;${
env.JAVA_HOME}\jre\lib\jce.jar</bootclasspath>
</compilerArguments>
<source>${
java.version}</source>
<target>${
java.version}</target>
<encoding>${
maven.compiler.encoding}</encoding>
<verbose/>
</configuration>
</plugin>
<plugin>
<!--打包跳过测试-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${
maven-surefire-plugin.version}</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${
spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${
maven-resources-plugin.version}</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>woff</nonFilteredFileExtension>
<nonFilteredFileExtension>eot</nonFilteredFileExtension>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
<nonFilteredFileExtension>svg</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
2.3. docker package plugin configuration
<!-- docker打包插件 -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.33.0</version>
<configuration>
<verbose>true</verbose>
<!-- docker远程管理url -->
<dockerHost>tcp://ip:2375</dockerHost>
<registry>registry.cn-hangzhou.aliyuncs.com</registry>
<authConfig>
<!-- docker私服账户密码 -->
<push>
<username>zfl_aliases@163.com</username>
<password>123456</password>
</push>
</authConfig>
<buildArgs>
<JAR_FILE>${
project.build.finalName}.jar</JAR_FILE>
</buildArgs>
<images>
<image>
<!-- 镜像名称 命名空间/仓库名称:镜像版本号 -->
<name>${
docker.image.prefix}/${
project.artifactId}:${
project.version}</name>
<build>
<dockerFile>${
project.basedir}/src/main/docker/Dockerfile</dockerFile>
<assembly>
<name>/</name>
<!-- artifact是预定义的值,表示将项目打包后的jar拷贝到编译上下文中,便于Dockerfile ADD指令 -->
<descriptorRef>artifact</descriptorRef>
</assembly>
</build>
<run>
<!-- 运行时容器名称 -->
<containerNamePattern>${
project.artifactId}</containerNamePattern>
<extraHosts>
<extraHost>服务器ip</extraHost>
</extraHosts>
</run>
</image>
</images>
</configuration>
</plugin>
- Write Dockerfile
// FROM 可根据实际情况来编写 openjdk:8
FROM registry.cn-hangzhou.aliyuncs.com/lee/jdk8-lee:1.0.0
MAINTAINER zhangfalu zfl_aliases@163.com 2021-01-25
ARG JAR_FILE
COPY ${
JAR_FILE} app.jar
EXPOSE 8063
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Duser.timezone=GMT+08", "-jar", "/app.jar"]
-
Packaging
command: mvn clean package -Dmaven.skip.tests=true -P online
View the packaged file information under classes
-
Local test
-
Put the local jar package as a mirror and push it to the remote private server, and run the docker container
Command: mvn docker:stop docker:remove docker:build docker:push docker:start
-
View private server
-
View the running status of the container
Use idea to integrate Docker to locally manage image generation, deletion, container start and stop, and avoid using commands. You can also better view the container running log information.
- Click File-Settings
- After filling in, click apply, there will be an additional Docker window under the development tool
- Click to open to connect to
open Containers, view the specified container, you can easily view the container log
Note: Project address