0

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>
1

1 Answer 1

0

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>
Sign up to request clarification or add additional context in comments.

4 Comments

This is correct, although I don't think it is necessary to configure <host> to explicitly use the environment variable.
There are two main steps to building an image with the Spring Boot Maven or Gradle plugin. First, the Spring Boot plugin code pulls a builder image, a run image, and possibly buildpack images. Then the plugins start a Docker container using the build image and run commands in that container (this is where all the real image building happens).
The Spring Boot plugin code and the code in the builder image both default to \var\run\docker.sock to talk to the Docker daemon. Setting DOCKER_HOST environment variable or the docker.host build plugin configuration tells the Spring Boot plugin code where to find the Docker daemon. Setting <bindHostToBuilder>true</bindHostToBuilder> tells the Spring Boot plugin code to pass the host to the container to override the default used there.
Thank you, @ScottFrederick, for your explanations. Indeed, <bindHostToBuilder>true</bindHostToBuilder> is enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.