fabric8io / docker-maven-plugin

Maven plugin for running and creating Docker images
https://dmp.fabric8.io
Apache License 2.0
1.88k stars 644 forks source link

Error Encountered When Building Multi-Architecture Docker Images Using docker-maven-plugin #1785

Open alankritaman opened 6 months ago

alankritaman commented 6 months ago

Description

Encountered an error while attempting to utilize the docker-maven-plugin for building multi-architecture images in GitLab. The error message received was:

ERROR: could not create a builder instance with TLS data loaded from environment. Please use `docker context create <context-name>` to create a context for the current environment and then create a builder instance with the context set to <context-name>

Following advice provided, I executed the following commands before initiating the build process:

- docker context create tls-environment
- docker buildx create --name my-builder --driver docker-container --use tls-environment

Despite implementing these commands, the error persisted. Attempting to build manually using the following command was successful:

docker buildx build --push --platform linux/amd64,linux/arm64 -t tmp .

However, when attempting to execute the build command through Maven (mvn), the same error continued to occur.

Even after attempting to manually create a Docker context using the following exec-maven-plugin code, the error persisted unchanged.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>${exec-maven-plugin.version}</version>
    <executions>
      <execution>
        <id>create-docker-context</id>
        <phase>pre-build</phase>  <goals>
          <goal>exec</goal>
        </goals>
        <configuration>
          <command>
            docker
            context
            create
            --name
            ${docker.context}
          </command>
        </configuration>
      </execution>
      </executions>       
</plugin>

Info

Add following to pom.xml
 <properties>
   <docker.platforms></docker.platforms>
 </properties>

and 
<buildx>
  <platforms>
    <platform>linux/amd64</platform>
    <platform>linux/arm64</platform>
  </platforms>
</buildx>
alankritaman commented 6 months ago

Discovered a workaround in the GitLab configuration to address the issue. Removed the docker-certs section from the previous configuration.

Previous Configuration:

config.template.toml: |
  [[runners]]
    [runners.kubernetes]
      namespace = "gitlab"
      tls_verify = false
      image = "docker:19"
      privileged = true
    [[runners.kubernetes.volumes.empty_dir]]
      name = "docker-certs"
      mount_path = "/certs/client"
      medium = "Memory"

New Configuration:

config.template.toml: |
  [[runners]]
    [runners.kubernetes]
      image = "docker:26.1"
      privileged = true
      namespace = "gitlab"
      tls_verify = false

Additionally, updated the gitlab-ci.yml file with the following configuration:

stage: mvnbuild
services:
  - name: docker:dind
    command: ["--tls=false"]

variables:
  DOCKER_HOST: "tcp://docker:2375"
  DOCKER_TLS_CERTDIR: ""