我正在使用 Nodejs 和 Express 运行一个简单的应用程序。 我正在尝试创建应用程序的 docker 映像,并使用 docker compose 创建包含该映像的容器。 我想将生成的容器中的工作区中的文件(在本例中为 index.js、package.json 和其他文件)挂载到 docker-compose.yml 文件所在目录中的 ./test ,但我得到了错误。
Index.js
const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
res.send("Hello Docker Compose!");
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
Package.json
{
"name": "my-node-app",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"express": "^4.17.1"
},
"scripts": {
"start": "node index.js"
}
}
Dockerfile
FROM node:14
WORKDIR /app
COPY package.json /app
COPY index.js /app
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]
“docker build -texpress-app:latest”我正在使用命令构建应用程序。
在另一个名为 test-app 的文件夹中,我使用命令 “docker compose up”运行以下 docker-compose.yml 文件
容器中workdir中的文件没有进入./test文件夹,并且出现以下错误。
Attaching to test-app-express-app-1
test-app-express-app-1 | npm ERR! code ENOENT
test-app-express-app-1 | npm ERR! syscall open
test-app-express-app-1 | npm ERR! path /app/package.json
test-app-express-app-1 | npm ERR! errno -2
test-app-express-app-1 | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
test-app-express-app-1 | npm ERR! enoent This is related to npm not being able to find a file.
test-app-express-app-1 | npm ERR! enoent
test-app-express-app-1 |
test-app-express-app-1 | npm ERR! A complete log of this run can be found in:
test-app-express-app-1 | npm ERR! /root/.npm/_logs/2023-11-17T15_23_04_691Z-debug.log
test-app-express-app-1 exited with code 254
我不明白我做错了什么。你能帮我吗?
您构建的图像中似乎没有
package.json
。docker build -t express-app:latest
时。
构建 docker 镜像时没有遇到错误吗?
就像来自控制台的以下错误。
=> ERROR [3/4] COPY package.json /app
您可以修改 dockerfile 以了解 docker 组合时里面的内容。
FROM node:14
WORKDIR /app
COPY package.json /app
COPY test.js /app
CMD "tail -f /dev/null"