Docker-compose build fail if node_modules are present in the local project

Viewed 3306

I'm trying to understand the basics of docker/docker-compose so I embarked in a didactic experiment of my own. Basically I want to test the whole cycle of: develop the code locally, write the docker and docker-compose files, push to a github repo, clone the repo in a test directory, do docker-compose up --build and observe the magic. To test all this I used an Angular app that I had already written. The docker file looks like this:

# 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

and the Docker-compose.yml is:

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"

So, everything works as expected unless I decide to start developing on the test folder. In that case, after I do npm install to download all the node_modules if I try again docker-compose up --build the build fails with the following error

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

As far as I understood, the whole building part of the Angular app is happening in the container (and in fact docker-compose works fine when i clone the repo) so why does it suddenly care of the node_modules in the local (OS) project?

2 Answers

I don't really know the reason of this behavior, but you should not copy node_module inside. See comments below

# 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

So you may be able to solve the problem by adding a .dockerignore file

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

Try to go inside the source image (just run it)

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

and try your commands step by step, to debug and have full control of it. Then add what is working to your Dockerfile.

To copy data to your running container use

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