我正在尝试通过我的SpringBoot
应用连接到CosmosDB
。如果我使用Spring
或通过Intellij
运行应用程序,所有这些都可以正常工作。但是,当我在Docker
中运行应用程序时,我会收到以下错误消息:
com.azure.data.cosmos.CosmosClientException: The authorization token is not valid at the current time.
Please create another token and retry
(token start time: Thu, 26 Mar 2020 04:32:10 GMT,
token expiry time: Thu, 26 Mar 2020 04:47:10 GMT, current server time: Tue, 31 Mar 2020 20:12:42 GMT).
请注意,以上错误信息中的
当前服务器时间
是正确的,但其他时间落后了5天。有趣的是,我只在Docker容器中收到这个错误。
FROM {copy of zulu-jdk11}
ARG JAR_FILE
#.crt file in the same folder as your Dockerfile
ARG CERT="cosmos.cer"
ARG ALIAS="cosmos2"
#import cert into java
COPY $CERT /
RUN chmod +x /$CERT
WORKDIR $JAVA_HOME/lib/security
RUN keytool -importcert -file /$CERT -alias $ALIAS -cacerts -storepass changeit -noprompt
WORKDIR /
COPY /target/${JAR_FILE} app.jar
COPY run-java.sh /
RUN chmod +x /run-java.sh
ENV JAVA_OPTIONS "-Duser.timezone=UTC"
ENV JAVA_APP_JAR "/app.jar"
# run as non-root to mitigate some security risks
RUN addgroup -S pcc && adduser -S nonroot -G nonroot
USER nonroot:nonroot
ENTRYPOINT ["/run-java.sh"]
需要注意的一点是ENV JAVA_OPTIONS "-Duser.timezone=UTC"
,但是删除这个并没有帮助我解决问题。
基本上我从IntelliJ中运行相同的步骤,没有任何问题,但在docker中,到期日期似乎比实际时间早5天。
version: "3.7"
services:
orchestration-agent:
image: {image-name}
ports:
- "8080:8080"
network_mode: host
environment:
- COSMOSDB_URI=https://host.docker.internal:8081/
- COSMOSDB_KEY={key}
- COSMOSDB_DATABASE={database}
- COSMOSDB_POPULATEQUERYMETRICS=true
- COSMOSDB_ITEMLEVELTTL=60
我认为还应该提到,我将 network_mode
更改为 host
。并且我还将 CosmosDB 的 URI 从 https://localhost:8081
更改为 https://host.docker.internal:8081/
。
我还想提一下,我使用以下链接来帮助构建我的 dockerfile
:
docker run --interactive --tty {image-name} /bin/sh -c 'date'
命令。如果你的客户端依赖于镜像的时间,而镜像的时间不正确,这可能会解释问题。我假设“当前服务器时间”是由 CosmosDB 服务返回的。 - DazWilkindocker run --interactive --tty --entrypoint=/bin/sh {image-name} date
命令来获取容器中的日期。显然,这个日期与您主机上的日期不匹配,因此需要进行调整。 - DazWilkin