使用Yarn依赖关系为Node应用程序构建Docker镜像

时间:2017-10-14 11:46:57

标签: node.js docker npm dockerfile yarnpkg

我正在尝试为使用yarn来安装依赖项的节点应用程序构建一个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构建时,我得到了这个永远阻塞的错误。

**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进行更新!

控制台永远处于这个阶段。我该怎么办呢。

3 个答案:

答案 0 :(得分:2)

在构建映像之前,首先应运行yarn install以生成yarn lockfile( 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

有了这个,所有依赖项应该在构建映像时成功安装

答案 1 :(得分:1)

您可以通过使用预定义的yarn docker映像来简化上述答案。我们在此假设此图像仅用于开发目的。对于生产模式,您应该只考虑最小的二进制文件,例如node。

FROM gmolaire/yarn:1.22.4_12.18.3-alpine3.12

WORKDIR /usr/local/app
ADD . .

RUN yarn install && \
    yarn build

EXPOSE 8080

CMD [ "yarn", "run", "develop" ]

答案 2 :(得分:-1)

<强> Dockerfile

FROM node:6.9.5-alpine
RUN mkdir -p /code
WORKDIR /code
ADD . /code
RUN npm install -g -s --no-progress yarn && \
    yarn && \
    yarn run build && \
    yarn cache clean
CMD [ "npm", "start" ]
EXPOSE 8080

<强>搬运工-compose.yml

version: '2'
services:
  sample-app:
    image: sample-node-yarn-app
    ports:
      - "8080:8080"

创建泊坞窗图片

  

docker build -t sample-node-app。

生成

  

docker-compose up -d