gpt4 book ai didi

node.js - Windows 10 Home 上的 Docker Compose 找不到任何文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:40:25 26 4
gpt4 key购买 nike

我正在尝试学习 Docker 和 Docker Compose 来设置一个小型示例 Node/express 服务器。我在 Windows 10 家庭版上运行 Docker 工具箱。以下是我的项目结构

root/ 
docker-compose.yml
Dockerfile
index.js
package.json

这是我的 Dockerfile

FROM node:8

WORKDIR /usr/app

COPY . .
RUN npm install --quiet

这是我的 docker-compose.yml

version: "2"
services:
node:
build: .
working_dir: /usr/app
environment:
- NODE_ENV=production
expose:
- "3000:3000"
command: "node index.js"

这是我的 package.json

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
}
}

这是我的index.js

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

我将在每个组件的文档中找到的各种示例混合在一起。但我无法让它运行。当我执行 docker-compose up 时,我得到以下信息:

$ docker-compose up
Building node
Step 1/5 : FROM node:8
8: Pulling from library/node
61be48634cb9: Pull complete
fa696905a590: Pull complete
b6dd2322bbef: Pull complete
32477089adb4: Pull complete
febe7209ec28: Pull complete
4364cbe57162: Pull complete
ace5c680ff94: Pull complete
4acd6a9b7a48: Pull complete
Digest: sha256:7b65413af120ec5328077775022c78101f103258a1876ec2f83890bce416e896
Status: Downloaded newer image for node:8
---> 82c0936c46c1
Step 2/5 : WORKDIR /usr/app
---> Running in 454ed6735e6e
Removing intermediate container 454ed6735e6e
---> 62d35be2db4c
Step 3/5 : COPY package.json .
---> fdbb25021d83
Step 4/5 : RUN npm install --quiet
---> Running in e535ef0bc18f
npm WARN test@1.0.0 No description
npm WARN test@1.0.0 No repository field.

added 48 packages from 36 contributors and audited 121 packages in 3.179s
found 0 vulnerabilities

Removing intermediate container e535ef0bc18f
---> 1a61e872c386
Step 5/5 : COPY . .
---> 56e765cf1c3c
Successfully built 56e765cf1c3c
Successfully tagged root_node:latest
WARNING: Image for service node was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating root_node_1 ... done
Attaching to root_node_1
node_1 | module.js:550
node_1 | throw err;
node_1 | ^
node_1 |
node_1 | Error: Cannot find module '/usr/app/index.js'
node_1 | at Function.Module._resolveFilename (module.js:548:15)
node_1 | at Function.Module._load (module.js:475:25)
node_1 | at Function.Module.runMain (module.js:694:10)
node_1 | at startup (bootstrap_node.js:204:16)
node_1 | at bootstrap_node.js:625:3
root_node_1 exited with code 1

我确实在谷歌搜索中发现 Windows 和文件权限存在一些问题,但我找不到任何更详细的信息。

编辑 1:我已从 docker-compose.yml 中删除了卷,当我使用适用于 MacOS 的 Docker CE 运行时,它工作正常。同样的事情在Windows中用Docker工具仍然找不到index.js

最佳答案

请您尝试以下操作。

在你的index.js中,暴露端口是3000,所以我们将在DockerFile中暴露相同的端口。

在docker-compose.yml中,完成了5000到3000端口的映射。其中 5000 个将作为主机 (Windows),3000 个客户端 (Linux)。

Dockerfile:

FROM node:8

WORKDIR /usr/app

COPY package.json .
RUN npm install --quiet

COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]

docker-compose.yml

version: '3'
services:
web:
build: .
ports:
- "5000:3000"

关于node.js - Windows 10 Home 上的 Docker Compose 找不到任何文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138923/

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