springspring-bootdockerspring-boot-maven-pluginbuildpack

spring build-image does not work with DOCKER_HOST different than /var/run/docker.sock


It looks like spring-boot:build-image does not respect the DOCKER_HOST environment variable which is on my host: unix:///run/user/1000/docker.sock

  1. Ubuntu Installation
  2. Installation of Docker CE
  3. Checkout https://github.com/spring-projects/spring-petclinic
  4. echo $DOCKER_ENV => returns unix:///run/user/1000/docker.sock
  5. Run ./mvnw spring-boot:build-image

Following error is shown:

[INFO] --- spring-boot:3.3.4:build-image (default-cli) @ spring-petclinic ---
[INFO] Building image 'docker.io/library/spring-petclinic:3.3.0-SNAPSHOT'
[INFO] 
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder-jammy-base:latest' 100%
[INFO]  > Pulled builder image 'paketobuildpacks/builder-jammy-base@sha256:4dac2a913d951b4f53d6fbfccabeb2c32324f9f8babbf6b78f4129cb60d1373e'
[INFO]  > Pulling run image 'docker.io/paketobuildpacks/run-jammy-base:latest' 100%
[INFO]  > Pulled run image 'paketobuildpacks/run-jammy-base@sha256:899201ebad25544171c62c8316d7726dd90df32564a8d29c18656e27729a117c'
[INFO]  > Executing lifecycle version v0.20.3
[INFO]  > Using build cache volume 'pack-cache-e76efea4b23a.build'
[INFO] 
[INFO]  > Running creator
[INFO]     [creator]     ===> ANALYZING
[INFO]     [creator]     ERROR: failed to initialize analyzer: getting previous image: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.45/version": dial unix /var/run/docker.sock: connect: permission denied

How can I configure build-image task to use the right value from $DOCKER_HOST ?

I tested also to set the host in the maven plugin:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <docker>
      <host>unix:///run/user/1000/docker.sock</host>
   </docker>
  </configuration>
</plugin>

Solution

  • Seems to work with this spring-boot-maven-plugin configuration:

    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <docker>
          <host>${env.DOCKER_HOST}</host>
          <bindHostToBuilder>true</bindHostToBuilder>
        </docker>
      </configuration>
    </plugin>
    

    See: https://docs.spring.io/spring-boot/docs/3.0.x/maven-plugin/reference/htmlsingle/#build-image.examples.docker.podman

    Update (10.11.2024):

    Actually setting <bindHostToBuilder>true</bindHostToBuilder> is enough (thx Scott Frederick):

    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <docker>
          <bindHostToBuilder>true</bindHostToBuilder>
        </docker>
      </configuration>
    </plugin>