Search code examples
node.jsdockeryarnpkgamazon-linux

Yarn command not found after successful docker build


Hi I am using amazonlinux:2 as my docker image and i am trying to install nodev14 from nvm I did not find any alternatives to do that I am trying to install yarn. Here's my Dockerfile

FROM amazonlinux:2

RUN yum update -y && \
    yum install tar curl gzip -y && \
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash && \
    . ~/.nvm/nvm.sh && \
    nvm install 14 -y &&  \
    npm install yarn -g && \
    yum install aws-cli -y && \
    yum clean all

When I build it is get build successful but when I run docker run -it container /bin/bash and after I get on /bin/bash it says command not found. Here's the full log for it

niteshrijal@Niteshs-MBP creepler % docker build -t node-14-tst .
[+] Building 50.9s (7/7) FINISHED                                                                           docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                        0.0s
 => => transferring dockerfile: 377B                                                                                        0.0s
 => [internal] load .dockerignore                                                                                           0.0s
 => => transferring context: 2B                                                                                             0.0s
 => [internal] load metadata for docker.io/library/amazonlinux:2                                                            2.5s
 => [auth] library/amazonlinux:pull token for registry-1.docker.io                                                          0.0s
 => CACHED [1/2] FROM docker.io/library/amazonlinux:2@sha256:7c24b50b9f0ad83b7219edb704962f361bcf3ec85fdfd302121159bf0d0a6  0.0s
 => [2/2] RUN yum update -y &&     yum install tar curl gzip -y &&     curl -o- https://raw.githubusercontent.com/nvm-sh/  47.5s
 => exporting to image                                                                                                      0.8s
 => => exporting layers                                                                                                     0.8s
 => => writing image sha256:bd8ae162aadb85eb1e08ac987f4cf136a7e604d7eac16231205d20ccab134e9a                                0.0s 
 => => naming to docker.io/library/node-14-tst                                                                              0.0s 

What's Next?
  View summary of image vulnerabilities and recommendations → docker scout quickview
niteshrijal@Niteshs-MBP creepler % docker run -it node-14-tst /bin/bash

bash-4.2# yarn
bash: yarn: command not found

Solution

  • Your NVM environment is not getting activated. When you install NVM, it attempts to add some stuff to your environment/PATH in the correct profile. If it fails to find a proper profile file, it will fail to do this. It also cannot know if you will be connecting to your container via bash, sh, zsh, or perhaps some other shell, and therefore it really cannot know which file to add your variables to. This is why the NVM readme specifies that it attempts to add variables to the correct profile.

    Try creating .bashrc file in the home directory of your container with the following contents, as mentioned in the NVM readme:

    export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    

    You could do this by creating the .bashrc file in the same folder as your Dockerfile and then just copying it:

    COPY .bashrc /root/.bashrc