Docker Compose: .env: no such file or directory / NodeJS - node.js

Hello I'm trying to make my docker compose work, but I have the following error:
Step 13/15 : COPY .env . COPY failed: stat
/var/lib/docker/tmp/docker-builder209795817/.env: no such file or
directory
I'm not able to find solutions or imagine what I can do to solve this
code:
DockerCompsoe:
version: "3.7"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: emasa
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
image: emasapg
depends_on:
- dbs
ports:
- "4000:4000"
DockerFile:
FROM node as builder
WORKDIR usr/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node
WORKDIR usr/app
COPY package*.json ./
RUN npm install --production
COPY --from=builder /usr/app/dist ./dist // I GOT PROBLEM HERE
COPY ormconfig.docker.json ./ormconfig.json
COPY .env .
expose 4000
CMD node dist/src/index.js
My package.json:
{
"name": "back-end",
"version": "0.0.1",
"description": "Awesome project developed with TypeORM.",
"scripts": {
"dev:server": "ts-node-dev --respawn --transpileOnly src/index.ts",
"build": "tsc -b"
},
"devDependencies": {
"#types/express": "^4.17.3",
"#types/node": "^13.9.1",
"typescript": "^3.8.3"
},
"dependencies": {
"apollo-server-express": "^2.11.0",
"express": "^4.17.1",
"graphql": "^14.6.0",
"pg": "^7.3.0",
"reflect-metadata": "^0.1.13",
"ts-node": "^8.6.2",
"typeorm": "0.2.24"
}
}
my ormconfig:
{
"type": "postgres",
"host": "db",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "emasa",
"synchronize": true,
"logging": false,
"entities": ["src/entity/**/*.ts"],
"migrations": ["src/migration/**/*.ts"],
"subscribers": ["src/subscriber/**/*.ts"],
"cli": {
"entitiesDir": "src/entity",
"migrationsDir": "src/migration",
"subscribersDir": "src/subscriber"
}
}
and this is my folders structures:

The error appears because there is no .env file and no dist folder.
First of all you need to create a .env file at the root of your project structure in order to provide the needed environment variables (e.g. like name, host, port, password and user of your database connection). Next run npm run build to build your project, which will create the dist folder (see your tsconfig.json).

Related

Docker-compose gives: "node|SyntaxError: Unexpected token import" error when running nodejs app that connects to MongoDB cloud cluster

I have a working nodejs app that uses a MongoDb cluster on the cloud. I am trying to dockerize it using docker-compose up, but I keep getting this error:
nodejs |import Mongodb from 'mongodb';
nodejs | ^^^^^^
nodejs | SyntaxError: Unexpected token import
Here is my dockerFile:
FROM node:12.16.1 as build
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
COPY . .
EXPOSE 80
CMD ["node", "app.js", "daemon off;"]
Here is my docker-compose.yml:
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file: .env
volumes:
- node_modules:/node_modules
command: node app.js
volumes:
node_modules:
Here is my package.JSON file:
{
"type": "module",
"name": "krakenchallenge",
"version": "1.0.0",
"description": "A Node.js app that will filter out valid transactions from given JSON files containging a list of transactions returned from running bitcoind’s rpc call `listsinceblock`. The app is configured to allow working with both SQL or mongoDB types.",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/abuaesh/KrakenChallenge.git"
},
"keywords": [
"node",
"js",
"mongodb",
"sql",
"nosql",
"bitcoin",
"transaction",
"deposit"
],
"author": "Noha Abuaesh <noha.abuaesh#gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/abuaesh/KrakenChallenge/issues"
},
"homepage": "https://github.com/abuaesh/KrakenChallenge#readme",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"#babel/cli": "^7.0.0-beta.46",
"#babel/core": "^7.10.4",
"#babel/plugin-proposal-class-properties": "^7.0.0-beta.46",
"#babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.46",
"#babel/preset-env": "^7.10.4",
"babel-core": "^6.26.3",
"babel-loader": "^8.1.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"bitcoin-address-validation": "^1.0.2",
"dotenv": "^8.2.0",
"fs": "0.0.1-security",
"mongodb": "^3.6.0",
"wallet-address-validator": "^0.2.4"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
The import statement is supported from version 13.2.0 Browser compatibility Table
Or From version 12.0.0, Users must explicitly enable this, using the --experimental-modules runtime flag.
Make sure to rebuild your image. docker-compose up --build will build your images before starting the containers.

SyntaxError: Unexpected token '<' when dockerizing react app

I have a react project with Webpack which I'm trying to dockerize it following this article.
Dockerfile:
FROM node:12.14.1
RUN npm install webpack -g
WORKDIR /tmp
COPY package.json /tmp/
RUN npm config set registry http://registry.npmjs.org/ && npm install
WORKDIR /usr/app
COPY . /usr/app/
RUN cp -a /tmp/node_modules /usr/app/
RUN webpack
ENV NODE_ENV=production
ENV PORT=4000
CMD [ "/usr/local/bin/node", "--experimental-modules", "./src/index.js" ]
EXPOSE 4000
docker-compose.yml:
ex_dashboard:
build: .
ports:
- "80:4000"
volumes:
- .:/usr/app/:rw
environment:
- NODE_ENV=dev
command: >
sh -c '
if test -d node_modules;
then
echo node_modules_exists ;
else
cp -a /tmp/node_modules /usr/app/dashboard;
fi &&
npm install &&
/usr/local/bin/node --experimental-modules ./src/index.js
'
When I run docker-compose up, everything goes well until I encounter this error:
ex_dashboard_1 | (node:18) ExperimentalWarning: The ESM module loader is experimental.
ex_dashboard_1 | file:///usr/app/src/index.js:9
ex_dashboard_1 | <Provider store={store}>
ex_dashboard_1 | ^
ex_dashboard_1 |
ex_dashboard_1 | SyntaxError: Unexpected token '<'
ex_dashboard_1 | at Loader.moduleStrategy (internal/modules/esm/translators.js:66:18)
ex_dashboard_1 | at async link (internal/modules/esm/module_job.js:37:21)
The whole project works well if I just run it by npm start or build it by npm run build. But I don't know why this happens in docker container.
Here is package.json:
{
"name": "explore_dashboard",
"version": "1.0.0",
"description": "A dashboard for the Explore project",
"main": "index.js",
"type": "module",
"scripts": {
"start": "cross-env NODE_ENV=development webpack-dev-server --hot",
"build": "cross-env NODE_ENV=production webpack",
"lint": "eslint ./src",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://gitlab.basalam.dev/data/explore_dashboard.git"
},
"author": "Gh.Sherafati",
"license": "ISC",
"resolve": {
"alias": {
"react-dom": "#hot-loader/react-dom"
}
},
"devDependencies": {
"#babel/core": "^7.8.4",
"#babel/plugin-transform-runtime": "^7.8.3",
"#babel/preset-env": "^7.8.4",
"#babel/preset-react": "^7.8.3",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"cross-env": "^7.0.0",
"css-loader": "^3.4.2",
"eslint": "^6.1.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-loader": "^3.0.3",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.18.0",
"eslint-plugin-react-hooks": "^1.7.0",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.13.1",
"sass-loader": "^8.0.2",
"style-loader": "^1.1.3",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.10.2"
},
"dependencies": {
"#fortawesome/fontawesome-svg-core": "^1.2.27",
"#fortawesome/free-solid-svg-icons": "^5.12.1",
"#fortawesome/react-fontawesome": "^0.1.8",
"#hot-loader/react-dom": "^16.11.0",
"#types/react": "^16.9.19",
"axios": "^0.19.2",
"react": "^16.12.0",
"react-beautiful-dnd": "^12.2.0",
"react-dom": "^16.12.0",
"react-hot-loader": "^4.12.19",
"react-redux": "^7.1.3",
"redux": "^4.0.5",
"redux-saga": "^1.1.3"
}
}
And webpack.config.js:
const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { env } = process;
module.exports = {
entry: './src/index.js',
mode: env.NODE_ENV,
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader', 'eslint-loader'],
},
{
test: /\.s[ac]ss$/i,
use: [
// Creates `style` nodes from JS strings
'style-loader',
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
],
},
],
},
resolve: {
extensions: ['*', '.js', '.jsx'],
},
output: {
path: path.join(__dirname, '/build'),
publicPath: '/',
filename: 'bundle.js',
},
plugins: [
new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(env.NODE_ENV) }),
new HtmlWebpackPlugin({
template: path.resolve('./src/index.html'),
}),
],
devServer: {
contentBase: './build',
hot: true,
},
devtool: env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : undefined,
};
Also index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import store from './redux/store';
import App from './components/App';
import './scss/main.scss';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app'),
);
module.hot.accept();
The --experimental-modules flag can be used to enable support for ECMAScript modules (ES modules).
option 1
CMD ["sh", "-c", "exec node --experimental-modules index.js"]
option 2
CMD exec node --experimental-modules index.js
Full Docs:
In your docker-compose.yml file, delete the volumes: and the rather long-winded command:.
The important thing that’s going on here is that your index.js file actually includes JSX constructs that React uses; it’s not plain Javascript and node can’t run it directly. In your Dockerfile you RUN webpack, which transpiles this into plain Javascript. The volumes: overwrite all of this with whatever content is on your local system; that does not include a transpilation step, so you don’t have valid Javascript for Node to run.
(It might work to put an explicit call to webpack in that command:. This assumes your host and container node_modules are compatible, and it is literally duplicating everything the Dockerfile does to build the image in the docker-compose.yml file.)
Nothing stops you from installing Node on your host system (if you don’t already have it), doing live development using the Webpack dev server, and then using Docker for a final packaging step. That’s probably much more convenient than introducing an isolation system like Docker and then trying to work around all of its features to emulate a local development setup.
Finally I've solve the issue by using nginx and running the build version in the container getting help from this great article.
The new Dockerfile and docker-compose.yml follows:
Dockerfile:
FROM node:12.14.1 as build
WORKDIR /app
COPY . /app
ENV PATH /app/node_modules/.bin:$PATH
RUN npm install
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml:
version: "3"
services:
explore_dashboard:
container_name: explore_dashboard
build:
context: .
dockerfile:
Dockerfile
ports:
- "8081:80"
nginx.conf:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
Now I'm able to run the app on http://127.0.0.1:8081/ running:
docker-compose up

Unable to debug a nodejs app inside of docker

I am trying my hands on debugging a Nodejs app from inside of Docker using Nodemon.
I can debug my app with Nodemon without Docker using Visual Studio Code's Auto Attach feature.
But when I build my docker image and start the container via npm run dev:docker:debug I get following log but debugger is not attached. It might be something with the volume but I am not able to figure it out...
Successfully built 857d9da57565
Successfully tagged app:dev
Creating docker_app_1 ... done
Attaching to docker_app_1
app_1 |
app_1 | > app#1.0.0 dev:debug /usr/src/app
app_1 | > nodemon --config nodemon.json --env=development
app_1 |
app_1 | [nodemon] 2.0.2
app_1 | [nodemon] to restart at any time, enter `rs`
app_1 | [nodemon] watching dir(s): src/**/*
app_1 | [nodemon] watching extensions: ts
app_1 | [nodemon] starting `cross-env NODE_OPTIONS='--inspect=0.0.0.0:5858' ts-node -r tsconfig-paths/register ./src --env=development`
app_1 | Debugger listening on ws://0.0.0.0:5858/k3h42h4-h49d-4f00-adj877-60f6731548787
app_1 | For help, see: https://nodejs.org/en/docs/inspector
app_1 | Service started at ports:3000
Folder structure
App
|-- docker
| |-- docker-compose.yml
| |-- Dockerfile
| `-- .dockerignore
|-- nodemon.json
|-- package.json
|-- tsconfig.json
|-- tslint.json
`-- src
`-- index.ts
index.ts
import express, { Request, Response } from "express";
const app = express();
const port = process.env.PORT || 3000; // default port to listen
// define a route handler for the default home page
app.get("/", (req: Request, res: Response) => {
res.send("Hello worlds!");
});
// start the Express server
app.listen(port, () => {
console.log(`Service started at ports:${port}`);
});
docker-compose.yml
# docker-compose.yml
version: "3"
services:
app:
image: app:dev
build:
context: ../
dockerfile: ./docker/Dockerfile
ports:
- 3000:3000
volumes:
- ../src:/usr/src/app/src/
Dockerfile
FROM node:12-slim
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . ./
CMD [ "npm", "run", "dev:debug" ]
package.json
{
"name": "app",
"version": "1.0.0",
"description": "API to receive data",
"author": "Nikhil Gupta",
"main": "./dist/index.js",
"scripts": {
"dev:debug": "nodemon --config nodemon.json --env=development",
"dev:docker:debug": "docker-compose -f ./docker/docker-compose.yml up --build"
},
"dependencies": {
"cross-env": "^6.0.3",
"express": "^4.17.1",
"ts-node": "^8.5.4",
"tsconfig-paths": "^3.9.0",
"tslib": "^1.10.0"
},
"devDependencies": {
"#types/express": "^4.17.2",
"#types/node": "^12.12.21",
"nodemon": "^2.0.2",
"tslint": "^5.20.1",
"typescript": "^3.7.4"
}
}
nodemon.json
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/public"],
"inspect": true,
"exec": "cross-env NODE_OPTIONS='--inspect=0.0.0.0:5858' ts-node -r tsconfig-paths/register ./src"
}
Since the debugger is listening on port 5858 inside the container, you need to bind it to a port on the host machine if you want to connect to it from outside the container similar to how you have done for port 3000 in your compose file.
# docker-compose.yml
version: "3"
services:
app:
image: app:dev
build:
context: ../
dockerfile: ./docker/Dockerfile
ports:
- 3000:3000
- 5858:5858
volumes:
- ../src:/usr/src/app/src/
As mentioned in the comments by #ykit9 I did following
1) Added port 5858 into my docker-compose.yml
# docker-compose.yml
version: "3"
services:
app:
image: app:dev
build:
context: ../
dockerfile: ./docker/Dockerfile
ports:
- 3000:3000
- 5858:5858
volumes:
- ../src:/usr/src/app/src/
2) Added following configuration in VS Code's launch.json file.
{
"type": "node",
"request": "attach",
"name": "Attach to Docker",
"protocol": "auto",
"port": 5858,
"restart": true,
"localRoot": "${workspaceFolder}/src",
"remoteRoot": "/usr/src/app/src"
}

I can curl another docker container inside docker compose but not fetch in code

I can't fetch from server container to frontend container even though I am able to get curl response from inside frontend container.
I have a docker-compose set up with a frontend and server container.
I want to fetch server response from frontend, but i get the error GET http://server:5000/api/panels net::ERR_NAME_NOT_RESOLVED. I get the correct response if i execute the following command from inside the frontend container curl http://server:5000/api/panels
Dockerfile server
FROM node:11
ADD . /code
WORKDIR /code
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD [ "node", "server" ]
Dockerfile frontend
FROM node:11
ADD . /frontend
WORKDIR /frontend
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm","start"]
docker-compose.yml
version: '3'
services:
server:
build: .
frontend:
depends_on:
- server
build: ./frontend
ports:
- "3030:3000"
api-call
this.callApi()
.then((res: ISolarPanels) => {
this.setState({ solarPanels: res })
})
.catch((err) => console.warn('server is offline?', err))
private callApi = async () => {
const response = await fetch(process.env.REACT_APP_URL+'/api/panels')
const body = await response.json()\
if (response.status !== 200) {
throw Error(body.message)
}
return body
}
package.json (i use dev script for local development and start for docker
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"#types/d3": "^5.0.1",
"d3": "^5.7.0",
"prettier": "^1.13.5",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-scripts-ts": "2.16.0",
"tslint-eslint-rules": "^5.3.1"
},
"scripts": {
"lint": "node_modules/.bin/tslint -c tslint.json 'src/**/{*.ts,*.tsx}'",
"dev": "REACT_APP_URL=http://localhost:3000 react-scripts-ts start",
"start": "REACT_APP_URL=http://server:5000 react-scripts-ts start",
"build": "react-scripts-ts build",
"test": "react-scripts-ts test --env=jsdom",
"eject": "react-scripts-ts eject"
},
"proxy": "http://localhost:5000/",
"devDependencies": {
"#types/node": "^10.3.3",
"#types/react": "^16.3.17",
"#types/react-dom": "^16.0.6",
"typescript": "^2.9.2"
}
}
For a quick fix, you can allow CORS on your server. here
And in your web application you can use:
0.0.0.0:5000/api for pointing to server.
Also, you would need to bind your port in server service in docker-compose.yml file.
server:
build: .
ports:
- "5000:5000"
To sum up what went wrong and how it was fixed.
The request was of-course sendt from the browser, and not the frontend container, therefore the server port needed to be exposed and CORS enabled from the serverside.

docker Cannot find module 'Sequelize'

Getting below error after deploying docker image:
Cannot find module 'Sequelize'
Directory structure:
src
views
Dockerfile
package.json
Dockerfile:
FROM node:8
ENV PORT 3000
EXPOSE 3000
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
CMD ["npm", "run", "prod"]
Package.json:
{
"name": "api",
"version": "0.0.0",
"private": true,
"main": "src/loader.js",
"scripts": {
"dev": "nodemon",
"prod": "node src/loader.js"
},
"dependencies": {
"sequelize": "^4.38.0",
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.16.0",
"express-graphql": "^0.6.12",
"graphql": "^0.13.2",
"graphql-relay": "^0.5.5",
"graphql-sequelize": "^9.0.1",
"http-errors": "~1.6.2",
"jade": "^1.11.0",
"morgan": "~1.9.0",
"mysql2": "^1.6.1"
},
"devDependencies": {
"nodemon": "^1.18.3"
}
}
Could it be a typo, ie can you verify that you are using require('sequelize') instead of require('Sequelize').
Maybe you need to explicitly add the module in your package.json
$ npm install --save sequelize
If the module is declared, it will be found by the dependent module that is causing you that error.
Hope it helps!

Resources