When we make the Docker image by the Maven and Dockerfile on the Spring project, we take the docker-maven-plugin among the several plugins these make the Docker image.
This story is about a occurred exception of connection refused, when I used the docker-maven-plugin.
Firstly, look at the code. (pom.xml)
<plugins>
...
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.9</version>
<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>
</plugin>
...
</plugins>
I added the code to the ‘<plugins> … </plugins>’.
If you use over the JDK 9, you will meet the error message.
Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource
This message is resolved to add the Java activation dependency in the plugin configuration.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.9</version>
<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>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</plugin>
And next problem is the connection refused, at the mvn docker:build command executed.
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.9:build
(default-cli) on project image-reader-api: Exception caught: java.util.concurrent.ExecutionException:
com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException:
Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused
(Connection refused) -> [Help 1]
When we look at the Exception messages, it says that failed connect to the the 2375 port of the Localhost.
The 2375 port is the Rest-API port of docker daemon, if we are using the Windows-os, it’s easy to resolve.
Go to settings menu of the Docker for windows, and check to the Expose daemon on.
(https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000327324–Docker-Setting-Connection-refused-on-localhost-2375)
But, we can’t find that menu on the Docker for mac-os.
How should we resolve this issue?
The easiest way is to add the small linux container on the Local docker, and it is redirect the Docker api to tcp port on our the Mac-os host.
docker run -d -v /var/run/docker.sock:/var/run/docker.sock -p 127.0.0.1:2375:2375
bobrik/socat TCP-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock
export DOCKER_HOST=tcp://localhost:2375
Let’s try that one again, perhaps the issue has been resolved.