AngularJS 的可重用 docker 镜像 [英] Reusable docker image for AngularJS

查看:29
本文介绍了AngularJS 的可重用 docker 镜像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 AngularJS 应用程序.我们为它编写了一个 dockerfile,因此它可以在每个系统上重用.dockerfile 不是最佳实践,对于某些人来说,它可能是一些奇怪的构建(构建和托管在同一个文件中),但它只是为了在每个开发人员的每台 PC 上本地运行我们的 angularjs 应用程序而创建的.

We have an AngularJS application. We wrote a dockerfile for it so it's reusable on every system. The dockerfile isn't a best practice and it's maybe some weird build up (build and hosting in same file) for some but it's just created to run our angularjs app locally on each PC of every developer.

Dockerfile:
FROM nginx:1.10

... Steps to install nodejs-legacy + npm

RUN npm install -g gulp
RUN npm install
RUN gulp build  

.. steps to move dist folder

我们使用 docker build -t myapp:latest 构建我们的镜像.每个开发人员都可以使用 docker run -d -p 80:80 myapp:latest

We build our image with docker build -t myapp:latest . Every developer is able to run our app with docker run -d -p 80:80 myapp:latest

但现在我们正在开发其他后端.所以我们在 DEV 中有一个后端,在 UAT 中有一个后端,......所以我们需要在 /config/xx.json

But now we're developing other backends. So we have a backend in DEV, a backend in UAT, ... So there are different URLS which we need to use in /config/xx.json

{
  ...
  "service_base": "https://backend.test.xxx/",
  ...
}

我们不想每次都更改那个 URL,重建图像并启动它.我们也不想声明一些可以在那里使用的 URL(dev、uat、prod、..).我们希望使用环境变量而不是硬编码的 URL 来执行我们的 gulp build 过程.

We don't want to change that URL every time, rebuild the image and start it. We also don't want to declare some URLS (dev, uat, prod, ..) which can be used there. We want to perform our gulp build process with an environment variable instead of a hardcoded URL.

所以我们可以像这样启动我们的容器:

So we we can start our container like this:

docker run -d -p 80:80 --env URL=https://mybackendurl.com app:latest

有没有遇到过此类问题的人?因此,我们需要在我们的 json 中创建一个 env 变量并构建它,如果可能的话,稍后再添加 URL.

Is there someone who has experience with this kind of issues? So we'll need an env variable in our json and building it and add the URL later on if that's possible.

推荐答案

更好的选择是使用 build args

EDIT : Better option is to use build args

您可以使用 docker build args,而不是在 docker run 命令中传递 URL.在 docker build 期间执行 build 相关命令比 docker run 更好.

Instead of passing URL at docker run command, you can use docker build args. It is better to have build related commands to be executed during docker build than docker run.

在你的 Dockerfile 中,

In your Dockerfile,

ARG URL 

然后运行

docker build --build-arg URL=<my-url> .

有关详细信息,请参阅此 stackoverflow 问题

See this stackoverflow question for details

这篇关于AngularJS 的可重用 docker 镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆