Node.js 我需要运行两次docker compose以避免出现错误

Node.js 我需要运行两次docker compose以避免出现错误,node.js,docker,docker-compose,Node.js,Docker,Docker Compose,我正在学习在真实的开发环境中使用Docker,所以我在Node.js中用Docker创建了一个非常简单的“Hello world”应用程序 package.json { "name": "docker-real-world", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" &&a

我正在学习在真实的开发环境中使用Docker,所以我在Node.js中用Docker创建了一个非常简单的“Hello world”应用程序

package.json

{
  "name": "docker-real-world",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0"
  }
}
server.js(应用程序的入口点):

Dockerfile.dev

FROM mhart/alpine-node:5.6.0

WORKDIR /app
ENV NODE_ENV development
EXPOSE 3000

ADD package.json /app
RUN npm install
docker compose.yml

version: "2"
services:
  web:
    build:
        dockerfile: Dockerfile.dev
        context: .
    volumes:
        - .:/app
        - /app/node_modules
    ports:
        - 80:3000
    command: node server.js
总结:在package.json中,我添加了express作为依赖项,以在server.js中创建基本服务器。在Dockerfile.dev中,我定义了此环境所需的映像。请注意,我复制了
/app
工作目录中的package.json,然后运行
npm install
。最后,我用docker compose.yml定义我的环境。非常基本(来自许多来源,以避免一些陷阱,例如在docker compose卷中添加
/app/node_模块

问题是运行
docker compose up
会产生以下错误:

$ docker-compose up

Building web
Step 1 : FROM mhart/alpine-node:5.6.0
 ---> cdbaa8672a25
...
...
web_1  | module.js:341
web_1  |     throw err;
web_1  |     ^
web_1  |
web_1  | Error: Cannot find module 'express'
web_1  |     at Function.Module._resolveFilename (module.js:339:15)
web_1  |     at Function.Module._load (module.js:290:25)
web_1  |     at Module.require (module.js:367:17)
web_1  |     at require (internal/module.js:16:19)
web_1  |     at Object.<anonymous> (/app/server.js:1:77)
web_1  |     at Module._compile (module.js:413:34)
web_1  |     at Object.Module._extensions..js (module.js:422:10)
web_1  |     at Module.load (module.js:357:32)
web_1  |     at Function.Module._load (module.js:314:12)
web_1  |     at Function.Module.runMain (module.js:447:10)
dockerrealworld_web_1 exited with code 1

为什么第一次就不行了?我做错了什么?

你差点就搞错了。Dockerfile需要

# Create the location of our app within the image
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Copy the package json containing dependencies, then install
COPY package.json /usr/src/app/
RUN npm install

# Copy the current source to the image
COPY . /usr/src/app

# Start the service
ENTRYPOINT ["npm", "start"]

你有写作的想法,但是docker images从底层开始构建。

你能试着分别构建这些图像吗?也许你可以教我--docker compose.yml中的/app/node\u modules卷的目的是什么?卷
/app/node\u modules
的目的是保存container node\u modules文件夹中的内容。否则,另一个卷
:app
将隐藏/覆盖node\u modules文件夹。
$ docker-compose up

Starting dockerrealworld_web_1
Attaching to dockerrealworld_web_1
web_1  | Server started
# Create the location of our app within the image
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Copy the package json containing dependencies, then install
COPY package.json /usr/src/app/
RUN npm install

# Copy the current source to the image
COPY . /usr/src/app

# Start the service
ENTRYPOINT ["npm", "start"]