-1

I am trying to create a docker container for my application but there seems to be an error with my Docker script. Whenever I run the command docker build - < Dockerfile, I get the following output:

enter image description here

I'm not entirely sure why this is happening, since my folder layout is the following:

root folder, Docker ---
                       server ---- package.json
                                   api
                                   tests

In case the folder layout was a bit confusing, I have my docker file inside my root folder and inside the root folder is the folder called server which contains my package.json, my api files and the tests.

Here's my docker script:

# --- Base Node ---
FROM alpine:3.8 AS base
#install node
RUN apk add  --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.7/main/ nodejs=8.9.3-r1 tini
# set working directory
WORKDIR /usr/src/app
# set tini as entrypoint
ENTRYPOINT ["/sbin/tini", "--"]
# copy project file
COPY . server/package*.json ./

# --- Dependencies ---
FROM base AS dependencies
# install node packages
RUN npm set progress=false && npm config set depth 0
RUN npm install .
# copy production node_modules aside
RUN cp -R node_modules prod_node_modules
# install ALL node_modules, including 'devDependencies'
RUN npm install

#
# ---- Test ----
# run linters, setup and tests
FROM dependencies AS test
COPY . .
RUN  npm run lint && npm run test

#
# ---- Release ----
FROM base AS release
# copy production node_modules
COPY --from=dependencies /root/server/prod_node_modules ./node_modules
# copy app sources
COPY server/ ./
# expose port and define CMD
EXPOSE 3003
CMD [ "npm", "start" ]

I've used this question as an example.

Why am I getting this error from my docker script?

  • Sidenote: Node has official base images, also alpine-based ones: https://hub.docker.com/_/node/ – k0pernikus Jul 16 '19 at 12:35
  • @k0pernikus I'm a complete Docker noob, it's hard for me to even incorporate this inside my script. Sorry for the hassle.. – someonewithakeyboardz1 Jul 16 '19 at 12:35
  • Try creating the `./node_modules` first in your release image. Either via `WORKDIR` or qick and dirty via `RUN mkdir ./node_modules` before your ``COPY --from=dependencies /root/server/prod_node_modules ./node_modules ` line. – k0pernikus Jul 16 '19 at 12:40
  • @k0pernikus this is what I got as output: https://i.imgur.com/gPAwRqB.jpg – someonewithakeyboardz1 Jul 16 '19 at 12:46
  • Please post formatted text, not pictures of text. Pictures cannot be searched in the future, and those that wish to answer your question have to manually retype any relevant comment from a picture. – BMitch Jul 16 '19 at 13:22
  • @BMitch that's fine, I can edit the question. Thanks, haven't thought about that. – someonewithakeyboardz1 Jul 16 '19 at 13:25

1 Answers1

1

The node_modules need was generated by npm install according to parse package.json.

But, not all package.json will make npm install generate this folder, only the one with devDependencies, dependencies could results in the generate of nodes_moduls folder.

I give you a sample package.json which could manage that:

package.json which will generate node_modules:

{
    "name": "my-demo",
    "version": "1.0.0",
    "description": "a project",
    "main": "index.js",
    "scripts": {
        "build": "weex-builder src dist",
        "build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
        "dev": "weex-builder src dist -w",
        "serve": "serve -p 8080"
    },
    "keywords": [
        "weex"
    ],
    "author": "xxx@gmail.com",
    "license": "MIT",
    "devDependencies": {
        "babel-core": "^6.14.0"
    },
    "dependencies": {
        "weex-html5": "^0.3.2"
    }
}

Above build will ends ok with next:

Step 8/10 : RUN npm install .
 ---> Running in 181f572843bc
 > core-js@2.6.9 postinstall /usr/src/app/node_modules/core-js
 > node scripts/postinstall || echo "ignore"
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo@1.0.0 No repository field.
added 53 packages in 6.491s
Removing intermediate container 181f572843bc
 ---> 016c98d9650d
Step 9/10 : RUN cp -R node_modules prod_node_modules
  ---> Running in c24631cc4bc6
 Removing intermediate container c24631cc4bc6
  ---> de413db9140c

But if you remove devDependencies & dependencies like next:

package.json which won't generate node_modules:

{
    "name": "my-demo",
    "version": "1.0.0",
    "description": "a project",
    "main": "index.js",
    "scripts": {
        "build": "weex-builder src dist",
        "build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
        "dev": "weex-builder src dist -w",
        "serve": "serve -p 8080"
    },
    "keywords": [
        "weex"
     ],
    "author": "xxx@gmail.com",
    "license": "MIT"
 }

It will results in:

Step 8/10 : RUN npm install .
 ---> Running in 45031bd21886
 npm notice created a lockfile as package-lock.json. You should commit this file.
 npm WARN my-demo@1.0.0 No repository field.
up to date in 0.115s
Removing intermediate container 45031bd21886
 ---> f88364d0725d
Step 9/10 : RUN cp -R node_modules prod_node_modules
  ---> Running in 16cd11546db0
 cp: can't stat 'node_modules': No such file or directory
 The command '/bin/sh -c cp -R node_modules prod_node_modules' returned a non-zero code: 1
atline
  • 28,355
  • 16
  • 77
  • 113