3

I'm working with Angular 5, Nginx for my front app and Node.js for my backend. Everything will run in Docker.

I need to create multiple environments (staging, production,...) for the backend and the frontend. The back is easy, I use NODE_ENV in my Docker compose. But what about the front?

Since I use Angular with AOT, serving the dist folder in Nginx, I don't know how to dynamically change the env. The only solution I found for now is to build multiple version of the front with ng build --env=staging, one by env, pushing each Docker image in the DTR and fetching those images by env name.

This is very cumbersome and I'm wondering if anyone encounter the same issue and found a better way. Or maybe the whole thing is a bad idea?

Any leads welcomed!

NorTicUs
  • 718
  • 1
  • 10
  • 27
  • Have a look here https://stackoverflow.com/a/49559443/1160794 – David Apr 11 '18 at 17:57
  • Thank you for the insight but I would still have to build N docker image with a different `conf.json` in it. Ideally I would like to use some env variables but still 1 image. – NorTicUs Apr 11 '18 at 18:30
  • I don't know docker well, but can't you have a task that just copies the correct config, based on environment, to the right place? Your build can include multiple config.xxx.json and the task copies the correct one to config.json – David Apr 11 '18 at 19:39
  • Docker have two phases: 1. you build an image, with all the files you need. 2. you "execute" an image, with some optional env. I need to operate only on phase 2 – NorTicUs Apr 11 '18 at 22:30
  • But when you execute the image, you start a process, like start nginx, right? Maybe you could start a bash file that does the config replacement and then start nginx? – David Apr 12 '18 at 08:09
  • You're right, this would work. A bit hacky but definitely a workaround. Thank you @David – NorTicUs Apr 12 '18 at 20:41
  • Otherwise I think there are some ways to access environment variables with nginx and lua, which means that you could serve a different conf.json depending on nginx rules. But it's probably more complicated than the script option – David Apr 12 '18 at 20:45

1 Answers1

0

You can create just one package with the configurationin json files, retrieved at run time, like shown here

If you have multiple config.[envName].json files (1 per environment), then, when you run your image, you need to copy the one for the corresponding environment to config.json which will be the one requested by angular)

To achieve that, you could run a bash script that copies the file (identified thanks to an environment variable) then start nginx when you run your image, instead of starting nginx directly

run.sh

#!/bin/sh
cp /config/folder/path/config.$ENVNAME.json /config/folder/path/config.json
#start nginx

and run that file as your docker command

Other option

If you have the lua module installed, you could write some dynamic script that'd serve the correct json config file based on some environment varible (that lua can access)

David
  • 33,444
  • 11
  • 80
  • 118