Search code examples
mavendockerdocker-build

What is the proper set up for Maven to communicate with Docker daemon on Mac?


I keep trying to build a Docker images of my Maven spring boot project on my Mac.

Here is my build section:

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
             </executions>
      </plugin>
      <plugin>
  <groupId>com.spotify</groupId>
  <artifactId>docker-maven-plugin</artifactId>
       <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
  <executions>
    <execution>
      <id>build-image</id>
      <phase>package</phase>
      <goals>
        <goal>build</goal>
      </goals>
    </execution>
    <execution>
      <id>tag-image</id>
      <phase>package</phase>
      <goals>
        <goal>tag</goal>
      </goals>
      <configuration>
        <image>my-image</image>
        <newName>registry.example.com/my-image</newName>
      </configuration>
    </execution>
  </executions>
</plugin>
    </plugins>
  </build>

When I run this command: mvn -X package docker:build

I get this error:

 [ERROR] Failed to execute goal 
com.spotify:docker-maven-plugin:0.4.13:build (build-image) 
on project Spring-Boot-ReceiverAPI: Exception caught: 
Request error: 
POST unix://localhost:80/build?t=uptake/Spring-Boot-ReceiverAPI: 500: HTTP 500 Internal Server Error -> [Help 1]

Here is my DOCKER_HOST:

echo $DOCKER_HOST
unix:///private/var/run/docker.sock

All other docker commands run fine:

docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
<none>                       <none>              384ed96af950        4 hours ago         640.9 MB
java                         8                   96cddf5ae9f1        10 days ago         640.9 MB
containersol/minimesos-cli   0.10.2              0f8fd0fee007        8 weeks ago         133.7 MB

The Docker daemon is clearly running, and the value of DOCKER_HOST appers to work fine for normal Docker command.

What can I do, to make the docker build work on Mac?


Solution

  • I have resolved this issue by enabling my regular user account to run docker commands without using sudo. Here is the build section of the POM:

    <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                 </executions>
          </plugin>
          <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
           <configuration>
                        <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                        <dockerDirectory>src/main/docker</dockerDirectory>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
                    </configuration>
      <executions>
        <execution>
          <id>build-image</id>
          <phase>package</phase>
          <goals>
            <goal>build</goal>
          </goals>
        </execution>
        <execution>
          <id>tag-image</id>
          <phase>package</phase>
          <goals>
            <goal>tag</goal>
          </goals>
          <configuration>
            <image>api</image>
            <newName>registry.example.com/api</newName>
          </configuration>
        </execution>
      </executions>
    </plugin>
        </plugins>
        <resources>       
            <resource>
                <directory>${basedir}/src/main/docker</directory>
                <filtering>true</filtering>
                <includes>
                    <include>Dockerfile</include>
                </includes>
            </resource>
        </resources>
      </build>
    

    And here is the command, which runs fine under a regular user account:

    mvn -X package docker:build