我正在尝试学习Docker和Docker Compose来设置一个小的示例节点/快速服务器。我正在Windows 10 Home上运行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
我确实发现Google和文件权限存在一些问题,但是我找不到更详细的信息。
编辑1:我已经从docker-compose.yml中删除了卷,当我在Mac OS上使用Docker CE运行时,它工作正常。使用Docker工具在Windows中仍然找不到相同的东西
答案 0 :(得分:0)
请您尝试以下方法。
在您的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"