Search code examples
spring-cloudspring-cloud-configspring-cloud-config-server

Can't connect to cloud config server, from inside docker compose


So, I try to connect my spring app to the cloud config server, but it doesn't work when I try to access it after I deploy my app from inside the docker.

networks:
  vd-network:

services:
  config:
    image: hyness/spring-cloud-config-server:3.1.3-jdk11
    deploy:
      labels:
        description: "Spring Cloud Config Server"
      mode: replicated
      restart_policy:
        condition: on-failure
    networks:
      - vd-network
    environment:
      - SPRING_APPLICATION_NAME=config
      - SERVER_PORT=8888
      - SPRING_CLOUD_CONFIG_SERVER_GIT_URI=<URI>
      - SPRING_CLOUD_CONFIG_SERVER_GIT_REFRESH-RATE=3
      - SPRING_CLOUD_CONFIG_SERVER_GIT_USERNAME=<NAME>
      - SPRING_CLOUD_CONFIG_SERVER_GIT_PASSWORD=<PASSWORD>
      - SPRING_CLOUD_CONFIG_SERVER_GIT_DEFAULT-LABEL=main
      - SPRING_CLOUD_CONFIG_SERVER_GIT_SKIPSSLVALIDATION=true
      - ENCRYPT_KEY=<KEY>
    ports:
      - "8888:8888"
  app:
    image: my-image
    ports:
      - "9000:9000"

bootstrap-docker.yaml

  application:
    name: ${name:config}
  cloud:
    config:
      failFast: true
      name: ${name:config}
      profile: ${profile:local}
      debug: true
      uri: ${config.host:http://config:8888}
      label: main
  config:
    import: optional:configserver:${config.host:http://config:8888}

This doesnt work, and If I change it to localhost, it works only when deploying the app directly, but not when running through docker.

If I add so that the app starts only after the config server is fully deployed, I get the error:

Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://config:8888/application/local/main": Connection refused


Solution

  • The problem was that they were not on the same network. Then after this, I had another problem of the app starting before the cloud config server and requesting the data from it, from an endpoint, but as it was not fully started, it failed. I fixed it by adding:

    deploy:
      restart_policy:
        condition: on-failure
    

    Now it fails repeatedly until the server starts and then it is able to connect. I have tried using healthcheck and conditionals, but the cloud server config image does not allow execution from terminal, I tried with wait-for-it.sh, with which I had some other problems. So, in the end I solved it with this. If you have a better solution, please let me know. So now my app looks something like:

    my-app:
      image: my-app-image
      deploy:
        mode: replicated
        restart_policy:
          condition: on-failure
      depends_on:
          - config
      networks:
        - vd-network
      ports:
        - "9000:9000"