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 。


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

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:

<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

猜你喜欢

转载自blog.csdn.net/long13631/article/details/79742345
今日推荐