【问题标题】:yarn doesn't create node_modules folder in docker containeryarn 不在 docker 容器中创建 node_modules 文件夹
【发布时间】:2020-03-21 03:01:28
【问题描述】:

我正在尝试使用 dockerfile 和 docker_compose 构建应用程序。一切似乎都很好,直到 yarn 命令没有创建 node_module 也没有安装依赖项。

docker-compose 文件:

version: '2'

services:
  mdisassistant:
    env_file: .env
    build:
      context: .
      args:
        - APP_NAME=${APP_NAME}
        - APP_USER=app
    ports:
      - ${APP_PORT}:${APP_PORT}

Dockerfile:

FROM node:10.15.2-alpine

ARG APP_NAME
ARG APP_USER

RUN adduser -D -s /bin/false -h /home/$APP_USER $APP_USER $APP_USER

ENV HOME=/home/$APP_USER
WORKDIR $HOME/$APP_NAME
COPY . .
RUN chown $APP_USER:$APP_USER -R $HOME
USER $APP_USER

RUN whoami

RUN yarn install

RUN ls -la 
RUN pwd

RUN yarn build

CMD ["node", "./dist/index.js"]

输出:

Building mdisassistant
Step 1/15 : FROM node:10.15.2-alpine
---> 072459fe4d8a
Step 2/15 : ARG APP_NAME
---> Using cache
---> 4c30b08b312d
Step 3/15 : ARG APP_USER
---> Using cache
---> 9631ef748cd7
Step 4/15 : RUN adduser -D -s /bin/false -h /home/$APP_USER $APP_USER $APP_USER
---> Using cache
---> f5d045ca5282
Step 5/15 : ENV HOME=/home/$APP_USER
---> Using cache
---> 32c4f9457b9e
Step 6/15 : WORKDIR $HOME/$APP_NAME
---> Using cache
---> a8d71a9f563f
Step 7/15 : COPY . .
---> d4ef17f02a9f
Step 8/15 : RUN chown $APP_USER:$APP_USER -R $HOME
---> Running in f6c194316e12
Removing intermediate container f6c194316e12
---> f6742a0c10df
Step 9/15 : USER $APP_USER
---> Running in ec22ed655aa5
Removing intermediate container ec22ed655aa5
---> af800732027d
Step 10/15 : RUN whoami
---> Running in ba9fa81a95a3
app
Removing intermediate container ba9fa81a95a3
---> ebf0f6a4f8a7
Step 11/15 : RUN yarn install
---> Running in 4d5e76dd1508
yarn install v1.13.0
[1/4] Resolving packages...
[2/4] Fetching packages...
Removing intermediate container 4d5e76dd1508
---> 1785eec9829e
Step 12/15 : RUN ls -la
---> Running in b0f3f1b1e5fc
total 92
drwxr-sr-x    1 app      app           4096 Mar 21 02:52 .
drwxr-sr-x    1 app      app           4096 Mar 21 02:52 ..
-rw-r--r--    1 app      app           1610 Mar 21 02:05 .dockerignore
-rw-r--r--    1 app      app             62 Mar 21 00:56 .env.example
drwxr-xr-x    1 app      app           4096 Mar 21 02:52 .git
-rw-r--r--    1 app      app           1610 Mar 20 23:56 .gitignore
-rw-r--r--    1 app      app            333 Mar 21 02:52 Dockerfile
-rw-r--r--    1 app      app           1060 Mar 20 23:56 LICENSE
-rw-r--r--    1 app      app            681 Mar 20 23:56 README.md
-rw-r--r--    1 app      app            280 Mar 21 02:35 docker-compose.yml
-rw-r--r--    1 app      app            614 Mar 21 01:57 package.json
drwxr-xr-x    1 app      app           4096 Mar 21 01:05 src
-rw-r--r--    1 app      app          44193 Mar 21 01:20 yarn.lock
Removing intermediate container b0f3f1b1e5fc
---> eb6fbee4548f
Step 13/15 : RUN pwd
---> Running in 92b3d6d20201
/home/app/mdisassistant
Removing intermediate container 92b3d6d20201
---> 853d9879da99
Step 14/15 : RUN yarn build
---> Running in 205ef8079386
yarn run v1.13.0
$ ncc build src/index.js -o dist -m
/bin/sh: ncc: not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: Service 'mdisassistant' failed to build: The command '/bin/sh -c yarn build' returned a non-zero code: 1

如何解决这个问题?

【问题讨论】:

  • 您是否将yarn.lock 添加到您的.dockerignore 文件中?如果没有,我会这样做并再试一次。
  • @KenanGüler 是的,我尝试使用 yarn.lock 并没有它。对这个问题没有影响。
  • 嗯,很奇怪。升级 docker 镜像中使用的 yarn 版本怎么样。您似乎使用的是旧版本(v1.13.0 -> 1.22.1)。
  • @KenanGüler,我试过了。相同。我认为问题出在 docker 版本上,我将使用另一个操作系统进行测试。

标签: node.js docker docker-compose dockerfile


【解决方案1】:

你可以在不创建用户的情况下尝试吗

FROM node:10.15.2-alpine

WORKDIR /app
COPY package.json .

RUN whoami

RUN yarn install
RUN ls -la 
RUN pwd


COPY . .
RUN yarn build

CMD ["node", "./dist/index.js"]

【讨论】:

  • 如果你想使用最新的节点图像
  • 我试过了。相同。我认为问题出在 docker 版本上,我将使用另一个操作系统进行测试。
【解决方案2】:

问题出在我用来构建 docker 容器的 docker 版本和操作系统上。

我使用 docker 版本 18.x 运行 docker-compose,操作系统是 Deepin 15.11 我切换到安装了 docker 版本 19.03.6 的 EleebtaryOS 版本 5.1.2 Hera,现在一切正常。

我不知道为什么在 Deepin OS 中会出现这种情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2022-12-19
    • 2021-06-16
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多