gpt4 book ai didi

docker-compose - Docker 构建缺少的 yarn 依赖项

转载 作者:行者123 更新时间:2023-12-04 08:27:17 25 4
gpt4 key购买 nike

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

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

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

包.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"
}
}

码头文件
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

时运行 yarn install 步骤的输出docker-compose 构建 :

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


运行命令: docker-compose 运行 web bash 进入容器
root@11af1818e494:/myapp# ls    
Dockerfile docker-compose.yml package.json

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

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.


然后在列出时:
root@11af1818e494:/myapp# ls    
Dockerfile docker-compose.yml node_modules package.json yarn.lock

文件夹 node_modules 存在。为什么?

最佳答案

Dockerfile的这部分安装 yarn 包:

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

文件夹 /myapp已创建, package.json复制到它并安装 yarn 包。构建成功,当然, node_modules文件夹在内置图像中。

但在那之后你开始构建镜像:
volumes:
- .:/myapp

这意味着 docker-compose.yaml 所在文件夹的内容是安装到 /myapp容器内的文件夹,因此它涵盖了容器 /myapp 的内容文件夹。

您不需要将当前文件夹挂载到容器的文件夹来实现您想要的。只需从您的 docker-compose.yaml 中删除它:
version: '3'
services:
web:
build: .

现在你可以:
$ 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

编辑:

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



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

1. 您修改 Dockerfile一点:
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/

这意味着 yarn 包将构建在 /build 中。文件夹内的图像并复制到 /myapp容器启动后的文件夹。

2. 你用的是原版 docker-compose.yaml文件:
version: '3'
services:
web:
build: .
volumes:
- .:/myapp

当您开始时 web容器:
docker-compose up web

文件夹 node_modules复制到挂载文件夹,即 .主机上的文件夹。

3. 现在您可以启动任何容器,它将包含 node_modules文件夹内 /myapp :
docker-compose run web bash

因此,您将能够通过以下方式实现您的目标:
$ 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-compose - Docker 构建缺少的 yarn 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com