一,前提条件
1,如果没有阿里云账号请注册阿里云账号,注册完成后在产品---镜像服务里创建镜像,创建完成后如下图所示:
2,本地(我这里是win10环境)已安装docker,如果没有可去官网下载,下载地址:https://docs.docker.com/docker-for-windows/install/
3,maven settings.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--本地仓库位置-->
<localRepository>D:\repository</localRepository>
<pluginGroups>
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
<proxies/>
<servers>
<server>
<!--maven的pom中可以根据这个id找到这个server节点的配置-->
<id>docker-aliyun</id>
<!--这里是在阿里云注册的账号-->
<username>abcd</username>
<!--这里是在阿里云注册的密码-->
<password>abcd123456</password>
<configuration>
<!--这是在阿里云注册时填写的邮箱-->
<email>adcd@qq.com</email>
</configuration>
</server>
</servers>
<mirrors>
<!--阿里云的镜像-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!--中央仓库在中国的位置-->
<mirror>
<id> maven-net-cn</id>
<name> Maven China Mirror</name>
<url> http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<!--配置jdk的版本-->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
二,springboot项目配置
1,创建Dockerfile文件
在项目的源文件路径下新建Dockerfile文件,Dockerfile文件是构建docker镜像的核心文件,配置如下:
#基于哪个镜像
FROM java:8
#将本地文件夹挂载到当前容器
VOLUME /tmp
#复制文件到容器
ADD gold-recycle-service.jar app.jar
#声明需要暴露的端口
EXPOSE 8086
#配置容器启动后执行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
2,pom.xml文件配置
pom.xml文件需要增加以下几点节点配置:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 监听端口 -->
<server.port>8086</server.port>
<!-- 镜像名称 docker.repostory表示仓库地址,docker.registry.name表示仓库名称。可以通过阿里云镜像查看-->
<docker.repostory>registry.cn-shenzhen.aliyuncs.com</docker.repostory>
<docker.registry.name>springios</docker.registry.name>
</properties>
<!--maven docker插件-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.14</version>
<configuration>
<imageName>registry.cn-shenzhen.aliyuncs.com/springios/${project.artifactId}:${project.version}</imageName>
<!--指定docker镜像的版本号-->
<imageTags>
<!--使用maven项目的版本号-->
<imageTag>${project.version}</imageTag>
<imageTag>latest</imageTag>
</imageTags>
<!--Dockerfile文件位置-->
<dockerDirectory>src/main/docker</dockerDirectory>
<!--指定推送的镜像-->
<dockerHost>http://127.0.0.1:2375</dockerHost>
<resources>
<resource>
<targetPath>/</targetPath>
<!--用于指定需要复制的目录-->
<directory>${project.build.directory}</directory>
<!--用于指定需要复制的文件-->
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!--推送阿里云相关-->
<serverId>docker-aliyun</serverId>
<!--表示阿里云的docker仓库地址。阿里云的容器镜像服务地址-->
<registryUrl>registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service</registryUrl>
<!--表示推送镜像到仓库。-->
<pushImage>true</pushImage>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--加入下面两项配置-->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
三,推送服务
完成了以上准备工作后就可以使用命令:mvn -f pom.xml clean package -DskipTests docker:build 来推送服务了 ,推送成功如下:
aliyuncs.com/springios/gold-recycle-service:0.0.1-SNAPSHOT
[INFO] Tagging registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service:0.0.1-SNAPSHOT with 0.0.1-SNAPSHOT
[INFO] Tagging registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service:0.0.1-SNAPSHOT with latest
[INFO] Pushing registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service:0.0.1-SNAPSHOT
The push refers to repository [registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service]
203c86e211c3: Pushed
35c20f26d188: Pushed
c3fe59dd9556: Pushed
6ed1a81ba5b6: Pushed
a3483ce177ce: Pushed
ce6c8756685b: Pushed
30339f20ced0: Pushed
0eb22bfb707d: Pushed
a2ae92ffcd29: Pushed
0.0.1-SNAPSHOT: digest: sha256:f4994be046ce880eb41240dcd1c47a9d8f66730457c388609003205684b5a6b1 size: 2212
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 49.472 s
四,阿里云服务器使用镜像
1,登录服务器:sudo docker login --username=szandyye2018 registry.cn-shenzhen.aliyuncs.com,密码为开通服务时设置的密码。
2,下载刚推送的镜像:sudo docker pull registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service:[镜像版本号]
3,使用docker images查看镜像是否下载成功。
4,启动镜像:docker run --name goldService -p 8086:8086 -t registry.cn-shenzhen.aliyuncs.com/springios/gold-recycle-service:0.0.1-SNAPSHOT --name:重新指定镜像的名称,-p:端口映射 -t:前台运行,可以看到项目启动信息。
五,浏览器验证。
注意事项:
如果镜像需要访问数据库,在打包前需要查看阿里云服务器的内网地址,查看命令:ip add,显示信息如下:
[root@izwz97rz0layh3rvnc50xaz ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:16:3e:12:4e:05 brd ff:ff:ff:ff:ff:ff
inet 172.18.120.230/20 brd 172.18.127.255 scope global dynamic eth0
valid_lft 314587579sec preferred_lft 314587579sec
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:00:23:2e:77 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
15: vethcfac81a@if14: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether 3e:91:41:ea:87:01 brd ff:ff:ff:ff:ff:ff link-netnsid 0
27: veth121bed1@if26: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether 4e:13:6c:8a:a3:9e brd ff:ff:ff:ff:ff:ff link-netnsid 1
[root@izwz97rz0layh3rvnc50xaz ~]#
主要看第2和第3,第二点eth0表示服务器的内网信息,其中172.18.120.230就是服务器的内网地址,该地址在使用mvn命令打包前需要写到配置文件中。第三点表示docker容器的网络配置信息。