0

I build a spring-server and a spring-client with eureka on docker and I try to connect the client to the server. When I try this in my build.gradle:

docker {
  maintainer = 'Me'
  baseImage = 'java:8'
}

distDocker {
  exposePort 8080
  setEnvironment 'JAVA_OPTS', '-Dspring.profiles.active=docker'
}

everything works.

But I want to use the Dockerfile I wrote, so I use buildDocker instead of distDocker and I use it this way:

task buildDocker(type: Docker, dependsOn: build) {
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    copy {
      from jar
      into stageDir
    }
  }
}

with this Dockerfile:

FROM java:8
ADD myjar-1.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]

and that always leads to a

connection refused-error or more precisely to a ClientHandleException: Connection to http://localhost:8761 refused

I don't really understand where the problem is? If I don't try to connect to the server both ways work but if I try to connect only distDocker works.

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
Plasmaschnee
  • 57
  • 1
  • 2
  • 11

2 Answers2

4

The Spring Boot client app, running inside the container, is trying to reach localhost:8761, and since in the context of this Docker container 'localhost' refers to the container's address. There's nothing running on port 8761 of the container and hence you are seeing the connection refused error.

This might help:

Specify the location of Eureka in your client app's configuration via its application.yml:

eureka:
  client:
    serviceUrl:
      defaultZone: <IP-address-of-Eureka-server>/eureka/

The <IP-address-of-Eureka-server> can be either an IP address or a host name that can be resolved from inside the container. If you want to pass the address of your host VM (the host that runs your Docker container), you can pass it as an environment variable using --env eureka.client.serviceUrl.defaultZone=xx.xx.xx.xx form. Something like this:

$ docker run ... --env eureka.client.serviceUrl.defaultZone=xx.xx.xx.xx

On a related note, if your Spring Boot client application also needs to register itself with Eureka, then you will need additional configuration to publish the resolvable host name and port of your Spring Boot app to Eureka. You can add the following to your application.yml:

eureka:
  instance:
    hostname: <client-host>
    nonSecurePort: <client-port>

Where, <client-host> is the resolvable host name of the client app container; and <client-port> is the host VM's port that's bound to the exposed port of the container>

Again, you can pass these using the --env argument as shown above.

$ docker run -p 9001:8080 ... \
             --env eureka.instance.hostname=yy.yy.yy.yy \
             --env eureka.instance.nonSecurePort=9001

Be sure to set yy.yy.yy.yy to your VM's IP address.

Kartik Pandya
  • 2,718
  • 2
  • 14
  • 28
  • Thank you very much for your help! I will see if it helps and report back – Plasmaschnee Nov 18 '15 at 08:39
  • I had almost all of this in my application.yml already so it did not help but it made many things clearer for me. I noticed, that when I use distDocker he builds a .tar-file and an extremely complicated classpath which he uses to execute the .jar-file he build out of my java-class. buildDocker on the other hand just builds the .jar-file and executes it so maybe the answer is somewhere hidden in the classpath – Plasmaschnee Nov 18 '15 at 09:26
  • The error very clearly says that the app could not locate Eureka, and that it was trying on localhost:8761. This means that `eureka.client.serviceUrl.defaultZone` is either not set or is set to `localhost:8761`. I'm not sure if your class path is a problem because in that case it would have most likely not even reached to that point. – Kartik Pandya Nov 18 '15 at 16:42
  • You were absolutely right! Thank you! As it turned out I had too much written in my application.yml. I don't really understand how the error occured but after deleting some lines (I'm not sure what they were doing but I thought I needed them) everything worked – Plasmaschnee Nov 19 '15 at 09:00
  • To be more precise, I had "spring: profiles: docker" written in the application.yml and after deleting this everything worked – Plasmaschnee Nov 19 '15 at 09:17
0

If you want to test in your local machine then try with host ip address. To do that use the below configuration.

eureka.client.service-url.defaultZone=http://host.docker.internal:8761/eureka/