How to build a Docker image for client service of Spring Cloud Config Server

Viewed 84

I have built a Docker image for cloud config service. I have another service employee for which I want to build a Docker image. This client service will read data source configuration properties from the config server

I am able to connect the employee service successfully with config service on localhost without Docker. The problem is only that I am not able to build a Docker image for the employee service.

On running mvn package dockerfile:build command for the employee service I get the following error

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Below is the bootstrap.yml file located at src/main/resources directory

spring:
  application:
    name: employee
  profiles:
    active: dev
  cloud:
    config:
      uri: http://configserver:8071

Config service's bootstrap.yml file

spring:
  application:
    name: config-server
  profiles:
    active: native,dev
    
  cloud:
    config:
      server:
        #Local configuration: This locations can either of classpath or locations in the filesystem.
        native:
          #Reads from a specific filesystem folder 
          search-locations: classpath:/config
    
server:
  port: 8071

employee.properties file in config directory

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

employee-dev.properties file

spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

This is probably because the container of config server with hostname configserver is not up. I am not sure what hostname I should provide. Even if I want to build the image of employee service by starting the container of config server, it won't be on configserver host but on localhost. This is probably why the build is failing?

I want to build the Docker image of employee service so that its container can be fired up using the docker-compose up command

Edit: This question is similar but I already have the spring-cloud-starter-config dependency added in my client service and it still gives me the same error on build

Edit 2

You don't have to separately build a Docker image for the client service at all!

When I ran docker-compose up command I noticed the following line in output

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://configserver:8071

But I was still not sure if it was actually reading the data from the config server. So I purposely added incorrect database credentials in config server properties file for my service and the service startup failed! Now I am sure that my employee service is reading the data from the config server

I am still not sure how can I achieve creating a new Docker image for my employee service.

1 Answers

Did you try changing the URL param db to actual host of vhostname ?

db =change it to IP addess or accessable load balancer URL

spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false

Related