gpt4 book ai didi

node.js - 无法在Docker内部启动 Node 应用程序

转载 作者:行者123 更新时间:2023-12-02 19:15:47 26 4
gpt4 key购买 nike

在docker内部创建节点应用程序时出现以下错误

docker container run -it --rm --volume $(pwd):/usr/src/app -p 7007:3000 sample-app-dev:latest
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"nodemon server.js\": executable f
我的Dockerfile如下所示
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["nodemon server.js"]
Package.json
{
"name": "nodedocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}

server.js
const express = require( "express" );

const app = express();
const port = 3000;

app.get( "/", ( req, res ) => {
res.send( "Hello World from express JS!!" );
} );

const hobbies = [
"Swimming", "Diving", "Jogging", "Cooking", "Singing" ] ;

app.get("/hobbies", (req, res) => {
res.send(hobbies);
});

const famous_programming = [
"python", "java", "c", "c++", "JS" ] ;

app.get("/famous_programming", (req, res) => {
message = famous_programming.includes("node")? "Yaaay, Node.js" : "Yup! Node.js is a framework from JS" ;
res.send(message);
});

app.listen( port, () => {
console.log( `Node JS started on http://localhost:${port}` )
} );


我不确定我在这里还缺少什么,任何输入都会受到赞赏。
谢谢。

最佳答案

您可以将包配置为在启动时运行nodemon server.js,然后在入口点中指定npm start:
Dockerfile:

FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["npm", "start"]
package.json:
{
"name": "nodedocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}
测试:
$ docker container run -it --rm q63534772
> nodedocker@1.0.0 start /usr/src/app
> nodemon server.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Node JS started on http://localhost:3000

关于node.js - 无法在Docker内部启动 Node 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63534772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com