docker-maven-plugin
<build>
<!-- docker 中使用 -->
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- docker 打包 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.3</version>
<configuration>
<imageName>${project.name}:${project.version}</imageName>
<imageTags>
<imageTag>${project.version}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<skipDockerBuild>false</skipDockerBuild>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<!--直接在package的时候执行docker build-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>package</phase>-->
<!--<goals>-->
<!--<goal>build</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
</plugin>
</plugins>
</build>
指定了jar的名称为app
指定了docker镜像名为${project.name}:${project.version}
Springboot实例
@SpringBootApplication
@RestController
public class SpringbootDockerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDockerDemoApplication.class, args);
}
@RequestMapping("/")
public String index(){
return "this is index";
}
}
docker文件
在src/main/docker文件夹下面建立Dockerfile
FROM java:8
# time zone
RUN echo "Asia/Chongqing" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata
RUN mkdir /app
RUN mkdir /app/logs
ADD app.jar /app/app.jar
ADD runboot.sh /app/
RUN bash -c 'touch /app/app.jar'
WORKDIR /app
RUN chmod a+x runboot.sh
CMD /app/runboot.sh
runboot.sh的内容
sleep 90
java -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar
也可以直接在Dockerfile中用ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
设置log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<properties>
<!--定义LOG存储位置-->
<property name="LOG_HOME">/app/logs</property>
<!--定义应用名称-->
<property name="APP_NAME">springboot-docker-demo</property>
</properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${APP_NAME} %-d{yyyy-MM-dd HH:mm:ss} [%-5p] (%c{1}:%L) %m%n"/>
</Console>
<RollingRandomAccessFile name="ROLLFILE"
fileName="${LOG_HOME}/${APP_NAME}.log"
filePattern="${LOG_HOME}/${APP_NAME}.%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="${APP_NAME} %-d{yyyy-MM-dd HH:mm:ss} [%-5p] (%c{1}:%L) %m%n"/>
<Policies>
<!--每隔1小时分割,modulate为true表示以0分为边界-->
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingRandomAccessFile>
<!--async appender-->
<Async name="Async">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLFILE"/>
</Async>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Async"/>
</Root>
</Loggers>
</Configuration>
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--use log4j2 instead -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
build镜像
mvn package -Dskip.test=true
mvn docker:build
docker run -d -p 8080:8080 -v /tmp/docker-logs:/app/logs springboot-docker-demo
http://{docker-machine-ip}:8080/
查看日志
docker-machine ssh default
cd /tmp/docker-logs
tail -f springboot-docker-demo.log
工程github:springboot-docker-demo
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。