Giter Club home page Giter Club logo

Comments (7)

mkenney avatar mkenney commented on May 28, 2024 1

Done and done, thanks for the suggestion! alpine takes a while to build so that image should be ready on DockerHub in an hour or so but the debian image is ready now.

from docker-npm.

mkenney avatar mkenney commented on May 28, 2024 1

@bennyp123 Thanks for reporting that, I'm taking a look to see if I can reproduce it.

from docker-npm.

bennypowers avatar bennypowers commented on May 28, 2024

i got errors like error An unexpected error occurred: "ENOENT: no such file or directory, lstat '/root/.cache/yarn/npm-test-fixture-1.1.1-3c513859eea8afbf18ee342771b62ad143533166'". when trying to yarn global add foo using alpine-7.0

from docker-npm.

mkenney avatar mkenney commented on May 28, 2024

Hi @bennyp123,

There are two issues here, the first is that there is an upstream issue with yarn-v0.17.2 which has been fixed in v0.17.4 and v0.17.8 appears to be available. It looks like some of the hub.docker.com images haven't been refreshed in a few days so I'm going to trigger new builds now but the process may take a few hours. I may look into setting up scheduled builds so that doesn't happen again.

The second issue is that unfortunately you can't do global installs using this project. When you run any of these scripts your project folder is "shared" with a virtual machine so anything that gets written to your project folder is saved to the hard drive but if you write files anywhere else they're saved in the virtual machine which is destroyed when the command ends, so each time you run it you run it without any of the changes.

Your best choices are to either install things locally and execute them that way (i.e. ./node_modules/foo/bin/foo) or to extend this project with your own Dockerfile and add whichever additional tools you need. I'm also happy to add more commonly used tools to this project.

Give it a few hours and run the yarn self-update command to get the new image, hopefully that will fix the yarn exception. And please let me know if you still have any trouble.

from docker-npm.

bennypowers avatar bennypowers commented on May 28, 2024

Hey thanks for checking this out.

The second issue is that unfortunately you can't do global installs using this project. When you run any of these scripts your project folder is "shared" with a virtual machine so anything that gets written to your project folder is saved to the hard drive but if you write files anywhere else they're saved in the virtual machine which is destroyed when the command ends, so each time you run it you run it without any of the changes.

my builds are passing when i run npm i -g foo so for my relatively simple use case what would be the difference using yarn? further to that, using node:latest; npm -i g yarn; yarn global add foo was fine. help me to understand

from docker-npm.

mkenney avatar mkenney commented on May 28, 2024

@bennyp123 Given your description I think there's some confusion. I don't think when you run the npm or yarn commands in your example that you are running the scripts in this project. That looks more like you're running locally installed versions.

In this project, if you run npm i -g yarn you get a permissions error because it's not running as root inside the container:

mkenney@9c0-docker$ ./bin/npm i -g yarn
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules/debug
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules/mime-db
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules/mime-types
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules/node-uuid
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules/request
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules/object-path
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/yarn/node_modules
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
- [email protected] node_modules/yarn/node_modules/node-uuid
/usr/local/lib
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  └─┬ [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    └── [email protected]

npm ERR! Linux 4.8.3-x86_64-linode76
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "i" "-g" "yarn"
npm ERR! node v7.0.0
npm ERR! npm  v3.10.9
npm ERR! path /usr/local/lib/node_modules/yarn/node_modules/debug
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules/yarn/node_modules/debug'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules/yarn/node_modules/debug'
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules/yarn/node_modules/debug' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /src/npm-debug.log

If you do happen to run that command in a folder owned by root (because the scripts run as a user that owns the current folder) it would succeed but it wouldn't actually install on the host system. Instead it would install into the Docker container and exit:

root@9c0-docker# ~mkenney/docker-tools/docker-npm/bin/npm i -g yarn
/usr/local/bin/yarn -> /usr/local/lib/node_modules/yarn/bin/yarn.js
/usr/local/bin/yarnpkg -> /usr/local/lib/node_modules/yarn/bin/yarn.js
- [email protected] node_modules/yarn/node_modules/node-uuid
/usr/local/lib
└─┬ [email protected]
  ├── [email protected]
  ├── [email protected]
  └─┬ [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    └── [email protected]

root@9c0-docker# yarn
-bash: yarn: command not found

That's because it runs in a virtual machine that never saves anything. This is confusing for a lot of people that I work with so I'll write up an example I can link to and share with them (and you :). Sorry if you already know most of this.

how it works...

First, you must have Docker installed to use this project, and you probably want to be able to run docker without sudo.

Once that's setup, you can run the mkenney/npm image in a Docker container and run the commands by hand which may be easier to understand:

# first, start the virtual machine and open a shell
mkenney@9c0-docker$ docker run -ti mkenney/npm:7.0-alpine sh

# now you will be logged in to the running `mkenney/npm:7.0-alpine` docker container
# as root:
/src# whoami
root
/src#

# `yarn` is already installed in the image so I'll use jshint as my example
/src# jshint
sh: jshint: not found
/src# npm i -g jshint
/usr/local/bin/jshint -> /usr/local/lib/node_modules/jshint/bin/jshint
/usr/local/lib
└─┬ [email protected]
  ├─┬ [email protected]
  │ └─┬ [email protected]
  │   ├── [email protected]
  │   ├─┬ [email protected]
  │   │ └── [email protected]
  │   ├── [email protected]
  │   ├── [email protected]
  │   └── [email protected]
  ├─┬ [email protected]
  │ └── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ ├── [email protected]
  │ ├── [email protected]
  │ ├─┬ [email protected]
  │ │ └─┬ [email protected]
  │ │   ├── [email protected]
  │ │   └── [email protected]
  │ ├── [email protected]
  │ └─┬ [email protected]
  │   ├── [email protected]
  │   ├── [email protected]
  │   └── [email protected]
  ├── [email protected]
  ├─┬ [email protected]
  │ └─┬ [email protected]
  │   ├── [email protected]
  │   └── [email protected]
  ├── [email protected]
  └── [email protected]

/src#jshint --version
jshint v2.9.4
/src#

# `jshint` is clearly installed, so now exit the virtual machine and restart it
/src# exit

mkenney@9c0-docker$

# now just go back in and try to run the jshint command
mkenney@9c0-docker$ docker run -ti mkenney/npm:7.0-alpine sh

/src# jshint
sh: jshint: not found
/src#

The reason for this is that Docker runs the image in memory and starts it from scratch each time so once it exits any changes are gone. You can share the filesystem with the running image (using the -v flag) when you start the image container and save any output that way:

mkenney@9c0-docker$ mkdir tmp && cd tmp && echo "Hello from the host" > test.txt
mkenney@9c0-docker$ docker run -ti -v $(pwd):/src:rw mkenney/npm:7.0-alpine sh
/src# cat test.txt
Hello from the host
/src# echo "Hello from Docker" > test.txt
/src# exit
mkenney@9c0-docker$ cat test.txt
Hello from Docker
mkenney@9c0-docker$

This project is the Dockerfile and some wrapper scripts for the image described above. The image includes a command wrapper that allows it to write files to the mounted directory with the same uid and gid as the mounted directory. That way npm doesn't install everything as root. When you run npm using the provided script it's actually executing:

mkenney@docker-9c0$ docker run --rm -ti -v $(pwd):/src:rw -v $HOME/.ssh:/home/dev/.ssh:ro mkenney/npm:7.0-alpine /run-as-user /usr/local/bin/npm

with comments:

mkenney@docker-9c0$ set -x \

    # Tell docker to run an image in a container. If it doesn't exist locally docker will try
    # to download it automatically
    && docker run \

        # Delete the container automatically when it exits so you don't have to do any
        # manual housekeeping
        --rm \

        # Run in an interactive terminal. If you want to pipe input into the docker command
        # just use -i
        -ti \

        # Mount the current folder into the container when it starts in the /src folder and
        # make it writeable. You can use ':ro' instead of ':rw' to make it read-only
        -v $(pwd):/src:rw \

        # If there is a .ssh/ folder in your home directory this will attempt to mount it into
        # the container as a read-only volume. This is useful for accessing remote services
        # using public key authentication, accessing private GitHub repos for example.
        -v $HOME/.ssh:/home/dev/.ssh:ro \

        # Specify which docker image to run in the container
        mkenney/npm:7.0-alpine \

        # Everything after the image name is passed in as a command. This is a script that
        # will run the next command as a user who's user Id and group Id matches the
        # current directory. That way, any files written to the mounted volume will be written
        # with permissions that match that volume instead of root.
        /run-as-user \

        # Execute npm with any arguments passed 
        /usr/local/bin/npm

Because it's running in a virtual machine, it doesn't have access to any folders except for the one you run the command from. Because of that, global installs don't work.

from docker-npm.

mkenney avatar mkenney commented on May 28, 2024

This appears to be fixed and I've restored the yarn tests, all branches are passing.

from docker-npm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.