Docker: OpenEMS backend not working out of the box

I got it running with the help of this tutorial Dynamically set Angular Environment Variables in Docker.

I case somebody else wants to run the backend with docker and a configurable UI_WEBSOCKET here are the steps I did.

  1. Create a env.js file in directory /ui/src/assets/env.js with following content
(function(window) {
  window["env"] = window["env"] || {};

  // Environment variables
  window["env"]["websocket"] = "ws://localhost:8082";
})(this);
  1. Create a env.template.js file in directory /ui/src/assets/env.template.js with following content
(function(window) {
  window.env = window.env || {};

  // Environment variables
  window["env"]["websocket"] = "${UI_WEBSOCKET}";
})(this);
  1. Adjust file backend-docker.ts to following
// @ts-strict-ignore
import {Environment} from "src/environments";
import {theme} from "./theme";

window["env"] = window["env"] || {};

export const environment: Environment = {
    ...theme, ...{

        backend: 'OpenEMS Backend',
        url: window["env"]["websocket"] || "ws://" + location.hostname + ":8082",

        production: true,
        debugMode: false,
    },
};
  1. Uncomment Line 13 in following file index.html. This line should be uncommented
  <script src="assets/env.js" onerror="null"></script>
  1. Adjust the Dockerfile for the UI, add following command at the end of the file
CMD ["/bin/sh",  "-c",  "envsubst < /usr/share/nginx/html/assets/env.template.js > /usr/share/nginx/html/assets/env.js && exec nginx -g 'daemon off;'"]
  1. After that build your own docker image as descriped in the README.

I´m pretty sure this is not the “OpenEMS” way to do it … but it works for me :slight_smile:

Greetings!