准备环境:
docker
jdk
maven
配置docker:
- 为加快拉取镜像速度,建议设置docker国内镜像源
# 创建或修改 /etc/docker/daemon.json 文件,修改为如下形式
{
"registry-mirrors" : [
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com",
"https://cr.console.aliyun.com/"
]
}
# 重启docker服务使配置生效
$ systemctl restart docker.service
- 国内加速地址有:
Docker中国区官方镜像:
https://registry.docker-cn.com网易:
http://hub-mirror.c.163.comustc:
https://docker.mirrors.ustc.edu.cn
新建并配置springboot项目:
1、新建springboot项目
2、pom.xml 增加docker依赖
<properties>
<docker.image.prefix>test-docker</docker.image.prefix>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.server.cloud.zipkin.ZipkinServerApplication</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<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>
</configuration>
</plugin>
<!-- docker plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<!-- Dockerfile的地址 -->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
3、新增 src/main/docker/Dockerfile 文件
# 指定基础镜像
FROM openjdk:8-jdk-alpine
# 维护者信息
MAINTAINER dbg
# 用于指定持久化目录
VOLUME /tmp
# 将本地文件添加到容器中
ADD zipkin-server-1.0.0-SNAPSHOT.jar app.jar
# 指定于外界交互的端口
EXPOSE 9411
# 配置容器,使其可执行化
ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar
到这里 Spring Boot 项目添加 Docker 依赖就完成了
mvn打包及生成docker镜像
1、进入项目路径下进行打包测试:
#mvn打包
mvn package
#jar启动测试
java -jar target/zipkin-server-1.0.0-SNAPSHOT.jar
2、看到 Spring Boot 的启动日志后表明环境配置没有问题,接下来我们使用 DockerFile 构建镜像:
# mvn打包并构建docker镜像
mvn clean package docker:build
- 第一次构建需要拉取基础镜像可能会有点慢,耐心等待,当你看到如下信息的时候说明构建成功:
...
Step 1/5 : FROM openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/5 : VOLUME /tmp
---> Using cache
---> d54a9bb20fac
Step 3/5 : ADD zipkin-server-1.0.0-SNAPSHOT.jar app.jar
---> 9353e6537a62
Step 4/5 : EXPOSE 9411
---> Running in 84034216b57b
Removing intermediate container 84034216b57b
---> 28272bf1375f
Step 5/5 : ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar
---> Running in 05813ce8ba7d
Removing intermediate container 05813ce8ba7d
---> 83dfc1c6753a
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 83dfc1c6753a
Successfully tagged test-docker/zipkin-server:latest
[INFO] Built test-docker/zipkin-server
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.412 s
[INFO] Finished at: 2019-08-08T16:51:52+08:00
[INFO] ------------------------------------------------------------------------
- 默认构建好的镜像在本机docker中:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test-docker/zipkin-server latest 83dfc1c6753a 5 minutes ago 135MB
- 启动
docker run -d -e JAVA_OPTS='-server -Xms512m -Xmx512m' -p 9411:9411 --name zipkin -t test-docker/zipkin-server
- 查看启动的容器
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
514931989a7b c2801d3d2b96 "/bin/sh -c 'exec ja…" About an hour ago Up 54 seconds 0.0.0.0:9411->9411/tcp zipkin
- 可以看到我们构建的容器正在在运行,访问浏览器:http://localhost:9411
so 到这里 使用Docker部署SpringBoot项目 就成功了!😊