5

I'm trying to create a docker image of a Spring Boot application using the Gradle plugin. I'm using Spring Boot 2.6.4 and Gradle 7.1.1.

I'm on a Mac, and I don't have Docker Desktop installed. Indeed, I run docker using Colima.

The problem is that I cannot build the docker image with the command ./gradlew bootBuildImage since Gradle cannot find the docker daemon:

Connection to the Docker daemon at 'localhost' failed with error "[2] No such file or directory"; ensure the Docker daemon is running and accessible

Is there any configuration I have to do in Colima or my build.gradle file?

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
  • 1
    Do you have a `DOCKER_HOST` environment variable set, or a `docker.host` configured in the build plugin configuration as shown in the docs (https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#build-image.docker-daemon)? Without this, the plugin should try to connect to the container runtime using the `/var/run/docker.sock` socket, which seems like it should work from a quick read of the Colima docs. – Scott Frederick Mar 25 '22 at 23:55
  • I tried this myself and got the same error as you without any Docker host configuration. I'll look into it and see what it will take to make this work. – Scott Frederick Mar 26 '22 at 19:17
  • Thanks, @ScottFrederick. Hope to hear you soon! – riccardo.cardin Mar 27 '22 at 19:44

1 Answers1

6

Colima creates a socket in the location ~/.colima/docker.sock by default. Running the command docker context ls should show a context named colima with the socket location shown in the DOCKER ENDPOINT column.

You can configure the Spring Boot Gradle plugin to use this socket by setting the DOCKER_HOST environment variable to unix:///Users/<user>/.colima/docker.sock or by adding the following to your build file as shown in the documentation.

tasks.named("bootBuildImage") {
  docker {
    host = "unix:///Users/<user>/.colima/docker.sock"
  }
}
Scott Frederick
  • 4,184
  • 19
  • 22
  • Thanks, Scott. So, it's not possible to have a `build.gradle` file that is not dependent on the location of the `docker.sock` file. Did I understand right? – riccardo.cardin Mar 29 '22 at 07:47
  • 1
    I've tested Spring Boot's image building with 4 container engines - Docker Engine, minikube, podman, and now colima. All expose the Docker client REST API using different means, as you can see in the documentation: https://docs.spring.io/spring-boot/docs/2.7.x/gradle-plugin/reference/htmlsingle/#build-image.examples.docker. It would be difficult for Spring Boot to guess from all these possibilities. The `docker` CLI uses the contexts mentioned above, but podman has it's own `podman` CLI that works differently so that's not entirely reliable either. – Scott Frederick Mar 29 '22 at 16:44
  • 1
    It is possible to have a `build.gradle` file that is independent of the socket location, as the Spring Boot plugins will honor the `DOCKER_HOST` environment variable. For Colima, you can set this with a command like `export DOCKER_HOST=$(docker context inspect colima -f '{{.Endpoints.docker.Host}}')`. – Scott Frederick Mar 08 '23 at 15:59