lorenzvth7 lorenzvth7 - 1 year ago 109
JSON Question

Reusable docker image for 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.

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

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

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

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

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

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.

Answer Source

This was my 'solution'. I know it isn't the best docker approach but just for our developers it was a big help.

My dockerfile looks like this:

FROM nginx:1.10

RUN apt-get update && \
 apt-get install -y curl

RUN sed -i "s/httpredir.debian.org/`curl -s -D - http://httpredir.debian.org/demo/debian/ | awk '/^Link:/ { print $2 }' | sed -e 's@<http://\(.*\)/debian/>;@\1@g'`/" /etc/apt/sources.list

  apt-get clean && \
  apt-get update && \
  apt-get install -y nodejs-legacy && \
  apt-get install -y npm

WORKDIR /home/app
COPY . /home/app

RUN npm install -g gulp
RUN npm install

COPY start.sh /
CMD ["./start.sh"]

So after the whole include of the app + npm installation inside my nginx I start my container with the start.sh script.

The content of start.sh:

sed -i 's@my-url@'"$DATA_ACCESS_URL"'@' configs/config.json
gulp build
rm -r /usr/share/nginx/html/
//cp right folders which are created by gulp build to /usr/share/nginx/html
//start nginx container
/usr/sbin/nginx -g "daemon off;"

So the build will happen when my container starts. Not the best way of course but it's all for the needs of the developers. Have an easy local frontend.

The sed command will perform a replace on the config file which contains something like:

  "service_base": "my-url",

So my-url will be replaced by my the content of my environment variable which I willd define in my docker run command.

Than I'm able to perform.

docker run -d -p 80:80 -e DATA_ACCESS_URL=https://mybackendurl.com app:latest

And every developer can use the frontend locally and connect with their own backend URL.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download