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


docker-hub
你的DockerHub用户名
你的DockerHub密码

你的DockerHub邮箱



在DockerHub上创建repo,例如:test,如下图
项目pom.xml修改为如下:注意imageName的路径要和repo的路径一致




com.spotify
docker-maven-plugin
0.4.12




eacdy/test

${project.basedir}/src/main/docker


/
${project.build.directory}

${project.build.finalName}.
jar




docker-hub
https://reg.dockerhub.upenny.cn/




执行命令:
mvn clean package docker:build -DpushImage
搞定,等构建成功后,我们会发现Docker镜像已经被push到DockerHub上了。
将镜像push到私有仓库
在很多场景下,我们需要将镜像push到私有仓库中去,这边为了讲解的全面性,私
有仓库采用的是配置登录认证的私有仓库。
和push镜像到DockerHub中一样,我们首先需要修改Maven的全局配置文件
settings.xml,添加以下段落
3.7 使用Maven插件构建Docker镜像


docker-registry
你的DockerHub用户名
你的DockerHub密码

你的DockerHub邮箱



将项目的pom.xml改成如下,

com.spotify
docker-maven-plugin
0.4.12


reg.itmuch.com/test-pull-registry
${project.basedir}/src/main/docker irectory>


/
${project.build.directory}
${project.build.finalName}.jar



docker-registry


执行:
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



com.spotify
docker-maven-plugin


build-image
package

build




${docker.image.prefix}/${project.artifactId}

java
["java", "-jar", "/${project.build.finalNa
me}.jar"]



/
${project.build.directory}
${project.build.finalName}.jar






如上,我们只需要添加:


build-image
package

build



即可。本例指的是讲docker的build目标,绑定在package这个phase上。也就是
说,用户只需要执行 mvn package ,就自动执行了 mvn docker:build 。

 

#################################################

 

Search In A Box With Docker, Elastic Search, Spring Boot, and Selenium


#另一种方法 (------------------------还是这个靠谱,都是英文奥!!!-----------------------)
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:


    org.springframework.boot
    spring-boot-maven-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:


    com.alexecollins.docker
    docker-maven-plugin
    2.3.1
   
       
       
            com.alexecollins.docker
            docker-java-orchestration-plugin-boot2docker
            2.3.1
       

   


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

 

深圳逆时针

你可能感兴趣的:(springboot项目maven打包成dockerImage推送到私有仓库dockerhub上)