29

Running yarn docker-build works fine but when yarn docker-up or yarn docker-dev an error pops up when RUN yarn is called. Nginx starts up fine but yarn fails into mkdir in the Projects directory.

package.json

...
    "docker-build": "docker-compose build",
    "docker-dev": "cross-env NGINX_HOST=localhost NGINX_PORT=3000 PORT=3000 docker-compose -f docker-compose.yml -f docker-compose.dev.yml up --no-deps",
    "docker-up": "cross-env NGINX_HOST=localhost NGINX_PORT=80 PORT=8080 docker-compose -f docker-compose.yml -f docker-compose.prod.yml up --no-deps -d",
    "docker-down": "docker-compose down"
...

Dockerfile

FROM mhart/alpine-node:8

# Install required dependencies (Alpine Linux packages)
RUN apk update && \
  apk add --no-cache \
    sudo \
    g++ \
    gcc \
    git \
    libev-dev \
    libevent-dev \
    libuv-dev \
    make \
    openssl-dev \
    perl \
    python

# Add user and make it sudoer
ARG uid=1000
ARG user=username
RUN set -x ; \
  addgroup -g $uid -S $user ; \
  adduser -u $uid -D -S -G $user $user \
  && exit 0 ; exit 1
RUN echo $user' ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Install (global) Yarn packages/dependencies
RUN yarn global add node-gyp
RUN git clone --recursive https://github.com/sass/node-sass.git \
  && cd node-sass \
  && yarn \
  && node scripts/build -f

# Make project directory with permissions
RUN mkdir /project

# Switch to project directory
WORKDIR /project

# Copy required stuff
COPY . .

# Give owner rights to the current user
RUN chown -Rh $user:$user /project

# Install (local) Yarn packages and build
RUN yarn

USER $user

Error

app_1    | [2] Error: EACCES: permission denied, mkdir '/project/node_modules/.cache/@babel'
app_1    | [2]     at Object.fs.mkdirSync (fs.js:885:18)
app_1    | [2]     at sync (/project/node_modules/mkdirp/index.js:71:13)
app_1    | [2]     at sync (/project/node_modules/mkdirp/index.js:77:24)
app_1    | [2]     at save (/project/node_modules/@babel/register/lib/cache.js:50:20)
app_1    | [2]     at _combinedTickCallback (internal/process/next_tick.js:132:7)
app_1    | [2]     at process._tickCallback (internal/process/next_tick.js:181:9)
app_1    | [2]     at Function.Module.runMain (module.js:696:11)
app_1    | [2]     at startup (bootstrap_node.js:204:16)
app_1    | [2]     at bootstrap_node.js:625:3

My repo can be found here at https://github.com/cozy-nyc/cozy-nyc

S1MB10T3
  • 406
  • 1
  • 4
  • 6

6 Answers6

20

I had the same problem with npm. I fixed it using:

RUN npm config set unsafe-perm true

The other way is to specify it in your install command:

npm install -g --unsafe-perm thePackage

You can find the documentation for it here: https://docs.npmjs.com/misc/config#unsafe-perm

Shnd
  • 1,846
  • 19
  • 35
12

Try setting the user before running yarn in your Dockerfile.

# Give owner rights to the current user
RUN chown -Rh $user:$user /project

USER $user

# Install (local) Yarn packages and build
RUN yarn

Buchanora
  • 799
  • 1
  • 7
  • 5
9

You probably have a volume specified in your docker-compose file and mounted as /project/node_modules, your user does not have permission to access that directory on your local disk.

Find out which local directory the volume is mapped to using this SO answer and change the owner with:

sudo chown -R yourusername:yourusername /path/to/node_modules
Perennialista
  • 1,083
  • 2
  • 12
  • 22
4

Try adding volumes of ./node_modules to you docker-compose file under your application service.

Something like:

services:
  app:
    command: yarn start
    volumes:
      - .:/project
      - ./node_modules:/project/node_modules
Benson Lu
  • 114
  • 9
1

I would suggest to do like what was suggested here (https://github.com/nodejs/docker-node/issues/1262#issuecomment-677577653). So change the following lines from this

# Copy required stuff
COPY . .

# Give owner rights to the current user
RUN chown -Rh $user:$user /project

to this

# Copy required stuff
COPY --chown $user:$user . .

Or as alternative change your WORKDIR (https://github.com/nodejs/docker-node/issues/740#issuecomment-458545074):

WORKDIR /home/node/project
Ivan Sidaruk
  • 435
  • 3
  • 13
0

I had these kinds of errors and after a long time of trying I found out that Docker's (Mac OS X) Experimental Features were the reason.

I had checked both "Use the new Virtualization framework" and "Enable VirtioFS accelerated directory sharing"

After disabling them, npm works properly.

Jaska
  • 111
  • 1
  • 2
  • 6