简体   繁体   中英

I need to run twice docker-compose to not have errors

I'm learning to use Docker in real world developement environment, so I created a very simple "Hello World" app in Node.js, with Docker.

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 (the entry point of the app):

var express = require('express');

express()
  .get('/', function(req, res) {
      res.send('Success');
  })
  .listen(3000, function(err) {
      if (!err) { console.log('Server started') }
  });

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

To summarize: In package.json I add express as a dependency to create a basic server in server.js . In Dockerfile.dev I define the image I need for this environment. Note that I copied package.json in the /app working directory and then run npm install . Finally, I define my environment with docker-compose.yml . Pretty basic (from many sources to avoid some pitfalls, like adding /app/node_modules in docker-compose volumes)

The problem is that running docker-compose up produces the following error:

$ 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

And then re-running the same command (without modifying any code), it works:

$ docker-compose up

Starting dockerrealworld_web_1
Attaching to dockerrealworld_web_1
web_1  | Server started

Why doesn't it work the first time? What did I do wrong?

You almost have it. The Dockerfile needs to...

# 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"]

You've got the write idea, but docker images take building from the ground up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM