【发布时间】:2020-03-31 20:23:24
【问题描述】:
我正在尝试通过我的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).
请注意,在上述错误消息中,current server time 是正确的,但其他时间晚了 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:
Importing self-signed cert into Docker's JRE cacert is not recognized by the service
How to add a SSL self-signed cert to Jenkins for LDAPS within Dockerfile?
【问题讨论】:
-
感觉好像你的容器(图像)没有正确获取|更新时间。如果它有壳,你能
docker run --interactive --tty {image-name} /bin/sh -c 'date'。如果您的客户在时间上依赖于图像并且图像时间不正确,这可以解释问题。我假设 CosmosDB 服务正在返回current server time。 -
@DazWilkin 我应该看到什么作为输出,我看到的错误和我通常做的一样
-
由于我没有关闭我的工作计算机,它会在周末睡觉,当计算机进入睡眠状态时,它会使 docker 不同步。重启我的电脑解决了这个问题
-
啊,抱歉,您可能需要调整入口点:
docker run --interactive --tty --entrypoint=/bin/sh {image-name} date以根据容器获取日期。显然,此日期时间与您的主机错误匹配。 -
@DazWilkin 我在您的解决方案中收到“/bin/sh: can't open 'date': No such file or directory”。我在窗户上
标签: docker