loading...
EcoLogic

One Docker Image to Rule them All

magbeat profile image Markus Huggler ・1 min read

We have a website with multiple divisions. We want to reuse the same docker image for all the divisions.

First approach was to load the configuration dynamically from a server (depending on the ENV variables in the image). This would mean that every client will have to send the request and that the configuration server's uptime is crucial.

We are now using variables in the environment.ts file, eg:

export const environment = {
  ...
  url: 'https://__DOMAIN__',
  ...
}

We then have an entrypoint.sh script for the docker which can be called with arguments and replaces the strings in the application.

#!/bin/bash

DOMAIN="$1"

for filename in dist/**/*.js; do
    if [[ $filename =~ "main" ]]; then
        sed -i -e 's/__DOMAIN__/'$DOMAIN'/g' $filename
    fi
done

...

pm2-runtime start pm2.json --web

The image is used in a Kubernetes deployment:

...
  spec:
    containers:
      - image: <image-repo>
        args: ["www.ecologic.ch"]  
...

envsubst would also be a possibility. You would then use ENV variables in the environmentfiles and use envsubst in the entrypoint script. In the kubernetes deployment you would then add env vars instead of args.

Need more details?

Please let us know if you are interested in more details!

Posted on by:

magbeat profile

Markus Huggler

@magbeat

Software Engineer, Dad and Drummer, Fedora and i3wm user

EcoLogic

EcoLogic AG. The IT company with a nimble workforce. IT with all the trimmings - just as much as you want. Tailored, down-to-earth, friendly. Get to know us!

Discussion

markdown guide