Docker在构建时丢失了纱线依赖 [英] Docker compose missing yarn dependencies on build

查看:71
本文介绍了Docker在构建时丢失了纱线依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dockerfile中运行yarn安装时无法获取node_modules文件夹

  test-sof 
├── docker-compose.yml
├──Dockerfile
├──package.json
└──yarn.lock

docker-compose.yml

  version:'3'
服务:
网站:
构建:。
卷:
-。:/ myapp

package.json

  {
name: site,
private:true,
dependencies :{
@ rails / webpacker: ^ 3.2.1,
babel-preset-react: ^ 6.24.1,
prop-types: ^ 15.6.0,
react: ^ 16.2.0,
react-dom: ^ 16.2.0,
reactjs: ^ 1.0 .0,
下划线: ^ 1.8.3
},
devDependencies:{
webpack-dev-server: ^ 2.11.1
}
}

Dockferfile

 从红宝石:2.5 

运行curl -sL https://deb.nodesource.com/setup_8.x | bash-&& \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt键添加-&& \
echo deb https://dl.yarnpkg.com/debian/ stable main | tee /etc/apt/sources.list.d/yarn.list&& \
易于获取更新&& \
apt-get install -qq -y build-essential libpq-dev nodejs yarn

RUN mkdir / myapp
WORKDIR / myapp

添加./package.json / myapp /

运行纱线安装

输出在 docker-compose build 时运行RUN yarn安装:


  Step 6/6:RUN毛线安装
--->在3a0e7095ec81中运行
yarn install v1.3.2
info找不到锁文件。
[1/4]正在解析软件包...
[2/4]正在获取软件包...
info fsevents@1.1.3:平台 linux与此模块不兼容。
info fsevents@1.1.3是可选的依赖项,并且兼容性检查失败。从安装中排除它。
[3/4]链接依赖项...
警告 @ rails / webpacker> postcss-cssnext@3.1.0具有未满足的对等依赖项 caniuse-lite@^1.0.30000697。
警告> webpack-dev-server@2.11.1具有未满足的对等依赖项 webpack@^2.2.0 || ^ 3.0.0。
警告 webpack-dev-server> webpack-dev-middleware@1.12.2具有未满足的对等依赖项 webpack@^1.0.0 || ^ 2.0.0 || ^ 3.0.0。
[4/4]构建新软件包...
成功保存了锁文件。
在21.11秒内完成。
移除中间容器3a0e7095ec81
---> 5720579a0f2a
成功构建5720579a0f2a
已成功标记testsof_web:latest


运行命令: docker-compose运行web bash 以进入容器

  root @ 11af1818e494:/ myapp#ls 
Dockerfile docker-compose.yml package.json

否存在node_modules文件夹,但稍后在容器内运行时:纱线安装输出:


  root @ 11af1818e494:/ myapp#纱线安装
纱线安装v1.3.2
信息未找到锁定文件。
[1/4]正在解析软件包...
[2/4]正在获取软件包...
info fsevents@1.1.3:平台 linux与此模块不兼容。
info fsevents@1.1.3是可选的依赖项,并且兼容性检查失败。从安装中排除它。
[3/4]链接依赖项...
警告 @ rails / webpacker> postcss-cssnext@3.1.0具有未满足的对等依赖项 caniuse-lite@^1.0.30000697。
警告> webpack-dev-server@2.11.1具有未满足的对等依赖项 webpack@^2.2.0 || ^ 3.0.0。
警告 webpack-dev-server> webpack-dev-middleware@1.12.2具有未满足的对等依赖项 webpack@^1.0.0 || ^ 2.0.0 || ^ 3.0.0。
[4/4]构建新软件包...
成功保存了锁文件。
在13.03秒内完成。


然后在上市时:

  root @ 11af1818e494:/ myapp#ls 
Dockerfile docker-compose.yml node_modules package.json yarn.lock

文件夹node_modules存在。为什么?

解决方案

Dockerfile 的这一部分将安装纱线包:

  RUN mkdir / myapp 
WORKDIR / myapp
添加./package.json / myapp /
运行纱线安装

文件夹 / myapp ,将 package.json 复制到其中并安装yarn软件包。构建成功,当然, node_modules 文件夹位于构建的映像中。



但是在此之后,您开始构建映像

 数量:
-。:/ myapp

表示将 docker-compose.yaml 挂载的文件夹的内容安装到 / myapp 容器中的文件夹,因此它涵盖了容器的 / myapp 文件夹的内容。



您不需要将当前文件夹挂载到容器的文件夹即可实现所需的功能。只需将其从您的 docker-compose.yaml 中删除​​:

 版本: '3'
服务:
网址:
构建:。

现在您可以:

  $ docker-compose build 
$ docker-compose run web bash
root @ 558d5b0c2ccb:/ myapp#ls -la
总计268
drwxr-xr -x 3 root root 4096年2月23日22:25。
drwxr-xr-x 65根root 4096 Feb 23 22:36 ..
drwxr-xr-x 818根root 36864 Feb 23 22:25 node_modules
-rw-rw-r- -1个根目录333 Feb 23 22:07 package.json
-rw-r--r-- 1个根目录219075 Feb 23 22:25 yarn.lock

编辑:


但是我想要的是在构建映像时,在旋转容器时获得这些依赖关系而不是
。否则,我有另一个容器
,它会装载源代码并在
运行 docker-compose up时需要此node_modules文件夹,并且我想避免某些
丑陋的睡眠,直到node_modules完成。所以我需要在我的根主机上出现这个
文件夹,然后再以某种方式打开容器


如果要实现上述目标,您可以使用以下解决方法:



1。。您对 Dockerfile 稍作修改:

 从红宝石:2.5 

RUN curl -sL https://deb.nodesource.com /setup_8.x | bash-&& \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt键添加-&& \
echo deb https://dl.yarnpkg.com/debian/ stable main | tee /etc/apt/sources.list.d/yarn.list&& \
易于获取更新&& \
apt-get install -qq -y build-essential libpq-dev nodejs yarn

RUN mkdir / build&& mkdir / myapp
WORKDIR / build

添加./package.json / build /

RUN yarn install

WORKDIR / myapp

CMD cp -a / build / node_modules / / myapp /

纱线包将在图像中的 / build 文件夹中构建,并在容器启动后复制到 / myapp 文件夹中。 / p>

2。。您使用原始的 docker-compose.yaml 文件:

 版本: 3 
服务:
网站:
构建:。
量:
-。:/ myapp

开始时 web 容器:

  docker-compose up web 

文件夹 node_modules 被复制到已装载的文件夹,即主机上的文件夹。



3。。现在您可以启动任何容器,它将包含<$ / myapp 中的c $ c> node_modules 文件夹:

  docker-compose run web bash 

因此,您将能够实现目标

  $ docker-compose build&& docker-compose up web 
$ docker-compose run web bash
root @ 4b38e60adfa3:/ myapp#ls -la
总计64
drwxrwxr-x 3 1000 1000 4096 2月24 10 :59。
drwxr-xr-x 66根root 4096 Feb 24 11:13 ..
-rw-rw-r-- 1 1000 1000 497 Feb 24 10:55 Dockerfile
-rw-rw -r-- 1 1000 1000 73 2月24日09:02 docker-compose.yaml
drwxr-xr-x 818根root 40960 2月24日10:57 node_modules
-rw-rw-r-- 1根根333 Feb 23 22:07 package.json


Can't get node_modules folder when running yarn install in the Dockerfile

test-sof
├── docker-compose.yml
├── Dockerfile
├── package.json
└── yarn.lock

docker-compose.yml

version: '3'
services:
  web:
    build: .
    volumes:
      - .:/myapp

package.json

{
  "name": "site",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "^3.2.1",
    "babel-preset-react": "^6.24.1",
    "prop-types": "^15.6.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "reactjs": "^1.0.0",
    "underscore": "^1.8.3"
  },
  "devDependencies": {
    "webpack-dev-server": "^2.11.1"
  }
}

Dockferfile

FROM ruby:2.5

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && \
    apt-get install -qq -y build-essential libpq-dev nodejs yarn

RUN mkdir /myapp
WORKDIR /myapp

ADD ./package.json /myapp/

RUN yarn install

output of the step RUN yarn install when docker-compose build:

Step 6/6 : RUN yarn install
---> Running in 3a0e7095ec81 
yarn install v1.3.2 
info No lockfile found. 
[1/4] Resolving packages... 
[2/4] Fetching packages... 
info fsevents@1.1.3: The platform "linux" is incompatible with this module. 
info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. 
[3/4] Linking dependencies... 
warning "@rails/webpacker > postcss-cssnext@3.1.0" has unmet peer dependency "caniuse-lite@^1.0.30000697". 
warning " > webpack-dev-server@2.11.1" has unmet peer dependency "webpack@^2.2.0 || ^3.0.0". 
warning "webpack-dev-server > webpack-dev-middleware@1.12.2" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0". 
[4/4] Building fresh packages... 
success Saved lockfile. 
Done in 21.11s. 
Removing intermediate container 3a0e7095ec81  
---> 5720579a0f2a 
Successfully built 5720579a0f2a 
Successfully tagged testsof_web:latest

Running command: docker-compose run web bash to get in the container

root@11af1818e494:/myapp# ls    
Dockerfile  docker-compose.yml  package.json

no node_modules folder present, but later when running inside the container: yarn install output:

root@11af1818e494:/myapp# yarn install
yarn install v1.3.2
info No lockfile found. 
[1/4] Resolving packages... 
[2/4] Fetching packages... 
info fsevents@1.1.3: The platform "linux" is incompatible with this module. 
info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. 
[3/4] Linking dependencies... 
warning "@rails/webpacker > postcss-cssnext@3.1.0" has unmet peer dependency "caniuse-lite@^1.0.30000697". 
warning " > webpack-dev-server@2.11.1" has unmet peer dependency "webpack@^2.2.0 || ^3.0.0". 
warning "webpack-dev-server > webpack-dev-middleware@1.12.2" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0". 
[4/4] Building fresh packages... 
success Saved lockfile. 
Done in 13.03s.

then when listing:

root@11af1818e494:/myapp# ls    
Dockerfile  docker-compose.yml  node_modules  package.json  yarn.lock

folder node_modules IT IS present. Why?

解决方案

This part of Dockerfile installs yarn packages:

RUN mkdir /myapp
WORKDIR /myapp
ADD ./package.json /myapp/
RUN yarn install

Folder /myapp is created, package.json is copied to it and yarn packages are installed. Build is successful and, of course, node_modules folder is inside built image.

But after that you start built image with:

volumes:
  - .:/myapp

which means that content of folder where docker-compose.yaml is is mounted to /myapp folder inside container, so it covers content of container's /myapp folder.

You don't need to mount current folder to container's folder to achieve what you want. Just delete it from your docker-compose.yaml:

version: '3'
services:
  web:
    build: .

Now you can:

$ docker-compose build
$ docker-compose run web bash
root@558d5b0c2ccb:/myapp# ls -la
total 268
drwxr-xr-x   3 root root   4096 Feb 23 22:25 .
drwxr-xr-x  65 root root   4096 Feb 23 22:36 ..
drwxr-xr-x 818 root root  36864 Feb 23 22:25 node_modules
-rw-rw-r--   1 root root    333 Feb 23 22:07 package.json
-rw-r--r--   1 root root 219075 Feb 23 22:25 yarn.lock

EDIT:

But what I want is when building the image, get these dependencies not when spinning up the containers. Otherwise I have another container which mounts de source code and needs this node_modules folder when running the "docker-compose up" and I'd like to avoid some kind of ugly sleep until the node_modules is finished. So I need present this folder on my root host before up the containers somehow

If you want to achieve the above goal, you can use the following workaround:

1. You modify Dockerfile a little:

FROM ruby:2.5

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && \
    apt-get install -qq -y build-essential libpq-dev nodejs yarn

RUN mkdir /build && mkdir /myapp
WORKDIR /build

ADD ./package.json /build/

RUN yarn install

WORKDIR /myapp

CMD cp -a /build/node_modules/ /myapp/

That's means that yarn packages will be built in /build folder inside image and copied to /myapp folder once container is started.

2. You use the original docker-compose.yaml file:

version: '3'
services:
  web:
    build: .
    volumes:
      - .:/myapp

when you start web container:

docker-compose up web

folder node_modules is copied to mounted folder i.e. to . folder on your host machine.

3. Now you can start any container and it will be contain node_modules folder inside /myapp:

docker-compose run web bash

So, you will be able to achieve your goal the following way:

$ docker-compose build && docker-compose up web
$ docker-compose run web bash
root@4b38e60adfa3:/myapp# ls -la
total 64
drwxrwxr-x   3 1000 1000  4096 Feb 24 10:59 .
drwxr-xr-x  66 root root  4096 Feb 24 11:13 ..
-rw-rw-r--   1 1000 1000   497 Feb 24 10:55 Dockerfile
-rw-rw-r--   1 1000 1000    73 Feb 24 09:02 docker-compose.yaml
drwxr-xr-x 818 root root 40960 Feb 24 10:57 node_modules
-rw-rw-r--   1 root root   333 Feb 23 22:07 package.json

这篇关于Docker在构建时丢失了纱线依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆