简体   繁体   中英

Unable to get Configuration from Spring Cloud Config Server while its running within a Docker Container

I have been working on trying to resolve this issue for a day, and have had no luck, I am really stumped on what the error is. I have created a simple Spring Cloud Config Server using Spring Boot version 1.5.9.RELEASE and Spring Cloud Edgware.RELEASE. My Config Repo is a bitbucket repository. For Docker I am using the fabric8 maven docker plugin to build the docker image, and its deployed on Docker for Mac. I am also using Java 9. I am able to build my docker image, and run it on port 8888 but when I invoke the health endpoint it is "Down". When I try to run the following Get Request ( http://localhost:8888/sample-api/dev ), I get the following error:

{
    "timestamp": 1515552923667,
    "status": 404,
    "error": "Not Found",
    "exception": "org.springframework.cloud.config.server.environment.NoSuchRepositoryException",
    "message": "Cannot clone or checkout repository: https://bitbucket.org/<project>/<repo>.git",
    "path": "/sample-api/dev"
}

Below is my Dockerfile, any sensitive data has been removed

FROM openjdk:9
ENV PROFILE dev
VOLUME /config
ADD /maven/*.jar app.jar
EXPOSE 8888
RUN bash -c 'touch /app.jar'
WORKDIR /
ENTRYPOINT exec java -Dspring.profiles.active=$PROFILE -Djava.security.egd=file:/dev/./urandom -jar /app.jar

Below is my application yaml file:

server:
  port: 8888
spring:
  application:
    name: sample-config-server
  cloud:
    config:
      server:
        encrypt:
          enabled: false
        git:
          uri: https://bitbucket.org/<project>/<repo>.git
          username: ${GIT_USER_NAME}
          password: ${GIT_PASSWORD}

username: ${CLOUD_CONFIG_NAME}
password: ${CLOUD_CONFIG_PASSWORD}

Below are the commands I use to run this with docker:

mvn clean package docker:build
docker run -p 8888:8888 -d sample/<sample-config-server:latest

When I run locally using, mvn -Dspring.profiles.active=dev spring-boot:run, everything works as expected. It is only when I run within Docker for Mac when I get the 404 error. I tried sshing into the container and cloning my git repo manually, I was able to do that with no problem. The issue has to be with my Spring Cloud configuration or even Spring Cloud Config Project itself, I don't know.

Has anyone faced this issue before? Can anyone replicate this issue?

Update:

I have run the same container in minikube and I still get this same issue... I am working round the clock trying to resolve this issue, but no success yet. I will keep you updated.

I was finally able to resolve the issue. This had nothing to do with Spring Cloud Config or my Spring Cloud Config configuration. After careful analysis of logs, I saw the following error coming from JGit:

java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

It seems like the openJDK 9 docker image was missing content in its cacerts file, preventing JGit from making a successful https connection to bitbucket, thus resulting in the NoSuchRepositoryException. I searched the issue an found this link on the subject:

https://github.com/docker-library/openjdk/issues/145

From what I read in the link, it was recommended to use 9-jdk-slim instead of 9-jdk, since 9-jdk-slim did not have the empty cacerts file.

In my Dockerfile, I change the FROM statement to use 9-jdk-slim instead of 9-jdk. I rebuilt the image using the Maven Fabric 8 plugin, and ran the image as a container. I was successfully able to get configurations from Bitbucket via the Spring Cloud Config server. This worked in Docker for Mac and in Minikube. My lesson learned is to fully enable logging in Spring Boot, and conduct a careful and through analysis of the logs to resolve issues like this. I hope this helps other developers with similar issues.

1) Your references to ports in host and container refer to 8888 but link you included says http://localhost:8080/sample-api/dev . Is it a typo or maybe you have another app listening on port 8080.

The Config server could be queried using these formats:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

How is the properties file named in your Bitbucket repo?

BTW, I have blogged about this exact topic with source code and docker image available, it might help you to get started with yours:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM