Home > Software engineering >  Node Docker can't run dev command
Node Docker can't run dev command

Time:09-18

I have a problem with my Node docker. In my Laravel containerized project, I also have a docker container for Node to run all the npm commands. The docker-compose.yml for my laravel project looks like this:

version: '3.8'

services:
    nginx:
        build:
            context: ./docker
            dockerfile: ./development/nginx.dockerfile
        ports:
            - "8000:80"
        volumes:
            - ./:/var/www/html
        depends_on:
            - mysql
            - php

    mysql:
        image: mariadb:10.9.2
        ports:
            - "3306:3306"
        environment:
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
            MYSQL_DATABASE: "laravel-database"
            MYSQL_USER: ${DB_USERNAME}
            MYSQL_PASSWORD: ${DB_PASSWORD}
        volumes:
            - ./mysql:/var/lib/mysql

    php:
        build:
            context: ./docker
            dockerfile: ./development/php.dockerfile
        volumes:
            - ./:/var/www/html

    npm:
        image: node:current-alpine
        volumes:
            - ./:/var/www/html
        entrypoint: [ "npm" ]
        working_dir: /var/www/html

In every dockerfile, I have set the group and user to laravel and added those user and group like this (in php container for example):

RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf

With the node docker container, I didn't do anything with user or groups, so I don't know if I am making a mistake with that, but now I can run the command

docker-compose run --rm npm install

to install the packages. This command gets executed successfully, but when I try to run:

docker-compose run --rm npm run dev

I get the following output:

> vite

npm verb stack Error: EACCES: permission denied, mkdir '/root/.npm/_cacache/tmp'
npm verb cwd /var/www/html
npm verb Linux 5.15.0-47-generic
npm verb node v18.9.0
npm verb npm  v8.19.1
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /root/.npm/_cacache/tmp
npm ERR! errno -13
npm verb Error: EACCES: permission denied, mkdir '/root/.npm/_cacache/tmp' 
npm ERR! 
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR! 
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 1000:1000 "/root/.npm"
npm verb exit -13
npm timing npm Completed in 207ms
npm verb unfinished npm timer command:run 1663366439750
npm verb code -13

I followed the docker video's from the Laracast series, but he didn't do anything with user/group permissions. I also tried running this command:

docker-compose run --rm npm cache clean --force 

But with no luck...

Why am I getting this error and how can I fix this?

UPDATE

After doing what @Mihai said, my output of command:

docker-compose run --rm --entrypoint ""  npm ls -lart /root/.npm

is the following:

drwxr-xr-x    1 node     node          4096 Sep  8 23:20 _logs
drwxr-xr-x    1 node     node          4096 Sep  8 23:20 .
drwx------    1 root     root          4096 Sep  8 23:20 ..

Unfortunately, when I run:

docker-compose run --rm npm run dev

I still get the following output:

 vite

npm verb stack Error: EACCES: permission denied, mkdir '/root/.npm/_cacache/tmp'
npm verb cwd /var/www/html
npm verb Linux 5.15.0-47-generic
npm verb node v18.9.0
npm verb npm  v8.19.1
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /root/.npm/_cacache/tmp
npm ERR! errno -13
npm verb Error: EACCES: permission denied, mkdir '/root/.npm/_cacache/tmp' 
npm ERR! 
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR! 
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 1000:1000 "/root/.npm"
npm verb exit -13
npm timing npm Completed in 216ms
npm verb unfinished npm timer command:run 1663406163420
npm verb code -13

npm ERR! Log files were not written due to an error writing to the directory: /root/.npm/_logs
npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
npm timing command:run Completed in 579ms
npm notice 
npm notice New patch version of npm available! 8.19.1 -> 8.19.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.19.2
npm notice Run npm install -g [email protected] to update!
npm notice 
npm verb exit 243
npm timing npm Completed in 616ms
npm verb code 243
ERROR: 243

Why is this not working???

CodePudding user response:

The error message tells you the problem and the solution:

npm verb Error: EACCES: permission denied, mkdir '/root/.npm/_cacache/tmp' 
npm ERR! 
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR! 
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 1000:1000 "/root/.npm"

You can see the problem:

docker run --rm --entrypoint "" node:current-alpine ls -lart /root/.npm

gives the output:

total 12
drwxr-xr-x    2 root     root          4096 Sep  8 23:20 _logs
drwxr-xr-x    3 root     root          4096 Sep  8 23:20 .
drwx------    1 root     root          4096 Sep  8 23:20 ..

To fix it in your setup you need to do the following:

create a new Dockerfile with the contents:

FROM node:current-alpine

RUN mkdir -p /root/.npm/_cacache/tmp
RUN chown -R 1000:1000 /root/.npm

ENTRYPOINT ["npm"]

edit docker-compose.yml for service npm like this:

services:
      npm:
        build:
          context: .
        image: nodefix:current-alpine
        volumes:
            - ./:/var/www/html
        working_dir: /var/www/html

The image name is not important here, only the build context needs to point to the correct location of the new Dockerfile.

Test that it works:

docker-compose build
docker-compose run --rm npm version

To see the difference:

docker-compose run --rm --entrypoint ""  npm ls -lart /root/.npm

gives the output:

total 24
drwxr-xr-x    1 node     node          4096 Sep  8 23:20 _logs
drwxr-xr-x    1 node     node          4096 Sep  8 23:20 .
drwx------    1 root     root          4096 Sep  8 23:20 ..

Notice the owner of _logs is now node:node.

  • Related