How to using docker-compose with environment - node.js

I have node application that build into an image. This image not contain .env file because I add it on .gitignore whereas the application need it. How can I configure this environment on docker-compose?
I've read some of example but still dont get it. This question has same problem with me but it still doesn't work.
docker-compose:
version: "3.7"
services:
node:
container_name: node-app
image: node_app:latest
ports:
- 3334:3333
environment:
- HOST=127.0.0.1
- PORT=3333
- NODE_ENV=dummy
- APP_NAME=AdonisJs
- CACHE_VIEWS=false
- DB_CONNECTION=mysql
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_USER=root
- DB_PASSWORD=password
- DB_DATABASE=database
Dockerfile:
FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm i -g #adonisjs/cli
RUN npm install
ENV HOST=0.0.0.0
COPY . .
EXPOSE 3333
CMD [ "node", "server.js" ]
Everytime I run docker-compose up its ended up with this.
WARNING: The HOST variable is not set. Defaulting to a blank string.
WARNING: The PORT variable is not set. Defaulting to a blank string.
Recreating node-app_node_1 ... done
Attaching to node-app_node_1
node_1 |
node_1 | Error: ENOENT: no such file or directory, open '/usr/src/app/.env'
node_1 |
node_1 |
node_1 | 1 Env.load
node_1 | /usr/src/app/node_modules/#adonisjs/framework/src/Env/index.js:110
node_1 |
node_1 | 2 new Env
node_1 | /usr/src/app/node_modules/#adonisjs/framework/src/Env/index.js:42
node_1 |
node_1 | 3 Object.closure
node_1 | /usr/src/app/node_modules/#adonisjs/framework/providers/AppProvider.js:29
node_1 |
node_1 | 4 Ioc._resolveBinding
node_1 | /usr/src/app/node_modules/#adonisjs/fold/src/Ioc/index.js:231
node_1 |
node_1 | 5 Ioc.use
node_1 | /usr/src/app/node_modules/#adonisjs/fold/src/Ioc/index.js:731
node_1 |
node_1 | 6 AppProvider.boot
node_1 | /usr/src/app/node_modules/#adonisjs/framework/providers/AppProvider.js:337
node_1 |
node_1 | 7 anonymous
node_1 | /usr/src/app/node_modules/#adonisjs/fold/src/Registrar/index.js:147
node_1 |
node_1 | 8 arrayMap
node_1 | /usr/src/app/node_modules/lodash/lodash.js:653
node_1 |
node-app_node_1 exited with code 1
All I know that docker-compose environment pass to the variable that called just as same as we define to get the value, but there is not .env file on the image to called the variable. What I have to do to make the docker-compose can pass the environment to the app? Any suggestion will be appreciated.

ENOENT indicates that node is looking for a file at /usr/src/app/.env but there is nothing there. Try adding
RUN touch /usr/src/app/.env
to your Dockerfile and see if having an empty .env file fixes it.

Related

Error: #prisma/client did not initialize yet. Please run "prisma generate"

I am having a problem with my nodeJS back-end. I want to create a docker compose environment but I keep getting this error when I try to start my Prisma NodeJS app. I already tried lots of things and I can't find any usable solutions on Google. The application works without docker.
Docker compose file:
endbit-express:
container_name: endbit-express
build: ./endbit-express
volumes:
- ./endbit-express:/app
- /app/node_modules
ports:
- 8080:8080
depends_on:
- mysql
environment:
- DATABASE_URL=mysql://root:root#localhost:3306/endbit
networks:
- endbit
mysql:
container_name: endbit-mysql
image: mysql:8.0.28
restart: always
ports:
- 6033:3306
environment:
- MYSQL_DATABASE=endbit
- MYSQL_ROOT_PASSWORD=root
volumes:
- dbdata:/var/lib/mysql
networks:
- endbit
Docker file:
FROM node:17.4.0
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma
RUN npm install
COPY . .
RUN prisma generate
EXPOSE 8080
CMD ["npm", "start"]
Error:
endbit-express | > express#0.0.0 start
endbit-express | > node ./bin/www
endbit-express |
endbit-express | /app/node_modules/.prisma/client/index.js:3
endbit-express | throw new Error(
endbit-express | ^
endbit-express |
endbit-express | Error: #prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
endbit-express | In case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issues
endbit-express | at new PrismaClient (/app/node_modules/.prisma/client/index.js:3:11)
endbit-express | at Object.<anonymous> (/app/config/passport.js:7:16)
endbit-express | at Module._compile (node:internal/modules/cjs/loader:1097:14)
endbit-express | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
endbit-express | at Module.load (node:internal/modules/cjs/loader:975:32)
endbit-express | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
endbit-express | at Module.require (node:internal/modules/cjs/loader:999:19)
endbit-express | at require (node:internal/modules/cjs/helpers:102:18)
endbit-express | at Object.<anonymous> (/app/app.js:7:1)
endbit-express | at Module._compile (node:internal/modules/cjs/loader:1097:14)
endbit-express |
endbit-express | Node.js v17.4.0
add RUN npm i -g prisma before RUN prisma generate it should work
Eventually I found the answer myself: I am using a docker compose file, and Docker doesn't recognize the localhost:3306 URL. In docker compose you need to link the containers by their name, so I changed the backend component environment with this (host = docker service name):
environment:
- DB_HOST=mysql
- DB_USER=root
- DB_PASSWORD=root
- DB_NAME=endbit-express
- DB_PORT=3306

Nuxtjs app stops immediately when start with pm2

I am trying to run a nuxtjs app using pm2 however, the app exits immediately, and I could not get useful info from pm2 log:
2|frontend_nuxt | Error: ENOENT: no such file or directory, chdir '����' -> '/var/www/frontend_nuxt/releases/20210831071742'
2|frontend_nuxt | at wrappedChdir (node:internal/bootstrap/switches/does_own_process_state:112:14)
2|frontend_nuxt | at process.chdir (node:internal/worker:100:5)
2|frontend_nuxt | at /usr/local/share/.config/yarn/global/node_modules/pm2/lib/ProcessContainer.js:298:13
2|frontend_nuxt | at wrapper (/usr/local/share/.config/yarn/global/node_modules/pm2/node_modules/async/internal/once.js:12:16)
2|frontend_nuxt | at next (/usr/local/share/.config/yarn/global/node_modules/pm2/node_modules/async/waterfall.js:96:20)
2|frontend_nuxt | at /usr/local/share/.config/yarn/global/node_modules/pm2/node_modules/async/internal/onlyOnce.js:12:16
2|frontend_nuxt | at WriteStream.<anonymous> (/usr/local/share/.config/yarn/global/node_modules/pm2/lib/Utility.js:186:13)
2|frontend_nuxt | at WriteStream.emit (node:events:394:28)
2|frontend_nuxt | at node:internal/fs/streams:72:16
2|frontend_nuxt | at FSReqCallback.oncomplete (node:fs:185:23)
The nuxtjs app it self can start without issue.
I have no idea how to debug this error, or how to get more useful details, any idea?
using npm2 show id I found that the script path was not updated.
so, i just had to:
pm2 delete frontend_nuxt && pm2 start ecosystem.config.js
and all is well.

Node App Error--Failed ro replace env in config: ${NPM_TOKEN}

I am trying to build a Docker image for my Sails.js application. Here's the Dockerfile:
FROM risingstack/alpine:3.4-v8.5.0-4.7.0
ENV NODE_ENV test
RUN npm install -g sails
COPY npmrc_file .npmrc
ARG NPM_TOKEN
COPY package.json package.json
RUN npm install
RUN rm -f .npmrc
# Add your source files
COPY . .
EXPOSE 3000
CMD ["npm","start"]
I took the steps for this Dockerfile from this link on the NPM documentation site. I have made sure to match what the documentation there shows perfectly.
My docker build command is:
image="my-repo-url/tagname:tagversion"
docker build --build-arg NPM_TOKEN=my-token-goes-here -t $image -f Dockerfile .
Then I run the container with the image using docker stack or docker-compose. The container doesn't start up, due to the following error:
sails_1 | Error: Failed to replace env in config: ${NPM_TOKEN}
sails_1 | at /usr/lib/node_modules/npm/lib/config/core.js:418:13
sails_1 | at String.replace (<anonymous>)
sails_1 | at envReplace (/usr/lib/node_modules/npm/lib/config/core.js:414:12)
sails_1 | at parseField (/usr/lib/node_modules/npm/lib/config/core.js:392:7)
sails_1 | at /usr/lib/node_modules/npm/lib/config/core.js:335:17
sails_1 | at Array.forEach (<anonymous>)
sails_1 | at Conf.add (/usr/lib/node_modules/npm/lib/config/core.js:334:23)
sails_1 | at ConfigChain.addString (/usr/lib/node_modules/npm/node_modules/config-chain/index.js:244:8)
sails_1 | at Conf.<anonymous> (/usr/lib/node_modules/npm/lib/config/core.js:322:10)
sails_1 | at /usr/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
sails_1 | /usr/lib/node_modules/npm/lib/npm.js:52
sails_1 | throw new Error('npm.load() required')
sails_1 | ^
sails_1 |
sails_1 | Error: npm.load() required
sails_1 | at Object.get (/usr/lib/node_modules/npm/lib/npm.js:52:13)
sails_1 | at process.errorHandler (/usr/lib/node_modules/npm/lib/utils/error-handler.js:213:18)
sails_1 | at emitOne (events.js:115:13)
sails_1 | at process.emit (events.js:210:7)
sails_1 | at process._fatalException (bootstrap_node.js:399:26)
The thing is, by the time we get to running the container, there should be no reference to or need for the ${NPM_TOKEN}. I'm using this exact same setup in another project, and don't see this error, so I'm not really sure what the deal is. I have triple checked that everything is the same in this project as the other project.
What are the possible reasons for this error and some possible solutions?
I've also read through this thread and this SO question. I feel like everything I've read has pointed me to the same solution, which I've implemented, but with no success.
This is what worked for me in the end:
ARG NPM_TOKEN
ENV NPM_TOKEN="${NPM_TOKEN}"
Why this solved this issue, I'm not sure!
This is what I am using,
in Dockerfile
ARG NPM_AUTH_TOKEN=$NPM_AUTH_TOKEN
ENV NPM_AUTH_TOKEN=$NPM_AUTH_TOKEN
and in docker-compose.yml
version: '2'
services:
app:
build:
args:
- NPM_AUTH_TOKEN

Learning NodeJS & MongoDB Docker compose

I have a NodeJS project that uses MongoDB. I would like to run this service in a Docker container, but despite the many examples I have based my learning from, this will not work.
Here is my /heimdall_jwt/Dockerfile:
FROM node:9-alpine
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
RUN npm install pm2 -g
COPY . /usr/src/app
EXPOSE 3000
CMD ["pm2-docker", "start", "process.json"]
And here is my /heimdall_jwt/docker-compose.yml
version: '2'
# Define the services/containers to be run
services:
myapp: #name of your service
build: ./ # specify the directory of the Dockerfile
ports:
- "3000:3000" #specify ports forwarding
links:
- database # link this service to the database service
volumes:
- .:/usr/src/app
depends_on:
- database
database: # name of the service
image: mongo # specify image to build container from
I tried running via: $docker-compose up --build
which results in the mongo being build and starting. Here is the condensed output:
Building myapp
Step 1/8 : FROM node:9-alpine
...
Step 4/8 : RUN npm install
---> Using cache
---> befb91b1324c
...
Removing intermediate container 945eb0ad40d5
Successfully built b500f7ec9b89
Successfully tagged heimdalljwt_myapp:latest
Creating heimdalljwt_database_1 ...
Creating heimdalljwt_database_1 ... done
Creating heimdalljwt_myapp_1 ...
Creating heimdalljwt_myapp_1 ... done
Attaching to heimdalljwt_database_1, heimdalljwt_myapp_1
database_1 | 2017-11-25T21:15:39.001+0000 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
database_1 | 2017-11-25T21:15:39.002+0000 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
database_1 | 2017-11-25T21:15:39.003+0000 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
database_1 | 2017-11-25T21:15:39.005+0000 I NETWORK [thread1] waiting for connections on port 27017
myapp_1 | 0|heimdall | Error: Cannot find module 'bcryptjs'
myapp_1 | 0|heimdall | at Function.Module._resolveFilename (module.js:542:15)
myapp_1 | 0|heimdall | at Function.Module._load (module.js:472:25)
myapp_1 | 0|heimdall | at Module.require (module.js:585:17)
myapp_1 | 0|heimdall | at require (internal/module.js:11:18)
myapp_1 | 0|heimdall | at Object.<anonymous> (/usr/src/app/user/User.js:2:14)
myapp_1 | 0|heimdall | at Module._compile (module.js:641:30)
myapp_1 | 0|heimdall | at Object.Module._extensions..js (module.js:652:10)
myapp_1 | 0|heimdall | at Module.load (module.js:560:32)
myapp_1 | 0|heimdall | at tryModuleLoad (module.js:503:12)
myapp_1 | 0|heimdall | at Function.Module._load (module.js:495:3)
myapp_1 | 0|heimdall | at Module.require (module.js:585:17)
myapp_1 | 0|heimdall | at require (internal/module.js:11:18)
myapp_1 | 0|heimdall | at Object.<anonymous> (/usr/src/app/user/UserController.js:12:12)
myapp_1 | 0|heimdall | at Module._compile (module.js:641:30)
myapp_1 | 0|heimdall | at Object.Module._extensions..js (module.js:652:10)
myapp_1 | 0|heimdall | at Module.load (module.js:560:32)
myapp_1 | PM2 | App name:heimdall_app id:0 disconnected
I am uncertain what is going on here, but I am guessing that the dependencies are either not being installed, or not being copied into the working directory. How
UPDATED
Modified from original posting as I still am struggling to get this to work.
You should copy files after npm install. Right now you are installing dependencies and then copying over everything, so you are effectively canceling that install and you don't have dependencies.
In your Dockerfile you have:
...
RUN npm install
RUN npm install pm2 -g
COPY . /usr/src/app
...
It should be:
...
COPY . /usr/src/app
RUN npm install
RUN npm install pm2 -g
...

Seeing error Module not found: 'pg-native' in heroku app

I'm attempting to use the postgres add-on with a heroku node app that is also using react.
I'm using this react buildpack to support react inside heroku:
https://github.com/opike/create-react-app-buildpack.git
When I try to add the line:
var pg = require('pg');
I get the following compilation error when I try to run the heroku app locally:
>heroku local web
[OKAY] Loaded ENV .env File as KEY=VALUE Format
[WARN] ENOENT: no such file or directory, open 'Procfile'
[OKAY] package.json file found - trying 'npm start'
4:15:19 PM web.1 | > test-app#0.1.0 start /Users/oliverpike/dev/heroku/ofp-react
4:15:19 PM web.1 | > react-scripts start
4:15:20 PM web.1 | Starting the development server...
4:15:29 PM web.1 | Failed to compile.
4:15:29 PM web.1 | Error in ./~/pg/lib/native/index.js
4:15:29 PM web.1 | Module not found: 'pg-native' in /Users/oliverpike/dev/heroku/ofp-react/node_modules/pg/lib/native
4:15:29 PM web.1 | # ./~/pg/lib/native/index.js 1:13-33
4:15:29 PM web.1 | Error in ./~/pg/lib/connection-parameters.js
4:15:29 PM web.1 | Module not found: 'dns' in /Users/oliverpike/dev/heroku/ofp-react/node_modules/pg/lib
4:15:29 PM web.1 | # ./~/pg/lib/connection-parameters.js 2:10-24
I was able to get past this issue by doing an:
> npm i pg-native
as described here:
https://github.com/brianc/node-pg-native
as well as an
> npm i dns
for the dns issue.
The odd thing was that I didn't need to explicitly install these packages in my other heroku app that is using postgres.

Resources