Building a Docker Image with the Spring Boot Gradle Plugin and Colima
Asked Answered
H

1

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?

Heard answered 25/3, 2022 at 14:18 Comment(3)
Do you have a DOCKER_HOST environment variable set, or a docker.host configured in the build plugin configuration as shown in the docs (docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/…)? 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.Dirty
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.Dirty
Thanks, @ScottFrederick. Hope to hear you soon!Heard
D
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"
  }
}
Dirty answered 28/3, 2022 at 21:0 Comment(3)
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?Heard
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: docs.spring.io/spring-boot/docs/2.7.x/gradle-plugin/reference/…. 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.Dirty
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}}').Dirty

© 2022 - 2024 — McMap. All rights reserved.