我的问题是我在 Javascript 文件中硬编码了一些 URL。它们特定于我部署 docker 镜像的环境。这是我需要的不同网址的示例。
对于实时 ENV
:
export const BLUE_SERVICE = 'https://myliveserver/api/blueservice';
export const RED_SERVICE = 'https://myliveserver/api/redservice';
对于发布ENV
:
export const BLUE_SERVICE = 'https://myreleaseserver/api/blueservice';
export const RED_SERVICE = 'https://myreleaseserver/api/redservice';
我的想法是拥有该文件的两个版本。然后,我将根据 dockerfile 中的 ENV VARIABLE
复制其中一个。顺便说一句,我已经有一个名为 STAGE
ENV VARIABLE
也许还有其他(更好)的解决方案可以做到这一点。有什么想法吗?
最佳答案
您可以使用build-time variables相反:
docker run –-build-arg BLUE_SERVICE='...' –-build-arg RED_SERVICE='...'
This flag allows you to pass the build-time variables that are accessed like regular environment variables in the
RUN
instruction of the Dockerfile.
Also, these values don’t persist in the intermediate or final images likeENV
values do.
您可以生成两个图像,这两个图像将在其 Dockerfile 中使用正确的 BLUE 和 RED SERVICE 值生成正确的 javascript 文件。
关于javascript - 如何根据Docker环境变量选择要复制的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34898246/