Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run eureka service in a docker container

I want to run eureka-server as container and want to let other microservices register to this container later on. But I got some issues to let it run as container and access it. The application is running in STS without issues. When I execute it in STS I can access the eureka-server using localhost:8761.

application.java:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

application.yml:

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

bootstrap.yml:

spring:
  application:
    name: eureka-service

Dockerfile:

FROM java:8 
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","- 
jar","/app.jar"]

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <docker.image.prefix>microservice</docker.image.prefix>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <groupId>com.example</groupId>
    <artifactId>eureka-service</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId} 
 </repository>
                    <buildArgs>

 <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                    <finalName>${project.build.finalName}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka- 
server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

What am I doing:

  • running mvn package && java -jar target/eureka-service-0.1.0.jar to build
    the jar. During this the spring boot application and I have to stop this using ctrl+c. The image has been created in the target folder.
  • building image using mvn install dockerfile:build. Image has been built.
  • run the created image using docker run -p 8080:8080 -t microservice/eureka- service
  • This is the response of the docker bash:

    2018-03-23 15:28:05.618 INFO 1 --- [ main] hello.EurekaServiceApplication : Started EurekaServiceApplication in 17.873 seconds (JVM running for 19.066) 2018-03-23 15:29:05.475 INFO 1 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms

The notification 2018-03-23 15:31:05.476 INFO 1 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 1ms returns by the bash about once a minute. If I try to call the eureka-serverusing 192.168.99.100:8080 in the browser it says I can't access this page. When I end the application using ctrl+c I can view all running containers and the bash tells me the container microservice/eureka-service is still running. Still can not access it tho.

like image 552
elp Avatar asked Mar 23 '18 15:03

elp


People also ask

How do I use Eureka server in Docker?

Add Dockerfile in Your Eureka Server ProjectFROM java:8 determines the Java application and it will require Java runtime and libraries to run the application and it will pull all the required Java libraries and add to container. EXPOSE 8761 determines the application will be exposed to the world on port 8761.

Can we use Eureka with Kubernetes?

You have to have a Kubernetes service on top of the eureka pods/deployments which then will provide you a referable IP address and port number. And then use that referable address to look up the Eureka service, instead of "8761".

Is Netflix Eureka deprecated?

Deprecated by Netflix and no longer maintained by Pivotal/VMware.


1 Answers

server:
  port: 8761

You have that in your application.yml. Which means the application is running on port 8761 not 8080

You should try docker run -p 8761:8761 -t microservice/eureka-service

like image 63
Amanuel Nega Avatar answered Sep 20 '22 16:09

Amanuel Nega