I want to make an image in docker. My app run in nodejs and use Mysql. When I build, it's error like this
[email protected] start /todo-skyshi
#11 1.831 > node server.js
#11 1.831
#11 2.247 /todo-skyshi/config/database/dbConn.js:4
#11 2.247 const db = new Sequelize(DB_NAME, DB_USERNAME,DB_PASSWORD, {
#11 2.247 ^
#11 2.247
#11 2.247 ReferenceError: DB_NAME is not defined
#11 2.247 at Object.<anonymous> (/todo-skyshi/config/database/dbConn.js:4:26)
#11 2.247 at Module._compile (internal/modules/cjs/loader.js:999:30)
#11 2.247 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
#11 2.247 at Module.load (internal/modules/cjs/loader.js:863:32)
#11 2.247 at Function.Module._load (internal/modules/cjs/loader.js:708:14)
#11 2.247 at Module.require (internal/modules/cjs/loader.js:887:19)
#11 2.247 at require (internal/modules/cjs/helpers.js:74:18)
#11 2.247 at Object.<anonymous> (/todo-skyshi/config/model/activity.js:2:12)
#11 2.247 at Module._compile (internal/modules/cjs/loader.js:999:30)
#11 2.247 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
#11 2.261 npm ERR! code ELIFECYCLE
#11 2.264 npm ERR! errno 1
#11 2.271 npm ERR! [email protected] start: `node server.js`
#11 2.271 npm ERR! Exit status 1
#11 2.271 npm ERR!
#11 2.271 npm ERR! Failed at the [email protected] start script.
#11 2.272 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
#11 2.336
#11 2.337 npm ERR! A complete log of this run can be found in:
#11 2.337 npm ERR! /root/.npm/_logs/2022-02-13T02_20_56_653Z-debug.log
------
executor failed running [npm start]: exit code: 1
It's like the env on my DB params, how to build image in docker that can read env when I want to run with this command ?
docker run -e MYSQL_HOST=172.17.0.1 -e MYSQL_USER=xxxx -e MYSQL_PASWORD=xxxx -e MYSQL_DBNAME=todo -p 8090:3030 agamtheos/todo-skyshi
this is my Dockerfile :
FROM node:12-alpine
RUN apk update
RUN mkdir /todo-skyshi
ADD . /todo-skyshi
WORKDIR /todo-skyshi
RUN npm update
RUN ["npm", "start"]
Appreciate for any answer
CodePudding user response:
ReferenceError: DB_NAME is not defined #11 2.247 at Object. (/todo-skyshi/config/database/dbConn.js:4:26).
Abobe error clearly says that variables you are trying to access are not defined.
Env variables name passed in docker run command are different than used in code.
DB_NAME, DB_USERNAME,DB_PASSWORD
.
For example
DB_NAME
can be accessed as process.env.MYSQL_DBNAME
and so other env variables.