目录
Skip Docker Goals Bound to Maven Phases(跳过)
1. com.spotify:dockerfile-maven-plugin:1.4.10 not found
2. java.io.FileNotFoundException: \\.\pipe\docker_engine (系统找不到指定的文件。)
docker-maven-plugin
我们常见开源项目中使用的Docker Maven插件是com.spotify:docker-maven-plugin
。可用版本信息见Github。
通过其介绍可知该插件已经不再推荐使用,取而代之的是com.spotify:dockerfile-maven-plugin
,Github地址。
使用该插件可以在mvn
命令中直接构建出Docker镜像和完成推送等。
dockerfile-maven-plugin
要求用户必须提供Dockerfile用于构建镜像,从而将Docker镜像的构建依据统一到Dockerfile上,这与过时的docker-maven-plugin
是不同的。
基本介绍
相比docker-maven-plugin更加灵活安全
也能够绑定 Docker 命令到 Maven 各生命周期
使用手册
Maven Goals命令
Goals available for this plugin:
Goal | Description | Default Phase |
dockerfile:build |
Builds a Docker image from a Dockerfile. | package |
dockerfile:tag |
Tags a Docker image. | package |
dockerfile:push |
Pushes a Docker image to a repository. | deploy |
Maven命令执行顺序
mvn package mvn dockerfile:build mvn verify mvn dockerfile:push mvn deploy
Skip Docker Goals Bound to Maven Phases(跳过)
You can pass options to maven to disable the docker goals.
Maven Option | What Does it Do? | Default Value |
dockerfile.skip |
Disables the entire dockerfile plugin; all goals become no-ops. | false |
dockerfile.build.skip |
Disables the build goal; it becomes a no-op. | false |
dockerfile.tag.skip |
Disables the tag goal; it becomes a no-op. | false |
dockerfile.push.skip |
Disables the push goal; it becomes a no-op. | false |
For example, to skip the entire dockerfile plugin:
mvn clean package -Ddockerfile.skip
Configuration
Build Phase
Maven Option | What Does it Do? | Required | Default Value |
dockerfile.contextDirectory |
Directory containing the Dockerfile to build. | yes | none |
dockerfile.repository |
The repository to name the built image | no | none |
dockerfile.tag |
The tag to apply when building the Dockerfile, which is appended to the repository. | no | latest |
dockerfile.build.pullNewerImage |
Updates base images automatically. | no | true |
dockerfile.build.noCache |
Do not use cache when building the image. | no | false |
dockerfile.build.cacheFrom |
Docker image used as cache-from. Pulled in advance if not exist locally or pullNewerImage is false |
no | none |
dockerfile.buildArgs |
Custom build arguments. | no | none |
dockerfile.build.squash |
Squash newly built layers into a single new layer (experimental API 1.25+). | no | false |
使用示例
https://github.com/spotify/dockerfile-maven/tree/master/plugin/src/it
注意事项(准备工作)
该插件需要Java 7或更高版本以及Apache Maven 3或更高版本(dockerfile-maven-plugin <= 1.4.6需要Maven> = 3,在其他情况下需要Maven> = 3.5.2)。
要运行集成测试或在实践中使用插件,需要有效的Docker设置。
1. 有一台Docker daemon主机
镜像的制作以及推送操作,都是由Docker来完成的,所以,必须要安装Docker环境。简单的说dockerfile-maven-plugin只是简化了直接操作Docker的复杂度,该是Docker完成的事情,还得由Docker来完成。
所以,明白这一点之后,就知道了并不是一定要在本地安装Docker环境,只要有Docker环境就可以了。
2. Docker开启远程API
3. 配置DOCKER_HOST
选项
要使用服务器的Docker环境,需要配置一个环境变量(默认情况下,插件是连接本地的Docker环境,即127.0.0.1)
环境变量的配置如下,改成你自己的服务器Docker环境即可(可能需要重启电脑):
DOCKER_HOST tcp://192.168.1.6:2375
标题
4. 镜像仓库认证信息
该插件构建和发布镜像依赖于镜像仓库,需要用户提供镜像仓库的登录信息,支持POM设置和Settings设置
具体参考:https://github.com/spotify/dockerfile-maven/blob/master/docs/authentication.md
pom.xml方式
从1.3.XX版开始,你可以使用pom本身的config进行身份验证。只需添加类似于以下内容的配置:
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${version}</version> <configuration> <username>repoUserName</username> <password>repoPassword</password> <repository>${docker.image.prefix}/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin>
或者
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${version}</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin>
然后使用命令
mvn goal -Ddockerfile.username=... -Ddockerfile.password=...
Maven多模块工程
Pom配置-父工程
<!-- 构建和推动Docker镜像 --> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${docker.maven.plugin.version}</version> <executions> <execution> <id>default</id> <goals> <!--如果package时不想用docker打包,就注释掉这个goal--> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <contextDirectory>${project.basedir}</contextDirectory> <useMavenSettingsForAuth>true</useMavenSettingsForAuth> <repository>${docker.repository.url}/${docker.registry.name}/${project.artifactId}</repository> <username>${docker.registry.username}</username> <password>${docker.registry.password}</password> <tag>${project.version}</tag> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin>
配置信息
<!--Registry2仓库的地址,ip:port--> <!--<docker.repository.url>192.168.172.128:5000</docker.repository.url>--> <docker.repository.url>registry.cn-beijing.aliyuncs.com</docker.repository.url> <!--上传的Docker镜像前缀,此前缀一定要和Harbor中的项目名称一致,和阿里云仓库的命名空间一致--> <docker.registry.name>fly_jt</docker.registry.name> <docker.registry.username>XXX@sina.com</docker.registry.username> <docker.registry.password>XXX</docker.registry.password> <docker.maven.plugin.version>1.4.13</docker.maven.plugin.version>
Pom配置-子工程
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <configuration> <tag>${project.version}</tag> </configuration> </plugin>
编写Dockerfile文件制作镜像
前提:
1. 要有环境变量DOCKER_HOST的配置
2. 要在子工程的目录下(与pom文件同级)创建一个Dockerfile文件
#设置镜像基础,jdk8 FROM java:8 #维护人员信息 MAINTAINER FLY #设置镜像对外暴露端口 EXPOSE 8080 #将当前 target 目录下的 jar 放置在根目录下,命名为 app.jar,推荐使用绝对路径。 ADD target/devicemag-core-1.0.0.jar /devicemag-core-1.0.0.jar #执行启动命令 ENTRYPOINT ["java", "-jar","/devicemag-core-1.0.0.jar"]
在父工程 , 执行mvn clean package 或者 mvn dockerfile:build 打包命令,即可制作镜像
docker images查看镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-file 1.0.0 d09619501d27 11 seconds ago 643MB registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-system 1.0.0 9497ab784991 13 seconds ago 643MB registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-gateway 1.0.0 861d2577311b 16 seconds ago 643MB registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-auth 1.0.0 98ff94ed01d0 17 seconds ago 643MB registry.cn-beijing.aliyuncs.com/fly_jt/devicemag-core 1.0.0 3defff63849a About a minute ago 643MB mysql 5.7 a70d36bc331a 2 weeks ago 449MB redis 5 e35748fd6a72 3 weeks ago 98.4MB portainer/portainer-ce latest 980323c8eb3f 4 weeks ago 196MB registry.cn-beijing.aliyuncs.com/fly_jt/portainer-ce 2.0.1 980323c8eb3f 4 weeks ago 196MB registry 2 678dfa38fcfa 7 weeks ago 26.2MB nacos/nacos-server 1.3.0 d1f1facebfbc 8 months ago 756MB rabbitmq 3.7.15-management f05c3eb3cf91 19 months ago 179MB java 8 d23bdf5b1b1b 4 years ago 643MB [root@localhost ~]#
推送镜像-Docker Registry
Docker Registry 2.0搭建
docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry2 registry:2
[root@localhost ~]# docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry2 registry:2 cba73bc3417883d68d1d47c17e48bc62b0ca8705b1a2532be4f1f8c0f659db45 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cba73bc34178 registry:2 "/entrypoint.sh /etc…" 6 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry2
启动验证
1、列出所有镜像
# curl http://192.168.172.128:5000/v2/_catalog
2、查看指定镜像都有哪些tag
# curl http://192.168.172.128:5000/v2/镜像名/tags/list
Docker Registry 用户和密码配置
容器运行成功,直接使用docker login 192.168.172.128:5000命令,输入Username和Password
我这里设置的是
用户名: test
密码:test
用户和密码base64
[root@localhost ~]# echo Hello World | base64^C [root@localhost ~]# echo test:test | base64 dGVzdDp0ZXN0Cg== [root@localhost ~]# echo dGVzdDp0ZXN0Cg== | base64 -d test:test
然后再 /.docker/config.json 添加如下内容
{undefined
"auths": {undefined
"192.168.172.128:5000": {undefined
"auth": "dGVzdDp0ZXN0"
}
}
}
[root@localhost ~]# docker login 192.168.172.128:5000 Username: test Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@localhost ~]# cat ~/.docker/config.json { "auths": { "192.168.172.128:5000": { "auth": "dGVzdDp0ZXN0" } } }
参考上面在父工程Pom中指定 repository 、username、password
推送镜像-阿里云
同时 参考上面在父工程Pom中指定 repository 、username、password
推送镜像-阿里云
在子工程 , 执行mvn deploy或者 mvn dockerfile:push 命令,即推送镜像
异常场景
1. com.spotify:dockerfile-maven-plugin:1.4.10 not found
在Maven配置文件settings.xml中添加
<pluginGroups> <!-- pluginGroup | Specifies a further group identifier to use for plugin lookup. <pluginGroup>com.your.plugins</pluginGroup> --> <pluginGroup>com.spotify</pluginGroup> </pluginGroups>
2. java.io.FileNotFoundException: \\.\pipe\docker_engine (系统找不到指定的文件。)
[ERROR] Failed to execute goal com.spotify:dockerfile-maven-plugin:1.4.13:build (default) on project devicemag-core: Could not build image: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.io.FileNotFoundException: \\.\pipe\docker_engine (系统找不到指定的文件。) -> [Help 1]
要使用服务器的Docker环境,需要配置一个环境变量(默认情况下,插件是连接本地的Docker环境,即127.0.0.1)
环境变量的配置如下,改成你自己的服务器Docker环境即可(可能需要重启电脑):
DOCKER_HOST tcp://192.168.1.6:2375
标题
3. repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project devicemag-core: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
需要注意的是,如果你没有配置部署artifact的maven repository,请不要使用mvn deploy命令,因为它会执行maven-deploy-plugin的deploy目标,而由于没有配置要部署的远程maven repository,会报类似如上的错。
解决办法1 :
在pom.xml中,应该将distributionManagement配置添加到要部署的位置。
<distributionManagement> <repository> <id>internal.repo</id> <name>Internal repo</name> <url></url> </repository> </distributionManagement>
可以在部署期间使用以下命令添加另一个位置(但为了避免出现上述错误,应至少配置一个存储库):
解决办法2 :
使用 mvn dockerfile:push 命令