MarioC
MarioC

Reputation: 3228

Docker Compose - Service name not working as environment variable

I'm trying to use docker-compose to setup a Spring Cloud project.

I'm using spring cloud configuration so I have a Configuration Server and some services.

For now, I have 3 services in my docker-compose.yml

version: '3'

services:
    db:
        image: mysql
        restart: always
        environment: 
            MYSQL_ROOT_PASSWORD: 'root' # TODO: Change this
            MYSQL_USER: 'user'
            MYSQL_PASS: 'password'
            MYSQL_ROOT_HOST: '%'
        volumes:
            - "db:/opt/mysql/docker:rw"
        ports:
            - "3307:3306"

    config:
        image: config-server
        restart: always
        depends_on:
            - db
        environment:
            - SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/ec_settings?createDatabaseIfNotExist=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC                
        ports:
            - "8100:8100"

    gateway:
        image: gateway
        restart: always
        depends_on:
            - config
        environment:
            - CONFIG_URI=http://config:8100
        ports:
            - '8081:8080'

volumes:
    db: {}

In gateway microservice, in bootstrap.yml, i have this setting

spring:
  cloud:
    config:
      uri: ${CONFIG_URI}

When i put up the docker composer i see that gateway service is trying to fetch configuration from http://config:8100

 Fetching config from server at : http://config:8100

So, the variable passes to Spring Boot but docker-compose does not replace the service name with its actual link. The very strange thing is that SPRING_DATASOURCE_URL environment variable gets translated correctly in config service to connect to db service.

Upvotes: 0

Views: 1381

Answers (1)

MarioC
MarioC

Reputation: 3228

I finally solved it thanks to this link Docker - SpringConfig - Connection refused to ConfigServer

The problem was the service was trying to fetch the config url too early. Solution is to put in bootstrap.yml this settings

spring:
  cloud:
    config:
      fail-fast: true
      retry:
        max-attempts: 20

Upvotes: 1

Related Questions