我遇到了同样的问题NEXT_PUBLIC我的 Azure 应用服务上的环境变量始终具有“未定义”值,尽管它们在应用服务设置中被定义为环境变量。
构建 Docker 镜像时,NEXT_PUBLIC 环境变量在构建过程中直接设置,之后无法覆盖。这就是 NEXT_PUBLIC 的 AppService 设置不起作用的原因。
Renato Pozzi 的以下解决方案(dev.to https://dev.to/itsrennyman/manage-nextpublic-environment-variables-at-runtime-with-docker-53dl) 帮助过我。
这是我的步骤:(下一个 v.12.2.5):
a) 在“构建”过程开始之前在 Dockerfile 中创建所需的 NEXT_PUBLIC 变量,并用“占位符值”填充它。
RUN NEXT_PUBLIC_API_URL=PLACEHOLDER_NEXT_PUBLIC_API_URL yarn build
b) 创建一个入口点文件 (entrypoint.sh),当 Docker 映像启动时,该文件将占位符值替换为 AppService 中的真实环境变量。
在我的示例中,占位符“PLACEHOLDER_NEXT_PUBLIC_API_URL”被替换为 AppService 配置(环境变量)中的实际值“NEXT_PUBLIC_API_URL”。
#!/bin/sh
test -n "$NEXT_PUBLIC_API_URL"
find /app/.next \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_NEXT_PUBLIC_API_URL#$NEXT_PUBLIC_API_URL#g"
exec "$@"
c) 使之前创建的入口点文件在 Dockerfile 中可见。
COPY --chown=nextjs:nodejs entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["node", "server.js"]