<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的名稱爲appjava
指定了docker鏡像名爲${project.name}:${project.version}git
@SpringBootApplication @RestController public class SpringbootDockerDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootDockerDemoApplication.class, args); } @RequestMapping("/") public String index(){ return "this is index"; } }
在src/main/docker文件夾下面創建Dockerfilegithub
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的內容web
sleep 90 java -Djava.security.egd=file:/dev/./urandom -jar /app/app.jar
也能夠直接在Dockerfile中用ENTRYPOINTspring
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
<?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>
添加依賴docker
<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>
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-demoshell