简体   繁体   中英

docker-compose - NODE_ENV not applying

There are 2 files in /config.

local.json

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

dev.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.

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:

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

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

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

...
  "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在此处输入图像描述

You should indicate which file for dev environment.

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

when run docker-compose

OR define

env_file:
     - ./dev.json

and:

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

in docker-compose file

and in nodejs file:

 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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