I am new to Docker. I have an application which runs on apache2 and nodejs (running on pm2) with postgresql database.
I am trying to create a Dockerfile and package.json file to the existing project(which mentioned above) but I am not able to proceed further as sometimes I am getting error as npm problem.
I am trying to do it sample nodejs with apache running but I am getting this error .. curl: (52) Empty reply from server.
My sample Dockerfile is
FROM node:4.2.6
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
#RUN npm install pm2 -g
COPY . .
EXPOSE 4000
CMD ["npm", "start"]
My Package.json is
{
"name": "pm2",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
Please help me out
Related
Setting up my dev environment on Docker. I am running Nodejs v16.16.0.
I am using Nodemon and a docker volume to keep my dev work in sync with the docker container.
Dockerfile.dev
FROM node:alpine
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "run", "dev"]
package.json
{
"name": "docker-example-1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index",
"dev": "nodemon index",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Kane Hooper <kanehooper#hotmail.com>",
"license": "MIT",
"dependencies": {
"express": "^4.18.1"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
I execute docker run -p 8080:3000 -v $(pwd):/usr/src/app -v /usr/src/app/node_modules --name nodeappcontainer nodeapp
I get the following error /tmp/dev6585779965.sh: line 2: nodemon: Permission denied
Any assistance on how to resolve this would be much appreciated.
The problem resolved when I bumped the version of nodemon from 2.0.18 down to 2.0.16.
I've raised an issue on the nodemon github repo.
I am trying to setup Nodemon in a Docker container. It says that nodemon is running, but when I change code in my index.js file it does not reload like it does outside of docker. I've tried adding -L to the command, but no luck. I've also tried installing nodemon in the docker file instead, but no luck.
I have to do docker-compose up --build anytime I change my index.js file.
Any ideas?
Here is my file structure:
-api
-node_modules
-.dockerignore
-Dockerfile
-index.js
-package.json
-package-lock.json
-docker-compose.yml
docker-compose.yml:
version: '3.4'
services:
api:
build:
context: ./api
container_name: api
environment:
- PORT=3001
volumes:
- ./api/src:/usr/app/src
ports:
- '3001:3001'
command: npm run dev
Dockerfile:
FROM node:14.15.2-alpine3.12
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
package.json:
{
"name": "api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"dev": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^2.0.6"
}
}
I FIGURED IT OUT!!!
After a lot of trial and error. It has to do with my volumes in my docker compose as well as nodemon. Not 100% sure why any insight would be helpful too.
The fix was to change my volume from
- ./api/src:/usr/app/src
to:
- ./api:/usr/src/app
Then I had to add the -L flag to my nodemon command in order for it to reload.
I have created a node application using typescript.
{
"name": "my-app",
"version": "1.0.0",
"main": "index.ts",
"author": "",
"license": "MIT",
"scripts": {
"start": "node -r ts-node/register index.ts",
},
"dependencies": {
"#types/express": "^4.17.3",
},
"devDependencies": {
"ts-node": "^7.0.1",
"typescript": "^3.4.5"
}
}
Currently, I have used following docker file for running my application
FROM node:10
WORKDIR /app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE 1234
CMD ["npm", "run", "start"]
I want to run my application using node command instead of npm
FROM node:10
WORKDIR /app
COPY package*.json ./
RUN npm i
COPY . .
EXPOSE 1234
CMD ["node", "-r", "ts-node/register", "index.ts"]
But it throws an error like this
'egister", "index.ts"]' is not recognized as an internal or external command,
operable program or batch file
The ts-node is not registered in the WORKDIR environment, you need to add the relative path.
CMD ["node", "-r", "./node_modules/ts-node/register", "index.ts"]
If you want to run other packages, you need to register the path like this
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
I'm having an issue trying to generate a dockerfile for my nodejs app:
My dockerfile:
FROM node
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["node", "/app/index.js"]
The nodejs (As part of npm install) needs grpc. When I try to run my app, I get the following error message:
Cannot find module '/app/node_modules/grpc/src/node/extension_binary/node-v57-linux-x64/grpc_node.node'
When I explore the app/node_modules/grpc/src/node/extension_binary/ folder, node-v48-win32-x64 is the only folder inside there. My guess is when npm install ran, it used the context my host machine where it detected windows/x64 and downloaded that binary instead. I'd like to avoid running npm install at runtime. How do I fix this?
My package.json:
{
"name": "microservice-test",
"version": "1.0.0",
"description": "A test microservice.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "FrankerZ",
"license": "ISC",
"devDependencies": {
"grpcc": "0.0.8",
"gulp-livereload": "^3.8.1"
},
"dependencies": {
"async": "^2.5.0",
"grpc": "^1.6.0",
"gulp": "^3.9.1",
"gulp-run": "^1.7.1",
"gulp-util": "^3.0.8",
"protoc-plugin": "0.0.6"
}
}
What I think is happening is, docker run is copying the local node_modules from your project inside the container at COPY . /app
.
So thus you get the linux-x64 error. It basically copied all the machine specific code from node_modules to the container that must have another OS. To fix this, ignore node_modules by making a .dockerignore file alongside your package.json and add just one line.
node_modules
Read more about it from here.
I am struggling on deploying my backend to the docker container. I found out, that my ploblem is the express framework. First of all, I must say, that everything works on my localhost fine. But when I deploy my app to the server I am getting the 503 Error (service is not availible). If I initialize my app NOT via express, than everything is also working inside the docker container. So what could be the problem? Here is my docker code:
FROM node:alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
EXPOSE 9080
CMD [ "npm", "start" ]
And here is my node code:
var express = require('express');
var app = express();
app.listen(process.env.PORT || 9080);
app.get('/test', function(req, res){
res.send("hi");
});
And here is my package.json:
{
"name": "server",
"version": "1.0.0",
"description": "Backend Shareco",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "Shareco GmbH",
"license": "ISC",
"dependencies": {
"multer": "1.1.0",
"mysql": "^2.13.0",
"express": "^4.14.0"
}
}
Why does it not work? Would be thankful for any help.
Kind regards,
Andrej
After you run docker build, when you run docker run you need to implement port forwarding by using the -p tag.
In your case, you would run
docker run -p 5000:9080 <image id>
Now when you go to http://localhost:5000 you should see your app running.