skip to Main Content

I would like to install Node.js with yarn.pkg in my Dockerfile.
The Dockerfile is for a Laravel8/PHP8 project.

This is my actual try:

RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - 
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
RUN apt-get update -y 
    && apt-get install -y --no-install-recommends 
        nodejs 
        yarn 
        mysql-client

If I try it this way, I got this output on my shell:

 => CACHED [ 8/20] RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -                                                                                                                                                                                      0.0s
 => ERROR [ 9/20] RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -                                                                                                                                                                             0.4s
------
 > [ 9/20] RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -:
#12 0.331 Warning: apt-key output should not be parsed (stdout is not a terminal)
#12 0.384 gpg: Segmentation fault
#12 0.384 no valid OpenPGP data found.
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -]: exit code: 2

Is there a way to solve this problem?
If you need more information about it, please let me know.

Regards,
Manny

2

Answers


  1. Chosen as BEST ANSWER

    Top-Master, thank's a lot for your input. It has helped me a lot to find my solution.

    Here it is...
    I have used the Dockerfile you posted above and made some small changes:

    ENV NODE_VERSION=16.5
    ENV NVM_DIR=/root/.nvm
    RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
    RUN . "$NVM_DIR/nvm.sh" 
        && nvm install ${NODE_VERSION}
    RUN . "$NVM_DIR/nvm.sh" 
        && nvm use v${NODE_VERSION}
    RUN . "$NVM_DIR/nvm.sh" 
        && nvm alias default v${NODE_VERSION}
    ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
    
    RUN npm install -g yarn
    

    The problem was, the RUN npm install -g yarn command run into an error and the build fails. After some googling around I've found this webpage https://docs.npmjs.com/downloading-and-installing-node-js-and-npm.
    They recommend, that npm should be installed separately because of the directory structure and user rights. They are not the same if you install npm with Node.js together.

    So I've done this:

    ENV NODE_VERSION=16.5
    ENV NVM_DIR=/root/.nvm
    RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
    RUN . "$NVM_DIR/nvm.sh" 
        && nvm install ${NODE_VERSION}
    RUN . "$NVM_DIR/nvm.sh" 
        && nvm use v${NODE_VERSION}
    RUN . "$NVM_DIR/nvm.sh" 
        && nvm alias default v${NODE_VERSION}
    ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
    RUN apt-get update 
        && apt-get install -y --no-install-recommends 
            npm
    RUN npm install -g yarn
    

    For me (and I hope for many other people too) it works now :)

    Thank's again!
    Have a nice time!


  2. Hmm… NPM (Node package manager) is installed along with Node.js, and Yarn is just a module that is installed like npm install -g yarn

    Simply edit and use what another post mentions like:

    ENV NODE_VERSION=12.6.0
    RUN apt install -y curl
    ENV NVM_DIR=/root/.nvm
    RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
    RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
    RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
    RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
    ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
    RUN node --version
    RUN npm --version
    
    RUN npm install -g yarn
    

    Note that this is the best way I know to manage Node‘s version.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search