我正在尝试为使用yarn来安装依赖项的 Node 应用程序构建docker镜像。我的Dockerfile看起来像这样:

 FROM node:7
WORKDIR /app
COPY package.json /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000

当我在本地计算机上运行yarn install时,每件事都运行良好,但是当我执行docker build时,此错误永远存在。
**docker build -t  rs  .**
Sending build context to Docker daemon  219.1MB
Step 1/7 : FROM node:7
 ---> d9aed20b68a4
Step 2/7 : WORKDIR /reason
 ---> Using cache
 ---> fe51a1860989
Step 3/7 : COPY package.json /reason
 ---> Using cache
 ---> b0e136ee6eeb
Step 4/7 : RUN yarn install
 ---> Running in e273f8cf1f3e
yarn install v0.24.4
info No lockfile found.
[1/4] Resolving packages...
Couldn't find any versions for "glamor" that matches "next"
? Please choose a version of "glamor" from this list: (Use arrow keys)
❯ 2.20.40
  2.20.39
  2.20.38
  2.20.37
  2.20.36
  2.20.35
  2.20.34
(Move up and down to reveal more choices)warning glamor@3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-inline@1.0.5: use glamor/inline instead
warning gatsby-plugin-glamor > glamor-react > glamor@3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-server > glamor@3.0.0-3: abandoned, please use v2 instead
warning gatsby > babel-preset-es2015@6.24.1: 🙌  Thanks for using Babel: we recommend using babel-preset-env now:

请阅读babeljs.io/env进行更新!

控制台永远保持在此阶段。请问我该如何解决。

最佳答案

在构建镜像之前,您应该首先运行yarn install生成 yarn 锁文件( yarn.lock )。然后确保将其与package.json一起复制。您的dockerfile应该如下所示:

FROM node:7
WORKDIR /app
COPY package.json /app
COPY yarn.lock /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000

这样,在构建镜像时所有依赖项都应成功安装

10-07 19:26
查看更多