Why is Docker suddenly trying to open docker-entrypoint.sh? - node.js

I have dockerised a Node.js app for my Ubuntu server. The Dockerfile is incredibly simple, and had been working fine. Suddenly, on my Ubuntu server, I'm now seeing this error:
sudo docker run -p 80:8080 username/test:test
/bin/sh: 0: Can't open /usr/local/bin/docker-entrypoint.sh
My Dockerfile looks like this:
FROM node:12
# Create the app directory
WORKDIR /Tap2Tap/TapServer
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
There is no reference to this docker-entrypoint.sh and I don't know why it seems to have spontaneously stopped working. Any help would be much appreciated.
Best,
Peter
package.json file is:
{
"name": "Tap2Tap",
"version": "0.0.0",
"scripts": {
"start": "node ./bin/www.js"
},
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1",
"helmet": "^3.22.0",
"https": "^1.0.0",
"mqtt": "^4.0.1",
"pg": "^8.0.3",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^4.4.2"
}

Related

sqlite3 with docker throws MODULE_NOT_FOUND error

My node application works on my local(MacOS), but it does not work if I use docker.
it works if I try local:
npm install
npm start
It throws error if I try docker
docker-compose build
docker-compose up
I'm getting this error.
Error: Cannot find module '/src/node_modules/sqlite3/lib/binding/napi-v6-linux-musl-x64/node_sqlite3.node'
Package.json
{
"name": "api",
"version": "1.0.0",
"description": "API",
"main": "index.js",
"scripts": {
"test": "rm -rf usersdb.sqlite && jest --forceExit",
"test:coverage": "npm run test -- --coverage --forceExit",
"start": "nodemon app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
"mysql": "^2.18.1",
"nodemon": "^2.0.20",
"sqlite3": "^5.1.4"
},
"devDependencies": {
"jest": "^28.1.1",
"supertest": "^6.3.3"
}
}
docker-compose.yml
version: "3.7"
services:
api:
image: test/api
build: ./
command: npm start
restart: on-failure
environment:
SERVER_PORT: 3004
TOKEN_KEY: test123
volumes:
- .:/src
ports:
- "3004:3004"
Dockerfile
FROM node:12.22-alpine as base
WORKDIR /src
COPY package*.json ./
EXPOSE 3004
RUN apk add --no-cache python2 g++ make
RUN npm install
FROM base as dev
ENV NODE_ENV=development
RUN npm install -g nodemon
COPY . ./
CMD ["nodemon", "app.js"]
Delete the volumes: block from your docker-compose.yml.
The volumes: block in your docker-compose.yml file is overwriting your entire application with content from your host system. That includes overwriting the Linux-OS node_modules tree with the MacOS version from your host system.
You don't need this volumes: block. The code and node_modules: tree are built into your image. If you need to develop your application, you can install Node on your host system (on MacOS this might be as little as brew install node) and use that for day-to-day development, even if you're planning to eventually use Docker for final deployment or if you have dependencies that run in containers.

Optimizing Dockerfile for smaller image size

I'm trying to build a docker image which is as small as possible. My project is codded in typescript.
For what I have at the moment, it works just fine, but I'm sure not if my implementation is even good. When I run docker build the image size comes up to 276.23MB.
The problem is that I have to call "tsc" to compile my code into JavaScript which is a dev-dependency.
This is my Dockerfile:
FROM node:16-alpine3.11 as build
WORKDIR /usr/src/app
COPY . /usr/src/app/
RUN \
npm install && \
npm run build
FROM node:16-alpine3.11
WORKDIR /app
COPY --from=build /usr/src/app .
CMD [ "npm", "start" ]
These are the scripts & dependencies in my package.json file:
{
...
"scripts": {
"test": "jest --coverage",
"test:watch": "jest --watch",
"start": "fastify start -l info dist/src/app.js",
"build": "npx rimraf /dist && tsc",
"prebuild": "ts-node scripts/clean",
"docker": "docker build -t barnes-biz/pokemon:1.0.0 .",
"predocker": "npm run build",
},
"dependencies": {
"fastify": "^3.0.0",
"fastify-autoload": "^3.3.1",
"fastify-cli": "^2.13.0",
"fastify-helmet": "^5.3.2",
"fastify-plugin": "^3.0.0",
"fastify-sensible": "^3.1.0",
"fastify-swagger": "^4.8.3",
"mongoose": "^5.13.5",
"weak-napi": "^2.0.2"
},
"devDependencies": {
"#types/jest": "^26.0.23",
"#types/node": "^15.0.0",
"#types/rimraf": "^3.0.1",
"#typescript-eslint/eslint-plugin": "^4.28.0",
"#typescript-eslint/parser": "^4.28.0",
"concurrently": "^6.0.0",
"cross-env": "^7.0.3",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"fastify-tsconfig": "^1.0.1",
"jest": "^27.0.5",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.3",
"ts-node": "^10.0.0",
"typescript": "^4.2.4"
}
}
I don't seem to understand how to do it. Can you help me? Do you also know some best practices that I should follow writing the Dockerfile or anything else? Thank you!
A couple of things:
You want to copy the output of the compiler, not the source files
You still need to install node_modules in the running container, but want to use the production flag to make sure dev dependencies are ignore. The docker file will then look something like this:
FROM node:16-alpine3.11 as build
WORKDIR /usr/src/app
COPY package*.json ./ # A small optimization to allow for caching in between Docker builds
RUN npm ci
COPY . /usr/src/app/
RUN npm run build
FROM node:16-alpine3.11
WORKDIR /app
COPY package*.json ./
RUN npm ci --production
COPY --from=build /usr/build/app . # This line will depend on your tsconfig
RUN ls -la
CMD [ "npm", "start" ]
You could alternatively prune your node_modules in the build image and copy that over as well.

Typescript Looks for Redis Types in wrong location inside Docker

My typescript compiler is looking in the wrong place for the types for one of my modules (redis), but only when run inside of Docker. Here's the end of the logging information running tsc inside the container:
======== Module name '../classes/range' was successfully resolved to '/app/node_modules/#types/semver/classes/range.d.ts' with Package ID '#types/semver/classes/range.d.ts#7.3.4'. ========
... more successful logs ...
node_modules/#types/connect-redis/index.d.ts(22,29): error TS2694: Namespace '"/app/redis"' has no exported member 'RedisClient'.
As you can see, it appears that the tsc command is looking inside of /app/redis for the member RedisClient rather than inside /app/node_modules/#types/redis which does export the type information. The tsc compiler doesn't have any problems when run from my local machine, only when it's being run inside the docker instance. Why is this the case?
Inside docker, the tsc command is being run from my package.json file, which is being run using docker-compose. The docker-compose file looks like this:
version: "3"
services:
api:
image: "typeorm-api:production"
container_name: "typeorm-api"
build:
context: ./
dockerfile: Dockerfile
volumes:
- ./src:/app/src
environment:
ENV: production
PORT: 1234
env_file:
- ".env.production"
# This command will overwrite what is contained in the Dockerfile
command: "npm run start:production"
ports:
- 1234:1234
depends_on:
- redis_server
reverse:
container_name: reverse
hostname: reverse
image: nginx:latest
volumes:
- ${PWD}/nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
- 443:443
redis_server:
image: "redis:alpine"
container_name: redis_server
volumes:
- "./redis/production/data:/var/lib/redis"
expose:
- 6379
The npm run start:production command runs a command that's contained inside my package.json file (which installs my dependencies inside my application on the build step) and looks like this:
"start:production": "tsc --extendedDiagnostics --traceResolution && node dist/index.js"
The part that's failing is the tsc step and gives the error mentioned above.
The development settings work fine. Other files that might be useful in debugging, listed below.
Full package.json file:
{
"name": "myapi",
"version": "0.2.1",
"devDependencies": {
"#types/bcryptjs": "^2.4.2",
"#types/connect-redis": "^0.0.16",
"#types/cors": "^2.8.10",
"#types/express": "^4.17.11",
"#types/express-session": "^1.17.3",
"#types/ioredis": "^4.22.0",
"#types/node": "^14.14.31",
"#typescript-eslint/eslint-plugin": "^4.15.2",
"#typescript-eslint/parser": "^4.15.2",
"eslint": "^7.21.0",
"ts-node": "9.1.1",
"ts-node-dev": "^1.1.6"
},
"dependencies": {
"#types/redis": "^2.8.28",
"apollo-server-express": "^2.21.0",
"bcryptjs": "^2.4.3",
"class-validator": "^0.13.1",
"connect-redis": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-session": "^1.17.1",
"graphql": "^15.5.0",
"ioredis": "^4.23.0",
"pg": "^8.5.1",
"redis": "^3.0.2",
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.1.1",
"typeorm": "0.2.31",
"typescript": "^4.2.2"
},
"scripts": {
"start:development": "ts-node-dev --respawn --poll src/index.ts",
"start:production": "tsc --extendedDiagnostics --traceResolution && node dist/index.js"
}
}
Full Dockerfile
FROM node:15.4.0-alpine3.10
# Specify that when you create the container, put the application inside the /app container
WORKDIR /app
# Copy our package.json + package-lock.json file into the /app folder (wildcard allows for package.lock.json)
COPY package*.json /app/
RUN npm ci
# Copy source files + compilation configuration, db connection options, etc.
COPY ./src .
COPY tsconfig.json .
COPY ormconfig.js .
COPY modules.d.ts .
# This command will be overwritten to be start:production with our docker-compose.prod.yml file
CMD npm run start:development
EXPOSE 1234
I'm so dumb.
I'd named a file that I was using in my project redis.ts and placed it in the root of my project. So, when the compiler was following it's normal rules to look for types, it looked at that file first, and didn't see the type declarations declared in it (because it wasn't the redis.d.ts file).
It didn't look inside the node_modules folder because of the name I chose. After changing the redis.ts file to redisConfiguration.ts everything works fine!

Can't Dockerize my simple typescript website

I am trying to get a Typescript project running in Docker. I created the Docker file based on a tutorial but when I run it I get:
/usr/local/bin/docker-entrypoint.sh: 8: exec: .: Permission denied
I am not sure how to diagnosis this problem.
I can enter the container with
docker run --rm -it d3ca97c88aec bash -il
and run the server with
npm start
I have no file called docker-entrypoint.sh and I didn't make one.
I STRONGLY FEEL THIS IS BECAUSE TYPESCRIPT MAKES A BUILD FOLDER AND THAT BUILD FOLDER IS PERMISSIONED DIFFERENTLY.
Dockerfile
FROM node:12
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
ENV COUCH_URL=NotSafeForStackOverflow
RUN npx tsc
EXPOSE 8080
CMD npx ts-node index.ts
{
"name": "express-react-app",
"version": "1.0.0",
"main": "index.js",
"repository": "",
"author": "",
"license": "",
"private": null,
"dependencies": {
"agentkeepalive": "^4.1.3",
"apollo-utilities": "^1.3.4",
"base64-arraybuffer": "^0.2.0",
"express": "^4.17.1",
"express-graphql": "^0.11.0",
"graphql": "^15.3.0",
"graphql-depth-limit": "^1.1.0",
"graphql-tag": "^2.11.0",
"graphql-type-json": "^0.3.2",
"i": "^0.3.6",
"merge-graphql-schemas": "^1.7.8",
"nano": "^8.2.2",
"node-webcrypto-ossl": "^2.1.1",
"npm": "^6.14.8",
"text-encoding": "^0.7.0",
"ts-node": "^9.0.0",
"typescript": "^3.7.4",
"uuid": "^8.3.0"
},
"scripts": {
"start": "npx ts-node index.ts",
"build": "npx tsc",
"dev": "nodemon --watch '**/*.ts' --exec 'ts-node' index.ts"
},
"devDependencies": {
"#types/express": "^4.17.7",
"#types/graphql": "^14.5.0",
"#types/node": "^14.6.3"
}
}
when you run CMD npx ts-node index.ts
it will overwrite default CMD [ "node" ]
please refer link below to how to use node image :
https://github.com/nodejs/docker-node/blob/master/README.md#how-to-use-this-image
you shoud be able done by using docker-compose :
version: "2"
services:
node:
image: "node:8"
user: "node"
working_dir: /home/node/app
environment:
- NODE_ENV=production
volumes:
- ./:/home/node/app
expose:
- "8081"
command: "npm start"

How to create Docker image of ubuntu for node.js application

I am new to docker build feature. I am trying to create docker image for my node.js application, however I am facing issue.
1) It shows image is create but when I am trying to docker run its give error like
"module.js:550
throw err;
^
Error: Cannot find module '
I think there is some issue with my docker file. Which I have created but I didn't get what exactly issue is
my Dockerfile
FROM ubuntu:16.04
RUN apt-get update -y
RUN apt-get install g++ -y
RUN apt install curl -y
RUN curl --silent --location https://deb.nodesource.com/setup_8.x — Node.js 8 LTS "Carbon" | bash -
RUN apt-get install --yes nodejs
RUN apt-get install --yes build-essential
# check the path
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 9012
CMD ["node", "app.js"]
Also see the my package.json for more info. of modules.
{
"name": "service",
"version": "0.0.0",
"private": true,
"scripts": {
"serve": "nodemon app.js"
},
"description": "test",
"author": {
"name": "caitayl"
},
"dependencies": {
"body-parser": "~1.8.1",
"config": "^1.28.1",
"cookie-parser": "~1.3.3",
"cors": "^2.8.5",
"debug": "~2.0.0",
"ejs": "^2.7.1",
"express": "^4.17.1",
"fabric-ca-client": "~1.4.0",
"fabric-network": "~1.4.0",
"firebase": "^6.4.0",
"firebase-admin": "^8.4.0",
"http-errors": "~1.6.3",
"json-server": "^0.15.0",
"lodash": "^4.17.15",
"method-override": "^2.3.10",
"mongodb": "^3.4.1",
"morgan": "~1.9.1",
"multer": "^1.4.1",
"nexmo": "^2.4.1",
"node-gyp": "^5.0.3",
"node-pre-gyp": "^0.13.0",
"node-rest-client": "^3.1.0",
"os": "^0.1.1",
"paypal-rest-sdk": "^1.8.1",
"qs": "^6.8.0",
"sendotp": "^1.2.9",
"socket.io": "^2.2.0",
"winston": "^2.4.2",
"winston-mongodb": "^2.0.10"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
Why You make nodejs image from ubuntu when there are already official ones.
I see issue - You've overengineered You Dockerfile with unnecessary steps.
Fix Your Dockerfile and try again:
FROM node:8-alpine3.9
RUN apk update
RUN apk add --no-cache g++ make python python-dev
RUN rm -rf /var/cache/apk/*
WORKDIR /app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE 9012
CMD node app.js
From last collaboration I found that it was a typo in code where You required file from not existing folder.
"module.js:550 throw err; ^ Error: Cannot find module '../dFarmUserService/controllers/ClientRegisterController'
So fixed it together :)

Resources