如果本地项目中存在node_modules,则Docker-compose构建失败

时间:2018-11-06 18:15:22

标签: angularjs node.js git docker

我试图了解docker / docker-compose的基础知识,所以我进行了自己的教学实验。基本上,我想测试以下整个周期:在本地开发代码,编写docker和docker-compose文件,推送到github存储库,在测试目录中克隆存储库,执行docker-compose up --build并观察魔术。 为了测试所有这些,我使用了已经编写的Angular应用程序。码头工人文件看起来像这样:

# base image
FROM node:10.13.0-jessie

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install -g @angular/cli

# add app
COPY . /usr/src/app

# expose port
EXPOSE 4200

# start app
CMD ng serve --host 0.0.0.0

和Docker-compose.yml是:

version: '3.7'

services:
  myapp-frontend:
    container_name: frontend
    build: ./
    working_dir: /usr/src/app
    command: ng serve --host 0.0.0.0
    ports:
      - "4200:4200"

因此,除非我决定开始在测试文件夹上进行开发,否则一切都会按预期进行。在这种情况下,如果我再次尝试npm installdocker-compose up --build下载所有的node_modules后,构建将失败,并显示以下错误

Traceback (most recent call last):
  File "site-packages\docker\utils\build.py", line 96, in create_archive
OSError: [Errno 22] Invalid argument: 'path\\myapp-frontend\\node_modules \\.bin\\acorn'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "docker-compose", line 6, in <module>
  File "compose\cli\main.py", line 71, in main
  File "compose\cli\main.py", line 127, in perform_command
  File "compose\cli\main.py", line 1052, in up
  File "compose\cli\main.py", line 1048, in up
  File "compose\project.py", line 471, in up
  File "compose\service.py", line 336, in ensure_image_exists
  File "compose\service.py", line 1067, in build
  File "site-packages\docker\api\build.py", line 154, in build
  File "site-packages\docker\utils\build.py", line 31, in tar
  File "site-packages\docker\utils\build.py", line 100, in create_archive
OSError: Can not read file in context: path\myapp-frontend\node_modules\.bin\acorn
[27228] Failed to execute script docker-compose

据我了解,Angular应用程序的整个构建部分都在容器中进行(实际上,当我克隆存储库时,docker-compose可以正常工作),所以为什么它突然关心本地的node_modules( OS)项目?

2 个答案:

答案 0 :(得分:1)

我真的不知道此行为的原因,但您不应在其中复制node_module。查看下面的评论

# install and cache app dependencies, **OK**
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install -g @angular/cli

# add app, **this also copies your local node_modules, overwriting previously installed**
COPY . /usr/src/app

因此,您可以通过添加一个.dockerignore文件来解决该问题

.dockerignore
./log
./tmp
.git
*Dockerfile*
*docker-compose*
node_modules

答案 1 :(得分:0)

尝试进入源图像(只需运行它)

docker run --rm -it node:10.13.0-jessie bash

,然后逐步尝试命令以进行调试并完全控制它。然后将正在运行的内容添加到您的Dockerfile中。

要将数据复制到正在运行的容器中,请使用

docker cp package.json mycontainer:/usr/src/app/package.json