springboot專案maven打包成dockerImage推送到私有倉庫dockerhub上
kubernates
修改
reg.dockerhub.upenny.cn
mvn package -Dmaven.test.skip=true docker:build -DpushImage
將映象push到私有倉庫(有私有倉庫hub的)
私有倉庫將生成的證書下載到根證書路徑
mkdir -p /etc/docker/certs.d/reg.dockerhub.upenny.cn
scp [email protected]:/root/certs/reg.dockerhub.upenny.cn.crt /etc/docker/certs.d/reg.dockerhub.upenny.cn/
#################################################
《springCloud參考指南.pdf》(.................無語.................誰參考誰進坑)
將Docker映象push到DockerHub上
首先修改Maven的全域性配置檔案settings.xml,新增以下段落
3.7 使用Maven外掛構建Docker映象
136
<servers>
<server>
<id>docker-hub</id>
<username>你的DockerHub使用者名稱</username>
<password>你的DockerHub密碼</password>
<configuration>
<email>你的DockerHub郵箱</email>
</configuration>
</server>
</servers>
在DockerHub上建立repo,例如:test,如下圖
專案pom.xml修改為如下:注意imageName的路徑要和repo的路徑一致
<build>
<plugins>
<!-- docker的maven外掛,官網:https://github.com/spoti
fy/docker-maven-plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 注意imageName一定要是符合正則[a-z0-9-_.]的
,否則構建不會成功 -->
<!-- 詳見:https://github.com/spotify/dockermaven-plugin
Invalid repository
name ... only [a-z0-9-_.] are allowed -->
<!-- 如果要將docker映象push到DockerHub上去的話,
這邊的路徑要和repo路徑一致 -->
<imageName>eacdy/test</imageName>
<!-- 指定Dockerfile所在的路徑 -->
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}
</directory>
<include>${project.build.finalName}.
jar</include>
</resource>
</resources>
<!-- 以下兩行是為了docker push到DockerHub使用的。 -->
<serverId>docker-hub</serverId>
<registryUrl>https://reg.dockerhub.upenny.cn/</registryUrl>
</configuration>
</plugin>
</plugins>
</build>
執行命令:
mvn clean package docker:build -DpushImage
搞定,等構建成功後,我們會發現Docker映象已經被push到DockerHub上了。
將映象push到私有倉庫
在很多場景下,我們需要將映象push到私有倉庫中去,這邊為了講解的全面性,私
有倉庫採用的是配置登入認證的私有倉庫。
和push映象到DockerHub中一樣,我們首先需要修改Maven的全域性配置檔案
settings.xml,新增以下段落
3.7 使用Maven外掛構建Docker映象
<servers>
<server>
<id>docker-registry</id>
<username>你的DockerHub使用者名稱</username>
<password>你的DockerHub密碼</password>
<configuration>
<email>你的DockerHub郵箱</email>
</configuration>
</server>
</servers>
將專案的pom.xml改成如下,
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<!-- 路徑為:私有倉庫地址/你想要的映象路徑 -->
<imageName>reg.itmuch.com/test-pull-registry</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerD
irectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!-- 與maven配置檔案settings.xml一致 -->
<serverId>docker-registry</serverId>
</configuration>
</plugin>
執行:
3.7 使用Maven外掛構建Docker映象
mvn clean package docker:build -DpushImage
稍等片刻,將會push成功。
如果想要從私服上下載該映象,執行:
docker login reg.itmuch.com # 然後輸入賬號和密碼
docker pull reg.itmuch.com/test-pull-registry
將外掛繫結在某個phase執行
在很多場景下,我們有這樣的需求,例如執行 mvn clean package 時,自動地
為我們構建docker映象,可以嗎?答案是肯定的。我們只需要將外掛的 goal 綁
定在某個phase即可。
所謂的phase和goal,可以這樣理解:maven命令格式是: mvn phase:goal ,
例如 mvn package docker:build 那麼, package 和 docker 都是
phase, build 則是goal 。
下面是示例:
3.7 使用Maven外掛構建Docker映象
140
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}
</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalNa
me}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
如上,我們只需要新增:
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
即可。本例指的是講docker的build目標,繫結在package這個phase上。也就是
說,使用者只需要執行 mvn package ,就自動執行了 mvn docker:build 。
#################################################
#另一種方法 (------------------------還是這個靠譜,都是英文奧!!!-----------------------)
Putting The Application Into A Container
We want to put the application into a Docker container. Spring Boot can create a standalone jar to put it into a container, so add this plugin to the pom.xml:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
You can see this at work by creating a package:
$ mvn package
...
spring-boot-maven-plugin:1.2.1.RELEASE:repackage
This replaces the original JAR, with a standalone version.
Next, we'll use a plugin to build the container:
<plugin>
<groupId>com.alexecollins.docker</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>2.3.1</version>
<dependencies>
<!-- only needed if you are using Boot2Docker -->
<dependency>
<groupId>com.alexecollins.docker</groupId>
<artifactId>docker-java-orchestration-plugin-boot2docker</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</plugin>
The plugin needs some files to create the app. Each directory in src/main/docker in treated as a container, so in src/main/docker/searchinabox create a Dockerfile that:
Adds the JAR,
Adds a configuration file,
Exposes the ports - both our app on 8080, and Elastic Search on 9200 and 9300 (so it can join a cluster),
Sets the start-up command.
FROM dockerfile/java:oracle-java7
EXPOSE 8080
EXPOSE 9200
EXPOSE 9300
ADD ${project.build.finalName}.jar .
CMD java -jar /${project.build.finalName}.jar
We need a conf.yml file in the same directory, this:
Indicates that we want to add the JAR as part of the Docker image,
States the ports it should expose on the host,
A health check URL we can use to smoke test the container,
Finally, a tag for the container so we can easily identify it:
packaging:
add:
- target/${project.build.finalName}.jar
ports:
- 8080
- 9200
- 9300
healthChecks:
pings:
- url: http://localhost:9200/
- url: http://localhost:8080/
tag:
searchinabox/searchinabox:${project.version}
Package this and start-up the container:
mvn docker:start
You should see this:
[INFO] Starting searchinabox
...
[INFO] BUILD SUCCESS
The container will be listed by the docker command
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f673731a9489 searchinabox/searchinabox:1.0.0-SNAPSHOT "/bin/sh -c 'java - 6 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp search-in-a-box_app
We can check we can access Elastic Search by opening the http://localhost:9200 URL, and our application by opening http://localhost:8080.
Tips
Packing containers can go wrong. I find it helpful to print/tail the logs of the last started container with this command:
docker logs -f $(docker ps -qa|head -n1)
We often want to start the container up with a shell to debug it, for example I often get the start command wrong, so here's what I'd do:
docker run -t -i searchinabox/searchinabox:1.0.0-SNAPSHOT bash