Home > Enterprise >  On the Alpine Linux Docker Image, I get `nvm: command not found`
On the Alpine Linux Docker Image, I get `nvm: command not found`

Time:09-28

I have read the README of the nvm project, which lists the packages I need to install, although I had to modify the python2 package to python3, since the former package was deprecated following Alpine 3.13 release, as node can run on python3 package. Here is my Dockerfile, which is meant to be part of my .devcontainer directory to run on a Codespace:

# Pull the Alpine 3.15 Docker image
FROM alpine:3.16

# Enter the BASH shell
ENTRYPOINT [ "/bin/bash" ]

# Add packages without caching, but while upgrading Alpine
RUN apk add --no-cache -U\
    curl bash ca-certificates\
    openssl ncurses coreutils\
    python3 make gcc g  \
    libgcc linux-headers grep\
    util-linux binutils findutils

RUN touch ~/.bashrc

# Install NVM and source it
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
RUN echo '\
export NVM_DIR="~/.nvm" \
[-s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" \
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion\
' >~/.bashrc

# Run the usual setup NVM commands
RUN [ "/bin/bash", "-c", "nvm install -s --lts --latest-npm" ]
RUN [ "/bin/bash", "-c", "nvm alias default lts/*" ]

# Install NPM packages and run the dev server
RUN [ "/bin/bash", "-c", "npm i" ]
CMD [ "/bin/bash", "-c", "npm run dev" ]

Here is the line in my creation.log file which denotes the error:

2022-09-27 14:44:57.546Z: #9 [6/8] RUN [ "/bin/bash", "-c", "nvm install -s --lts --latest-npm" ]
2022-09-27 14:44:58.134Z: #9 0.539 /bin/bash: line 1: nvm: command not found
2022-09-27 14:44:58.229Z: #9 ERROR: executor failed running [/bin/bash -c nvm install -s --lts --latest-npm]: exit code: 127

If anything else is needed, please let me know! Thanks for any help!

CodePudding user response:

I wouldn't be using alpine based image for python applications,

Standard PyPI wheels don’t work on Alpine

Most Python packages these days include binary wheels on PyPI, significantly speeding install time. But if you’re using Alpine Linux you need to compile all the C code in every Python package that you use.

See here eventual issues you can have https://pythonspeed.com/articles/alpine-docker-python/

You can end up resolving this, then another one will show up and so on and on...

CodePudding user response:

Version managers like nvm largely don't work in Docker. In particular, most paths to running Docker containers and individual RUN commands don't read the .bashrc file or any other shell dotfiles; so if the only thing that adds nvm to $PATH is code in the .bashrc file, that is why you're getting the nvm: command not found error.

You should be able to replace the first two thirds of the Dockerfile with just

FROM node:lts

See the Docker Hub node image page for more details on what's in the image. This includes the Node interpreter itself, NPM, and Yarn. The default node image is based on Debian, so if you do need to install other packages (like a Python interpreter to run node-gyp) you'll need to use apt-get and not apk to install them.

Your Dockerfile will run into a couple of other notable problems. You need to COPY the package metadata into the image before you can run npm install; and then you need to COPY the application code in before you run the application itself. A corrected Dockerfile should look more like

FROM node:lts
# RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y ...
WORKDIR /app     # don't install into filesystem root
COPY package.json package-lock.json ./
RUN npm i        # or npm ci
COPY ./ ./       # make sure node_modules is in .dockerignore
CMD npm run dev  # or something that doesn't run a dev server

I've avoided RUN ["sh", "-c", "..."] syntax: Docker automatically inserts the sh -c wrapper if RUN or CMD is a bare string. I've also skipped the problematic ENTRYPOINT line, which will limit your image to only running commands that happen to be interpreted as shell scripts.

  • Related