os-js / osjs-client

OS.js Client Module
https://manual.os-js.org/
Other
31 stars 31 forks source link

How can set a client config through Dockerfile #208

Closed maryam4s26 closed 9 months ago

maryam4s26 commented 9 months ago

How can I set a client config through Dockerfile or docker compose file?

According to the description in the Docker document, we should be able to define a runtime variable using ENV in the Docker file. Dockerfile

ENV session_timeout=600000

Also, according to Osjs document , we should be able to use this parameter in the client configuration.

src/client/config.js

export default {
  sessionTimeout: process.env.session_timeout,
};

But unfortunately I get undefined value in client in such situation.

@andersevenrud @ajmeese7

andersevenrud commented 9 months ago

Try something like dotenv-webpack. This works with both dotenv file and global variables (with docker).

andersevenrud commented 9 months ago

You can also just have a pre-made config file and copy this into your docker image, or set up a dotenv file before building in the dockerfile.

mahsashadi commented 9 months ago

Hi @andersevenrud

Actually we need to pass the value from docker-compose.yml file.

For example if we config webpack plugins as below:

 new Dotenv({
    path: './client.env'
 }),

The value is not known in this client.env , and It will be passed when the container is made through docker-compose file

// ./client.env
SESSION_EXPIRY_TIMEOUT= process.env.my_yml_env
// ./docker-compose.yml
   ....
    environment:
      - my_yml_env = 60000
   ...
andersevenrud commented 9 months ago

I think there might be an issue with the Webpack version. Did a quick test, and I had to do the following.

My variable was now exposed to the client on build time and was resolved correctly.

# docker-compose.yaml

version: '3'
services:
  osjs:
    user: node
    build:
      context: .
    volumes:
      - .:/usr/src/osjs
    ports:
      - "${OSJS_NODE_PORT:-8000}:8000"
    environment:
      - MY_VARIABLE=foo
// webpack.config.js

//...
module.exports = {
  plugins: [
    // ...
    // Standard env variables + explicitly the ones I want
    new webpack.EnvironmentPlugin(['NODE_ENV', 'DEBUG', 'MY_VARIABLE']),
   // ...
  ]
}
// config.js

export default {
  myVariable: process.env.MY_VARIABLE, // = "foo"
  auth: {
    login: {
      username: 'demo',
      password: 'demo'
    }
  }
};
mahsashadi commented 9 months ago

Many Thanks

andersevenrud commented 9 months ago

Closing because this should be resolved.