简体   繁体   English

如何在docker文件中获取对我可用的docker环境变量? (使用React / JS / webpack)

[英]How do I get the docker environment variables available to me in the docker file? (using React/JS/webpack)

dockerfile: dockerfile:

ARG MY_VAR

ENV NODE_ENV  production
ENV MY_VAR $MY_VAR

COPY . .

RUN NODE_ENV=development npm install && \
    touch ./.env && \
    eval env > ./.env && \
    npm run build && \
    npm prune --production

Docker newb here Docker newb在这里

In my container I run a env command and see the variables I've set, also when running a node console I can see they are set in process.env 在我的容器中,我运行一个env命令并查看已设置的变量,同时在运行节点控制台时,我可以看到它们已在process.env中设置process.env

But when my project builds, all my env variables are undefined (they are not available during that RUN command) 但是在我的项目构建时,我所有的env变量都未定义(在该RUN命令期间它们不可用)

The eval env > ./.env && line DOES put the vars I set with ENV command into the .env, but I can't have those variables hardcoded in the dockerfile eval env > ./.env &&行确实将用ENV命令设置的变量放入.env中,但是我无法在dockerfile中将这些变量硬编码

I wish I could do something like: 我希望我可以做类似的事情:

ENV MY_VAR $process.env.MY_VAR

Anything like that to grab my environment variables? 像这样抢我的环境变量?

edit: 编辑:

webpack.config.js: webpack.config.js:

require('dotenv').config()

const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const BUILD_PATH = path.resolve(__dirname, "./client/build") + '/';
const SOURCE_PATH = path.resolve(__dirname, "./client/src") + '/';
const PUBLIC_PATH = path.resolve(__dirname, "./client/build") + '/';

const env = require('./env')(PUBLIC_PATH)


module.exports = () => {
  return {
    entry: ["idempotent-babel-polyfill", SOURCE_PATH + "/index.jsx"],

    context: SOURCE_PATH,

    output: {
      path: BUILD_PATH,
      filename: "bundle.js",
      publicPath: PUBLIC_PATH
    },

    module: {
      rules: [
        {
          test: /\.jsx?$/,
          exclude: /node_modules/,
          use: [
            { loader: "babel-loader" },
          ]
        },
        {
          test: /\.scss$/,
          exclude: /node_modules/,
          use: [
            { loader: "style-loader" },
            { loader: "css-loader" },
            { loader: "sass-loader" }
          ]
        },
        {
          test: /\.(png|jpg|gif)$/,
          exclude: /node_modules/,
          use: [
            { loader: "file-loader" }
          ]
        }
      ]
    },

    devServer: {
      compress: true,
      port: 3002,
      historyApiFallback: true,
      contentBase: BUILD_PATH,
      publicPath: PUBLIC_PATH
    },

    devtool: "eval-source-map",

    plugins: [
      new webpack.DefinePlugin(env),
      new HtmlWebpackPlugin({
        filename: "index.html",
        template: path.resolve(SOURCE_PATH + "/index.html"),
        inject: true
      }),
      new webpack.optimize.UglifyJsPlugin(),
    ],

    watch: false,

  }
};

env.js: env.js:

require('dotenv').config()

// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
// injected into the application via DefinePlugin in Webpack configuration.
var REACT_APP = /^REACT_APP_/i;

function getClientEnvironment(publicUrl) {
  var processEnv = Object
    .keys(process.env)
    .filter(key => REACT_APP.test(key))
    .reduce((env, key) => {
      env[key] = process.env[key];
      return env;
    }, {
        'NODE_ENV': process.env.NODE_ENV || 'development',
        'MY_VAR': process.env.MY_VAR // for example
      });

  processEnv['process.env'] = Object
    .keys(processEnv)
    .reduce((env, key) => {
      env[key] = JSON.stringify(processEnv[key]);
      return env;
    }, {})

  return processEnv;
}

module.exports = getClientEnvironment;

在docker的CMD语句中执行构建命令可以解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在js脚本中读取docker环境变量? - How do I read a docker environment variable within a js script? Node.js Docker映像环境变量 - Node.js Docker image environment variables 如何在不使用 webpack 的情况下创建环境变量并在 React 库中使用它们? - How can I create environment variables and use them in a react library, without using webpack? 如何通过 HTTP 获取 .js 文件并提取“导出”变量? - How do I HTTP GET a .js file and extract 'exports' variables? 如何获取webpack来编译app / js目录中的main.js文件? - How do I get webpack to compile the main.js file in app/js directory? 理解 React JS 中的 .env 文件和环境变量 - Understanding .env file and environment variables in React JS 我们如何在 react.js 中正确使用环境变量和 env.config.js 文件(我重新加载后未定义) - how can we use environment variables properly with env.config.js file in react.js(i got undefined after reload) 如何根据prop的值使用React.js,ES6和Webpack导入文件? - How can I import a file using React.js, ES6 and Webpack based on the value of a prop? 如何使用 webpack 将全局 JS 文件包含到我的 React 项目中? - How can I include a global JS file to my React project using webpack? 如何在 JS 文件中使用 GitHub 秘密环境变量? - How can I use GitHub secret environment variables in a JS file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM