Overriding ".env" environment variable in dockerized React SPA at Docker container starting point


I use “dotenv-webpack” plugin in my webpack build for React SPA.

In webpack.config:

const Dotenv = require('dotenv-webpack')

const envFileName = env.local ? '.env.local' : isDevelopment ? '.env.dev' : '.env'

plugins: [
    // ...
    new Dotenv({
        systemvars: true,
        ignoreStub: true,
        path: path.resolve(__dirname, envFileName),
    // ...

In “.env” file there are some environment variables. “.env” is for production, but we have servers with distinct requirements, so, I need to override one variable in “.env” at Docker starting, depending on which server my image is started on.

It there a good way to override? For instance:

“.env” is: MY_VAR=xxx and, at starting container I want someting like this: docker start -d -p 3000:3000 -e MY_VAR=yyy my-container

But looks like overriding does not work in this way.

You can’t override a variable when the container starts, only when it is created.

Thanks, Akos!
The same conclusion did I, as webpack-dotenv uses DefinePlugin to replace ENV usage statically, at the build time.