docker-node icon indicating copy to clipboard operation
docker-node copied to clipboard

create-react-app doesn't work in official node Docker container - permission denied

Open kmezynski opened this issue 2 years ago • 10 comments

Version

node:15, node:16, node:17, node:lts

Platform

Linux 5.13.0-40-generic #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

Docker

What steps will reproduce the bug?

docker run --rm --name node -it -v $(pwd):/app -w /app node:lts npx create-react-app react-app

How often does it reproduce? Is there a required condition?

Entrypoint always fails with official node images - node:15, node:16, node:17, node:lts

What is the expected behavior?

Creating a new React app in /app/react-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...


> [email protected] postinstall /app/react-app/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"


> [email protected] postinstall /app/react-app/node_modules/core-js-pure
> node -e "try{require('./postinstall')}catch(e){}"

+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
added 1388 packages from 618 contributors in 51.042s

181 packages are looking for funding
  run `npm fund` for details


Initialized a git repository.

Installing template dependencies using npm...
npm WARN deprecated [email protected]: See https://github.com/lydell/source-map-resolve#deprecated
npm WARN @apideck/[email protected] requires a peer of ajv@>=8 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of typescript@>= 2.7 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ [email protected]
+ @testing-library/[email protected]
+ @testing-library/[email protected]
+ @testing-library/[email protected]
added 39 packages from 109 contributors in 6.575s

181 packages are looking for funding
  run `npm fund` for details

Removing template package using npm...

npm WARN @apideck/[email protected] requires a peer of ajv@>=8 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of typescript@>= 2.7 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

removed 1 package and audited 1427 packages in 3.974s

181 packages are looking for funding
  run `npm fund` for details

found 1 moderate severity vulnerability
  run `npm audit fix` to fix them, or `npm audit` for details
Git commit not created Error: Command failed: git commit -m "Initialize project using Create React App"
    at checkExecSyncError (child_process.js:790:11)
    at execSync (child_process.js:863:15)
    at tryGitCommit (/app/react-app/node_modules/react-scripts/scripts/init.js:62:5)
    at module.exports (/app/react-app/node_modules/react-scripts/scripts/init.js:350:25)
    at [eval]:3:14
    at Script.runInThisContext (vm.js:134:12)
    at Object.runInThisContext (vm.js:310:38)
    at internal/process/execution.js:81:19
    at [eval]-wrapper:6:22
    at evalScript (internal/process/execution.js:80:60) {
  status: 128,
  signal: null,
  output: [ null, null, null ],
  pid: 126,
  stdout: null,
  stderr: null
}
Removing .git directory...

Success! Created react-app at /app/react-app
Inside that directory, you can run several commands:

  npm start
    Starts the development server.

  npm run build
    Bundles the app into static files for production.

  npm test
    Starts the test runner.

  npm run eject
    Removes this tool and copies build dependencies, configuration files
    and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

  cd react-app
  npm start

Happy hacking!

What do you see instead?

Need to install the following packages:
  create-react-app
Ok to proceed? (y) y
npm WARN deprecated [email protected]: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap.
sh: 1: create-react-app: Permission denied
npm notice 
npm notice New minor version of npm available! 8.5.5 -> 8.9.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.9.0
npm notice Run npm install -g [email protected] to update!
npm notice

Additional information

To prevent downloading node.js on my host machine, I've wanted to create basic react-app inside container in attached volume, so that I can later take it out and play with it in some development/production container.

Since default user used in container is root, I find it weird to encounter error with file/app permissions.

kmezynski avatar May 11 '22 07:05 kmezynski

Works fine on my machine (mac), seems like an issue with your local machine?

SimenB avatar May 11 '22 08:05 SimenB

I'm working on Linux machine: Linux 5.13.0-40-generic #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux What exact issue/s comes to your mind?

kmezynski avatar May 11 '22 08:05 kmezynski

None in particular (beyond stuff like ownership of pwd) - seems more like a docker thing than this particular image.


FWIW, this is my output:

$ docker run --rm --name node -it -v $(pwd):/app -w /app node:lts npx create-react-app react-app
Need to install the following packages:
  create-react-app
Ok to proceed? (y)
npm WARN deprecated [email protected]: This version of tar is no longer supported, and will not receive security updates. Please upgrade asap.

Creating a new React app in /app/react-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...


added 1377 packages in 4m

181 packages are looking for funding
  run `npm fund` for details

Installing template dependencies using npm...
npm WARN deprecated [email protected]: See https://github.com/lydell/source-map-resolve#deprecated

added 39 packages in 17s

181 packages are looking for funding
  run `npm fund` for details
Removing template package using npm...


removed 1 package, and audited 1416 packages in 6s

181 packages are looking for funding
  run `npm fund` for details

6 moderate severity vulnerabilities

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.

Success! Created react-app at /app/react-app
Inside that directory, you can run several commands:

  npm start
    Starts the development server.

  npm run build
    Bundles the app into static files for production.

  npm test
    Starts the test runner.

  npm run eject
    Removes this tool and copies build dependencies, configuration files
    and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

  cd react-app
  npm start

Happy hacking!
npm notice
npm notice New minor version of npm available! 8.5.5 -> 8.9.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.9.0
npm notice Run npm install -g [email protected] to update!
npm notice

SimenB avatar May 11 '22 08:05 SimenB

I'm using official Docker image... what I should have mentioned is it's working with node:14 image.

kmezynski avatar May 11 '22 08:05 kmezynski

Can you try mounting somewhere else than /app? Somewhere like /home/node/app

LaurentGoderre avatar May 11 '22 14:05 LaurentGoderre

Also, can you check what your local user id is?

LaurentGoderre avatar May 11 '22 14:05 LaurentGoderre

https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#non-root-user

nschonni avatar May 11 '22 16:05 nschonni

https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md#non-root-user

@nschonni wow! Running container as unprivileged user has fixed everything. Now app is installed for all images mentioned above. Even though it's reasonable to use non-root user inside container, I can't get how on earth root user gets such error as Permission denied.

kmezynski avatar May 12 '22 08:05 kmezynski

Can you try mounting somewhere else than /app? Somewhere like /home/node/app

Sure thing @LaurentGoderre , what I've found out is that app is being installed properly whenever workdir path is different from volume path in the container, regardless user type used (root/non-root).

Also, can you check what your local user id is?

1000, the same as node user provided with node image.

kmezynski avatar May 12 '22 08:05 kmezynski

@kmezynski, I got the same issue:

$ whoami
root
$ stat -c "%U %G" .
node node
$ npx create-react-app my-app
[root](sh: 1: create-react-app: Permission denied)

Here is how I fixed it:

$ su node
$ whoami
node
$ npx create-react-app my-app
Success! Created my-app at /usr/local/src/my-app

You could also use docker run -itd -u node node or add user: node to your docker-compose.yml.

DiegoHeliosG avatar Sep 04 '22 04:09 DiegoHeliosG

move to a non-root user docker run -it --name react-test -v /etc/react:/app -u "node" -p 3000:3000 node bash

abhi-io avatar Oct 24 '22 12:10 abhi-io

IMO nothing more to be added on this topic, therefore I'm closing the issue. Thanks everyone for your assistance and valuable insights!

kmezynski avatar Apr 20 '23 23:04 kmezynski