title: how to dockerize a spring microservices app that uses Eureka and Cloud config
body: <p>i'm working in this spring microservices application, and everything works fine untill i want to dockerize the microservices. Having a little knowledge about docker i'm struggling to make it work, Here's what i've done till now:</p>
  
  <p><strong>1.Config server</strong>
  this server is a centralized server that contains common configurations of other services and also special configurations for each microservice.</p>
  
  <p>This is the config service configuration</p>
  
  <pre><code>spring.profiles.active=native
  server.port=8888
  spring.cloud.config.server.native.search-locations=classpath:/common-config
  spring.application.name=config-server
  #the password which apps will use to access its configurations
  spring.security.user.password=1234
  </code></pre>
  
  <p>then i added a foler in src/main/resources called common-config which contains each service configs:</p>
  
  <ol>
  <li>discover-service.yml</li>
  </ol>
  
  <p>this is configuration for eureka server</p>
  
  <pre><code>server:
    port: 8762
  </code></pre>
  
  <p>Then in the root of the config-server, i add 2 files Dockerfile and docker-compose.yml:</p>
  
  <p><strong>Dockerfile</strong></p>
  
  <pre><code># Start with a base image containing Java runtime
  FROM openjdk:8-jdk-alpine
  
  # Add Maintainer Info
  LABEL maintainer="sanchi.goyal.sg@gmail.com"
  
  # Add a volume pointing to /tmp
  VOLUME /tmp
  
  # Make port 8888 available to the world outside this container
  EXPOSE 8888
  
  # The application's jar file
  ARG JAR_FILE=./target/*.jar
  
  # Add the application's jar to the container
  ADD ${JAR_FILE} app.jar
  
  # Run the jar file
  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  </code></pre>
  
  <p><strong>docker-compose</strong></p>
  
  <pre><code>version: "3"
  
  services:
    config-server:
      image: config-server:0.0.1-SNAPSHOT
      environment:
        - JAVA_OPTS=
           -DEUREKA_SERVER=http://discover-service:8762/eureka
      ports:
        - 8888:8888  
    discover-service:
      image: discover-service:0.0.1-SNAPSHOT
      ports:
        - 8762:8762
      depends_on:
        - config-server
  restart: on-failure
  </code></pre>
  
  <p>Now in the discover-service application, ive added Dockerfile in root directory as follow:</p>
  
  <pre><code>    # Start with a base image containing Java runtime
  FROM openjdk:8-jdk-alpine
  
  # Add Maintainer Info
  LABEL maintainer="sanchi.goyal.sg@gmail.com"
  
  # Add a volume pointing to /tmp
  VOLUME /tmp
  
  # Make port 8888 available to the world outside this container
  EXPOSE 8762
  
  # The application's jar file
  ARG JAR_FILE=./target/*.jar
  
  # Add the application's jar to the container
  ADD ${JAR_FILE} app.jar
  
  # Run the jar file
  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  </code></pre>
  
  <p>and in src/main/resources, i have added the file bootstrap.yml as follow:</p>
  
  <pre><code>#This file is the entrypoint for spring to know where to look for the configuration file and hold the initial configuration
  
  spring:
    application:
      name: discover-service #name of the application
    cloud:
      config:
        uri: http://localhost:8888 #where the config-service is running
        fail-fast: true #the service will not run if it can't reach the config-service
        password: 1234 #password of the config-service
        username: user #username of the config-service
  
  eureka:
    instance:
      prefer-ip-address: true #when the application registers with eureka, it uses its IP address rather than its hostname
    client:
      registerWithEureka: false #tells the built-in eureka client to not register itself with eureka, because we should act as a server
      fetchRegistry: false
  </code></pre>
  
  <p>Now here's the problem, before starting dockerize the two apps, i build a jar file and docker jar file for each app, so for the config-server app i create the 2 JARs with no problems, but for the second app, in order to create the 2 JARs,i have to keep the below property in bootstrap.yml so it knows where the config server.</p>
  
  <pre><code>uri: http://localhost:8888
  </code></pre>
  
  <p>Now moving to docker, when i run the command:</p>
  
  <pre><code>docker-compose up
  </code></pre>
  
  <p>the first app works, but the second app says:</p>
  
  <pre><code>Fetching config from server at : http://localhost:8888
  Connect Timeout Exception on Url - http://localhost:8888
  org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/discover-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
  </code></pre>
  
  <p>can any one please help me with this !</p>
  
  <p>here's my github repository <a href="https://github.com/MoroccanGeek/UnitedRemote-BackendChanllenge" rel="nofollow noreferrer">https://github.com/MoroccanGeek/UnitedRemote-BackendChanllenge</a>
  I didn't commit the changes described in this question. You will find multiple services, but if i can run the 2 described apps in docker i can do it with the other ones</p>
  
  <p><strong>EDIT</strong>
  I've managed to start my applications by changing docker-compose.yml to this:</p>
  
  <pre><code>version: "3"
  
  services:
    config-server:
      image: config-server:0.0.1-SNAPSHOT
      environment:
        - JAVA_OPTS=
           -DEUREKA_SERVER=http://discover-service:8762/eureka
      ports:
        - 8888:8888
    discover-service:
      image: discover-service:0.0.1-SNAPSHOT
      ports:
        - 8762:8762
      depends_on:
        - config-server
      restart: on-failure
  networks:
    host:
  </code></pre>
  
  <p>but something weird is happening, when i want for example go to discover-service application (which is an eureka server), it doesnt work, but when i go to another website on the same Tab and go back it shows what is expected</p>