Spring Boot Docker
开发环境
开发工具: Intellij IDEA 2018.2.6
springboot: 2.0.6.RELEASE
jdk: 1.8.0_192
maven: 3.6.0
docker: Docker Engine - Community 18.09.0
搭建项目
由于使用了 Spring Cloud 的Eureka所以加了 eureka 的依赖,基本不需要多加额外的配置
- pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-docker</artifactId>
<groupId>com.andy</groupId>
<version>1.0.7.RELEASE</version>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Cairo-SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<docker.image.prefix>andy</docker.image.prefix>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-docker</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<configuration>
<!--<mainClass>${start-class}</mainClass>-->
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<dockerDirectory>src/main/docker</dockerDirectory>
<baseImage>java</baseImage>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
- application.yml
server:
port: 8761
servlet:
path: /
spring:
application:
name: spring-eureka-docker
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka
- 启动类
/**
* <p>
*
* @author Leone
* @since 2018-03-06
**/
@EnableEurekaServer
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
- Dockerfile
在src/main/docker目录下(没有则创建此目录)创建Dockerfile文件然后把maven编译后的jar文件拷贝到和Dockerfile同级的目录
# 基于哪个镜像
FROM java:8
# 将本地文件夹挂载到当前容器
VOLUME /tmp
# 维护者信息
MAINTAINER leone "exklin@gmail.com"
# 拷贝文件到容器
ADD spring-docker.jar eureka.jar
# 执行命令
RUN bash -c 'touch eureka.jar'
# 声明端口
EXPOSE 8761
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-jar","-Djava.security.egd=file:/dev/./urandom","eureka.jar"]
安装 idea Docker 插件后需要在 windows 下安装 Docker 在docker 插件的 Deploy configuration 里面配置 docker 容器参数之后 点击Run后就会顺序执行 Dockerfile 里面的命令就会启动这个docker容器。打开浏览器访问http://localhost:8761,传送门