简体   繁体   English

docker-compose - NODE_ENV 不适用

[英]docker-compose - NODE_ENV not applying

There are 2 files in /config. /config 中有 2 个文件。

local.json local.json

{
    "host": "localhost",
    "port": 3030,
    "env": "local"
}

dev.json开发者json

{
    "host": "localhost",
    "port": 3030,
    "env": "dev"
}

Whenever the application runs, it should log something like info: Feathers application started on http://localhost:3030 in local , which "local" is the env variable in config file.每当应用程序运行时,它应该记录类似info: Feathers application started on http://localhost:3030 in local ,其中“local”是config文件中的 env 变量。

index.js index.js

/* eslint-disable no-console */
const logger = require('./logger');
const app = require('./app');
const port = app.get('port');
const env = app.get('env');
const server = app.listen(port);

process.on('unhandledRejection', (reason, p) =>
  logger.error('Unhandled Rejection at: Promise ', p, reason)
);

server.on('listening', () =>
  logger.info('Feathers application started on http://%s:%d in %s', app.get('host'), port, env)
);

I tried to set NODE_ENV=dev on container test-web in docker-compose.yml , but every time I run the application, it shows:我尝试在 docker-compose.yml 的容器test-web上设置docker-compose.yml NODE_ENV=dev ,但每次运行应用程序时,它都会显示:

info: Feathers application started on http://localhost:3030 in local

but I am expecting但我期待

info: Feathers application started on http://localhost:3030 in dev

Dockerfile Dockerfile

FROM node:lts-alpine

RUN npm install --global sequelize-cli nodemon

WORKDIR /server

COPY package*.json ./

RUN npm install

COPY . . 

EXPOSE 3030

CMD ["npm", "run", "dev"]

docker-compose.yml docker-compose.yml

version: '2.1'

services:
  test-db:
    image: mysql:5.7
    environment:...
    volumes:
      - ./db-data:/var/lib/mysql
    ports:
      - 3306:3306
  test-web:
    environment:
      - NODE_ENV=dev
      #- DEBUG=*
      - PORT=3030
    build: .
    command: >
      ./wait-for-db-redis.sh test-db npm run dev
    ports:
      - "3030:3030"
    volumes:   
      - ./:/server
    depends_on:
      - test-db

package.json package.json

...
  "scripts": {
    "test": "npm run lint && npm run mocha",
    "lint": "eslint src/. test/. --config .eslintrc.json --fix",
    "dev": "nodemon --legacy-watch src/",
    "start": "node src/",
  },
...

Update更新
I checked the docker application, and it shows correct NODE_ENV, but I cannot get the data from correct file dev.json我检查了 docker 应用程序,它显示了正确的 NODE_ENV,但我无法从正确的文件dev.json中获取数据在此处输入图像描述

You should indicate which file for dev environment.您应该为开发环境指明哪个文件。

docker-compose --env-file ./dev.json up 

when run docker-compose当运行 docker-compose

OR define或定义

env_file:
     - ./dev.json

and:和:

environment:
     - NODE_ENV=dev
     - HOST_ENV=localhost
     - PORT_ENV=3030

in docker-compose file在 docker-compose 文件中

and in nodejs file:在 nodejs 文件中:

 logger.info('Feathers application started on http://%s:%d in %s', app.get('host'), port, process.env.NODE_ENV)

Reference: https://docs.docker.com/compose/environment-variables/参考: https://docs.docker.com/compose/environment-variables/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM