3

I'm trying to build a docker image with my application in Gitlab continuous integration using the jib Maven plugin, but I can't get the plugin connect to the docker daemon using the dind (docker-in-docker) service. Currently I'm using this configuration in my gitlab-ci.yml file:

deploy:mvn:
  image: maven:3.6.3-jdk-8-openj9
  stage: deploy
  services:
    - docker:dind
  script:
    - mvn compile jib:dockerBuild

This is the error I get:

[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]


UPDATE

I updated to 2.2.0 and it's running locally. I added the docker images command before and the plugin seems not to be able to find the docker command in Gitlab CI:

$ docker images && mvn compile jib:dockerBuild /bin/bash: line 97: docker: command not found

This is the configuration for the jib plugin:

<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 &amp;&amp; sync &amp;&amp; /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>
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • 1
    0.9.11 is too old. Try 2.2.0. And I'd try `script: - docker images && mvn compile jib:dockerBuild` to see if the `docker` command is on PATH. – Chanseok Oh Apr 17 '20 at 19:06
  • @ChanseokOh updated my question. – Aritz Apr 19 '20 at 17:02
  • Just as I expected, the `docker` command is not available in the image. See [my answer](https://stackoverflow.com/a/61324055/1701388) below. – Chanseok Oh Apr 20 '20 at 13:49

2 Answers2

2

the plugin seems not to be able to find the docker command in Gitlab CI:

No, it is not Jib but /bin/bash that cannot find the docker command. Even before using Jib, you don't have docker available. Take a closer look at the error message.

$ docker images && mvn compile jib:dockerBuild /bin/bash: line 97: docker: command not found

For example, on my Linux, if I try a command foo that doesn't exist in a shell script, it outputs the same message.

$ /bin/bash -c "foo && mvn -v"
/bin/bash: line 1: foo: command not found

Therefore, the following command without mvn will fail with the same error.

script:
  - docker images

This proves that either docker doesn't exist in your GitLab runtime environment or is not on the PATH environment variable.


UPDATE

Updating my answer, since you answered that you now use jib:build instead of jib:dockerBuild.

If you use jib:build, you don't even need Docker. Jib doesn't require Docker when building and pushing an image to a remote registry with jib:build. Therefore, you can completely forget about setting up Docker and remove docker:dind and export DOCKER_HOST:

mvn compile jib:build -Djib.to.auth.username=$DOCKER_REGISTRY_USER -Djib.to.auth.password=$DOCKER_REGISTRY_PWD

Chanseok Oh
  • 3,920
  • 4
  • 23
  • 63
  • Okay, I understand that the command not being available in PATH. Thanks for the answer, but this only discards Jib to cause the issue, but doesn't answer the part of the container failing to resolve the docker command.. – Aritz Apr 20 '20 at 15:23
  • Yeah, I have never used GitLab, so this is as far as I can get you to. I am afraid my assist ends here. I hope someone having GitLab expertise will explain how to set up Docker on GitLab. Maybe post another question without Jib (and mark this one as a duplicate), as I can imagine the mere presence of Jib may scare away some GitLab experts who have never heard of Jib or make them confused at least. – Chanseok Oh Apr 20 '20 at 17:48
  • Thank you for your advices, you definitelly worth being marked the good answer! – Aritz Apr 22 '20 at 19:59
0

Finally this is the configuration I got it working with:

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

Apart from using the Docker in Docker service I needed to establish the DOCKER_HOST environment variable and also pass the credentials to my mvn jib:build command. I stored the credentials in CI settings, as environment variables. Thanks @Chanseok Oh for your help.

See also:

Aritz
  • 30,971
  • 16
  • 136
  • 217
  • If you use `jib:build` instead of `jib:dockerBuild`, you don't need Docker. Jib doesn't require Docker when building and pushing an image to a remote registry with `jib:build`. Therefore, you can remove `docker:dind` and `export DOCKER_HOST`. – Chanseok Oh Apr 22 '20 at 14:50