在Dockerfile中运行yarn install时,无法获取node_modules文件夹
test-sof
├── docker-compose.yml
├── Dockerfile
├── package.json
└── yarn.lock
搬运工-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
在 docker-compose build 时安装步骤RUN纱线的输出:
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 install 输出:
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它存在。为什么呢?
答案 0 :(得分:4)
这部分Dockerfile
安装纱线包:
RUN mkdir /myapp
WORKDIR /myapp
ADD ./package.json /myapp/
RUN yarn install
创建文件夹/myapp
,将package.json
复制到其中并安装纱线包。构建成功,当然,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
修改强>
但我想要的是在构建图像时,不要获取这些依赖项 在旋转容器时。否则我有另一个容器 安装de源代码并在需要时使用此node_modules文件夹 运行" docker-compose up"而且我想避免某种情况 在node_modules完成之前一直睡不着觉。所以我需要提出这个 我的根主机上的文件夹在以某种方式向上容器之前
如果您想实现上述目标,可以使用以下解决方法:
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/
这意味着纱线包将在/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