Docker中的NodeJS - 无法访问node_modules

时间:2017-03-01 12:35:30

标签: docker jenkins nodes node-modules

我们在Docker容器中有一个带Jenkins的NodeJS项目。

图像启动后出现问题 - 我们遇到错误:

transform_1 | > node ./build/server.js
transform_1 | 
transform_1 | module.js:472
transform_1 |     throw err;
transform_1 |     ^
transform_1 | 
transform_1 | Error: Cannot find module './lib/express'
transform_1 |     at Function.Module._resolveFilename (module.js:470:15)

在Jenkins构建期间 - 我检查权限并且它们没问题:

...
+ ls -l node_modules/express/lib
total 80
-rw-r--r-- 1 jenkins 1001 14202 Feb 28 12:18 application.js
-rw-r--r-- 1 jenkins 1001  1954 Feb 28 12:18 express.js
drwxr-xr-x 2 jenkins 1001  4096 Feb 28 12:18 middleware
...

但是从容器 - “权限被拒绝”错误:

admin@swarm-master-EB25F3D5-0:~$ docker run -ti automation/cms-transform-layer bash
app@3b86a8e8cc4d:~$ ls -l node_modules/express/
ls: cannot access node_modules/express/lib: Permission denied
ls: cannot access node_modules/express/node_modules: Permission denied
total 124
-rw-r--r-- 1 app app 99111 Feb 28 12:18 History.md
-rw-r--r-- 1 app app  1249 Feb 28 12:18 LICENSE
-rw-r--r-- 1 app app  4541 Feb 28 12:18 Readme.md
-rw-r--r-- 1 app app   224 Feb 28 12:18 index.js
d????????? ? ?   ?       ?            ? lib
d????????? ? ?   ?       ?            ? node_modules
-rw-r--r-- 1 app app  4939 Feb 28 12:18 package.json

到底是什么"d????????? ? ? ? ? ? node_modules"

Dockerfile是:

FROM node:7.5.0

RUN useradd --user-group --create-home --shell /bin/false app

ENV HOME=/home/app

USER root
COPY . $HOME/
RUN chown -R app:app $HOME/*

USER app
WORKDIR $HOME

CMD ["npm", "run", "start:production"]

UPD

package.json中的相关条目:

...
"contentful": "^3.8.0",
"express": "^4.14.0",
"jsum": "^0.1.1",
...

docker-compose.yml的服务定义:

...
transform:
environment:
  - LOGZ_TOKEN=""
  - LOGZIO_API_KEY=""
  - NPM_TOKEN=""
image: "company/transform-layer:latest"
restart: "always"
ports:
  - "3003:3003"
...

这是Jenkins管道脚本:

...
def npmBuildTransform(repoUrl='1', env='2') {
    docker.image('node').inside('-v /var/run/docker.sock:/var/run/docker.sock') {
        git branch: "${BRANCH}", credentialsId: 'github', url: "${repoUrl}"
        stage('Transform build') {
           sh 'apt-get update && apt-get -y install rsync'
           sh 'npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN'
           sh 'npm install --production=false'
           ...

1 个答案:

答案 0 :(得分:0)

解决方案。

我按照Patricsome资源的建议,将所有Node的构建逻辑从Jenkin的脚本移动到Dockerfile。

所以,最终结果是 - 从Jenkins调用Docker构建:

        ...
        def appimage = docker.build("projectname/${imgName}:${TAG}", "--build-arg NPM_TOKEN=${NPM_TOKEN} .")

            appimage.push()
            appimage.push('latest')
        ...

然后我从Dockerfile

进行NPM构建
FROM node:7.5.0

ARG NPM_TOKEN="${NPM_TOKEN}"

RUN useradd --user-group --create-home --shell /bin/false app

ENV HOME=/home/app

USER app
COPY package.json $HOME/

WORKDIR $HOME
RUN pwd && ls -l

RUN npm config set //registry.npmjs.org/:_authToken=${NPM_TOKEN}

RUN npm install --production=false
COPY . $HOME

USER root
RUN chown -R app:app $HOME/

USER app
RUN npm run build:production

CMD ["npm", "run", "start:production"]

这是一种“开发”版本,很少有像RUN pwd && ls -l这样的冗余步骤/层,但是这样可行。