Use fabric8io/docker-maven-plugin to build image on Windows

Use fabric8io/docker-maven-plugin to build image on Windows

The spotify docker maven plugin (Archived since Mar, 2022) can only build image with local docker client installed. This plugin works fine on my Mac.

But when I switched to Windows(with docker installed in ubuntu-WSL), problem occurred:

1 Caused by: com.spotify.docker.client.exceptions.DockerException: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.io.FileNotFoundException: \.\pipe\docker_engine (系统找不到指定的文件。)

Which means plugin can not find docker engine running on my machine. That's not the thing! The real reason is that spotify maven plugin can not build image through WSL docker.

An alternative was found after searching google. that's fabric8io/docker-maven-plugin.

This page only shows basic usage of this plugin.

The fabric8io docker maven plugin can set remote docker host, which works on Windows with docker installed within WSL.

Below shows the simplest configuration:

 1<plugin>
 2<groupId>io.fabric8</groupId>
 3<artifactId>docker-maven-plugin</artifactId>
 4<version>0.45.1</version>
 5<configuration>
 6    <dockerHost>tcp://localhost:2375</dockerHost>
 7    <images>
 8        <image>
 9            <name>${docker.image.prefix}/${project.artifactId}:%v</name>
10            <build>
11                <dockerFile>${project.basedir}/Dockerfile</dockerFile>
12            </build>
13        </image>
14    </images>
15    <buildArgs>
16        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
17    </buildArgs>
18</configuration>
19</plugin>
  1. dockerHost is the WSL host and post WSL docker daemon runs on.

  2. %v is the same as ${project.version}.

  3. Image name and tag are configured in image>name tag.

  4. Dockerfile (same as spotify docker maven plugin) is configured in image>build>dockerFile tag.

  5. Build source(java web application Jar) is configured in configuration>buildArgs tag.

You need make some change to make WSL docker daemon listen on 2375 port:

  1. open docker.service config file:

    vim /usr/lib/systemd/system/docker.service

  2. modify docker.service setting:

    1# default setting
    2# ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    3# new setting
    4ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
    
  3. restart docker daemon:

    1systemctl daemon-reload
    2systemctl restart docker
    

After that, you can run mvn clean package docker:build -DskipTests to build docker image.

To run docker image with docker-compose, use wsl docker compose up on Windows.

References #