在 Gitlab CI 中 运行 Maven jib 插件时无法连接到 docker 守护进程
Can't connect to the docker daemon while running Maven jib plugin in Gitlab CI
我正在尝试使用 jib Maven 插件在 Gitlab 持续集成中使用我的应用程序构建一个 docker 图像,但我无法使用该插件连接到 docker 守护进程dind (docker-in-docker) 服务。目前我在我的 gitlab-ci.yml 文件中使用这个配置:
deploy:mvn:
image: maven:3.6.3-jdk-8-openj9
stage: deploy
services:
- docker:dind
script:
- mvn compile jib:dockerBuild
这是我得到的错误:
[ERROR] Failed to execute goal
com.google.cloud.tools:jib-maven-plugin:0.9.11:dockerBuild
(default-cli) on project my-application: Build to Docker daemon failed,
perhaps you should make sure Docker is installed and you have correct
privileges to run it -> [Help 1]
更新
我更新到 2.2.0 并且在本地 运行。之前添加了docker images
命令,插件在Gitlab中好像找不到docker命令CI:
$ docker images && mvn compile jib:dockerBuild /bin/bash: line 97:
docker: command not found
这是 jib 插件的配置:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<from>
<image>adoptopenjdk/openjdk11:alpine-jre</image>
</from>
<to>
<image>my-application:latest</image>
</to>
<container>
<entrypoint>
<shell>sh</shell>
<option>-c</option>
<arg>chmod +x /entrypoint.sh && sync && /entrypoint.sh</arg>
</entrypoint>
<ports>
<port>8080</port>
</ports>
<environment>
<SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
<JHIPSTER_SLEEP>0</JHIPSTER_SLEEP>
</environment>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
</configuration>
</plugin>
the plugin seems not to be able to find the docker command in Gitlab CI:
不对,不是Jib而是/bin/bash
找不到docker
命令。即使在使用 Jib 之前,您也没有 docker
可用。仔细查看错误信息。
$ docker images && mvn compile jib:dockerBuild /bin/bash: line 97: docker: command not found
例如,在我的 Linux 上,如果我尝试 shell 脚本中不存在的命令 foo
,它会输出相同的消息。
$ /bin/bash -c "foo && mvn -v"
/bin/bash: line 1: foo: command not found
因此,以下不带 mvn
的命令将失败并出现相同的错误。
script:
- docker images
这证明 docker
在您的 GitLab 运行时环境中不存在,或者不在 PATH
环境变量中。
更新
更新我的答案,因为你 你现在使用 jib:build
而不是 jib:dockerBuild
。
如果你使用jib:build
,你甚至不需要Docker。使用 jib:build
构建图像并将图像推送到远程注册表时,Jib 不需要 Docker。因此,您可以完全忘记设置 Docker 并删除 docker:dind
和 export DOCKER_HOST
:
mvn compile jib:build -Djib.to.auth.username=$DOCKER_REGISTRY_USER -Djib.to.auth.password=$DOCKER_REGISTRY_PWD
最后这是我使用的配置:
services:
- docker:dind
deploy:mvn:
image: maven:3.6.3-jdk-8-openj9
stage: deploy
script:
- export DOCKER_HOST=tcp://docker:2375
- mvn compile jib:build -Djib.to.auth.username=$DOCKER_REGISTRY_USER -Djib.to.auth.password=$DOCKER_REGISTRY_PWD
only:
- tags
除了在 Docker 服务中使用 Docker 之外,我还需要建立 DOCKER_HOST 环境变量并将凭据传递给我的 mvn jib:build
命令。我将凭据作为环境变量存储在 CI 设置中。谢谢@Chanseok 哦你的帮助。
另请参阅:
我正在尝试使用 jib Maven 插件在 Gitlab 持续集成中使用我的应用程序构建一个 docker 图像,但我无法使用该插件连接到 docker 守护进程dind (docker-in-docker) 服务。目前我在我的 gitlab-ci.yml 文件中使用这个配置:
deploy:mvn:
image: maven:3.6.3-jdk-8-openj9
stage: deploy
services:
- docker:dind
script:
- mvn compile jib:dockerBuild
这是我得到的错误:
[ERROR] Failed to execute goal com.google.cloud.tools:jib-maven-plugin:0.9.11:dockerBuild (default-cli) on project my-application: Build to Docker daemon failed, perhaps you should make sure Docker is installed and you have correct privileges to run it -> [Help 1]
更新
我更新到 2.2.0 并且在本地 运行。之前添加了docker images
命令,插件在Gitlab中好像找不到docker命令CI:
$ docker images && mvn compile jib:dockerBuild /bin/bash: line 97: docker: command not found
这是 jib 插件的配置:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<from>
<image>adoptopenjdk/openjdk11:alpine-jre</image>
</from>
<to>
<image>my-application:latest</image>
</to>
<container>
<entrypoint>
<shell>sh</shell>
<option>-c</option>
<arg>chmod +x /entrypoint.sh && sync && /entrypoint.sh</arg>
</entrypoint>
<ports>
<port>8080</port>
</ports>
<environment>
<SPRING_OUTPUT_ANSI_ENABLED>ALWAYS</SPRING_OUTPUT_ANSI_ENABLED>
<JHIPSTER_SLEEP>0</JHIPSTER_SLEEP>
</environment>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
</configuration>
</plugin>
the plugin seems not to be able to find the docker command in Gitlab CI:
不对,不是Jib而是/bin/bash
找不到docker
命令。即使在使用 Jib 之前,您也没有 docker
可用。仔细查看错误信息。
$ docker images && mvn compile jib:dockerBuild /bin/bash: line 97: docker: command not found
例如,在我的 Linux 上,如果我尝试 shell 脚本中不存在的命令 foo
,它会输出相同的消息。
$ /bin/bash -c "foo && mvn -v"
/bin/bash: line 1: foo: command not found
因此,以下不带 mvn
的命令将失败并出现相同的错误。
script:
- docker images
这证明 docker
在您的 GitLab 运行时环境中不存在,或者不在 PATH
环境变量中。
更新
更新我的答案,因为你 jib:build
而不是 jib:dockerBuild
。
如果你使用jib:build
,你甚至不需要Docker。使用 jib:build
构建图像并将图像推送到远程注册表时,Jib 不需要 Docker。因此,您可以完全忘记设置 Docker 并删除 docker:dind
和 export DOCKER_HOST
:
mvn compile jib:build -Djib.to.auth.username=$DOCKER_REGISTRY_USER -Djib.to.auth.password=$DOCKER_REGISTRY_PWD
最后这是我使用的配置:
services:
- docker:dind
deploy:mvn:
image: maven:3.6.3-jdk-8-openj9
stage: deploy
script:
- export DOCKER_HOST=tcp://docker:2375
- mvn compile jib:build -Djib.to.auth.username=$DOCKER_REGISTRY_USER -Djib.to.auth.password=$DOCKER_REGISTRY_PWD
only:
- tags
除了在 Docker 服务中使用 Docker 之外,我还需要建立 DOCKER_HOST 环境变量并将凭据传递给我的 mvn jib:build
命令。我将凭据作为环境变量存储在 CI 设置中。谢谢@Chanseok 哦你的帮助。
另请参阅: