Spring Boot Docker
1. IDEA中配置Docker
Docker默认只接受本地客户端的请求,为了能够远程访问它,首先要开放Docker的监听端口,允许外部应用可以访问
修改 /lib/systemd/system/docker.service 文件
找到 ExecStart
在后面追加 -H tcp://0.0.0.0:2375
然后重启Docker
接下来就可以在idea中连接docker了
这样就不用登录到服务器上看了,直接在idea中查看即可,而且还有一个好处是,在idea中可以看到容器的日志
2. Docker安装Redis
https://registry.hub.docker.com/_/redis
docker pull redis
docker run --name my-redis -p 6380:6379 -d redis
这里由于宿主机上已经运行了一个redis并占用6379端口,所以在docker容器里面这个redis服务就映射到6380端口了
接下来,用redis-cli连接试一下
docker exec -it my-redis /bin/bash
或者
/宿主机Redis安装目录/src/redis-cli -p 6380
用上面的方式直接启动redis的话,采用的是默认配置,默认配置下只允许本机连接,无法远程连接。通常情况下,我们会指定自己的配置文件(此处redis.conf配置文件中 daemonize 设置为 no)
docker run -v /usr/local/conf:/usr/local/etc/redis --name myredis3 -p 6381:6381 -d redis redis-server /usr/local/etc/redis/redis.conf
3. 使用 docker-maven-plugin 构建镜像
https://github.com/spotify/docker-maven-plugin
pom.xml中引入docker-maven-plugin插件
第一种方式:在pom文件中指定构建信息
1 <build>
2 <plugins>
3 <plugin>
4 <groupId>org.springframework.boot</groupId>
5 <artifactId>spring-boot-maven-plugin</artifactId>
6 </plugin>
7
8 <plugin>
9 <groupId>com.spotify</groupId>
10 <artifactId>docker-maven-plugin</artifactId>
11 <version>1.2.2</version>
12 <configuration>
13 <imageName>example</imageName>
14 <dockerHost>http://192.168.28.31:2375</dockerHost>
15 <baseImage>java</baseImage>
16 <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
17 <resources>
18 <resource>
19 <targetPath>/</targetPath>
20 <directory>${project.build.directory}</directory>
21 <include>${project.build.finalName}.jar</include>
22 </resource>
23 </resources>
24 </configuration>
25 </plugin>
26 </plugins>
27 </build>
构建成功了,但是这个镜像太太太大了
咱们不用 java:8 ,换成 openjdk 试试
https://registry.hub.docker.com/_/openjdk
1 <build>
2 <plugins>
3 <plugin>
4 <groupId>org.springframework.boot</groupId>
5 <artifactId>spring-boot-maven-plugin</artifactId>
6 </plugin>
7
8 <plugin>
9 <groupId>com.spotify</groupId>
10 <artifactId>docker-maven-plugin</artifactId>
11 <version>1.2.2</version>
12 <configuration>
13 <imageName>cjs/${project.artifactId}</imageName>
14 <dockerHost>http://192.168.28.31:2375</dockerHost>
15 <!--
16 <baseImage>java:8</baseImage>
17 <baseImage>openjdk:8</baseImage>
18 <baseImage>openjdk:8-jre</baseImage>
19 <baseImage>openjdk:8-alpine</baseImage>
20 -->
21 <baseImage>openjdk:8-jre-alpine</baseImage>
22 <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
23 <resources>
24 <resource>
25 <targetPath>/</targetPath>
26 <directory>${project.build.directory}</directory>
27 <include>${project.build.finalName}.jar</include>
28 </resource>
29 </resources>
30 </configuration>
31 </plugin>
32 </plugins>
33 </build>
从结果来看,openjdk:8-jre-alpine 是最小的,openjdk:8-alpine 其次
第二种方式:指定Dockerfile构建
首先,创建一个Dockerfile文件
FROM openjdk:8-jdk-alpine
COPY hello-docker-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
然后,指定Dockerfile文件所在的位置
1 <build>
2 <plugins>
3 <plugin>
4 <groupId>org.springframework.boot</groupId>
5 <artifactId>spring-boot-maven-plugin</artifactId>
6 </plugin>
7
8 <plugin>
9 <groupId>com.spotify</groupId>
10 <artifactId>docker-maven-plugin</artifactId>
11 <version>1.2.2</version>
12 <configuration>
13 <imageName>cjs/${project.artifactId}</imageName>
14 <dockerHost>http://192.168.28.31:2375</dockerHost>
15 <dockerDirectory>src/main/docker</dockerDirectory>
16 <resources>
17 <resource>
18 <targetPath>/</targetPath>
19 <directory>${project.build.directory}</directory>
20 <include>${project.build.finalName}.jar</include>
21 </resource>
22 </resources>
23 <imageTags>
24 <imageTag>${project.version}</imageTag>
25 <imageTag>latest</imageTag>
26 </imageTags>
27 </configuration>
28 </plugin>
29 </plugins>
30 </build>
第三种方式:手动构建
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
命令行执行
docker build -t cjs/hello-docker .
运行的时候可以指定环境变量参数
docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8084:8083 -d cjs/hello-docker:0.0.2-SNAPSHOT
4. 备忘录
https://github.com/spotify/docker-maven-plugin
https://registry.hub.docker.com/_/openjdk
https://registry.hub.docker.com/_/redis
https://spring.io/guides/gs/spring-boot-docker/
https://spring.io/guides/topicals/spring-boot-docker