Luke Batema
Luke Batema

Reputation: 45

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

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!

Upvotes: 1

Views: 3255

Answers (3)

Luke Batema
Luke Batema

Reputation: 45

# Pull the Alpine 3.15 Docker image
FROM alpine:3.15

# Set the entrypoint to the `ASH` shell
ENTRYPOINT ["/bin/ash"]

# Add the needed packages without caching, but upgrading Alpine
RUN apk add --no-cache -U npm nodejs sudo git

RUN git config --global user.email [email protected]
RUN git config --global user.name BatemaDevelopment

# Add the `node` group and user, then assign the user to the group
RUN addgroup -S node && adduser -S node -G node

# Make the directory and subdirectories `/home/node/acoustic-docs/node_modules`
# and change ownership recursivly to the `node` user and group
RUN mkdir -p /home/node/acoustic-docs/node_modules && chown -R node:node /home/node/acoustic-docs

# Make the required directories for the docs
RUN mkdir -p /home/node/acoustic-docs/blog && chown -R node:node /home/node/acoustic-docs/blog
RUN mkdir -p /home/node/acoustic-docs/Buttons && chown -R node:node /home/node/acoustic-docs/Buttons
RUN mkdir -p /home/node/acoustic-docs/docs && chown -R node:node /home/node/acoustic-docs/docs
RUN mkdir -p /home/node/acoustic-docs/src && chown -R node:node /home/node/acoustic-docs/src
RUN mkdir -p /home/node/acoustic-docs/static && chown -R node:node /home/node/acoustic-docs/static

# Set the working directory to `/home/node/acoustic-docs`
WORKDIR /home/node/acoustic-docs

COPY --chown=node:node . .

CMD [ "npm", "run dev" ]

Upvotes: 1

Reda E.
Reda E.

Reputation: 868

I wouldn't be using alpine based image for python/Node 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/

Update

As for Node Applications

See https://www.youtube.com/watch?v=mA8wtTUCdgc

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

Upvotes: 0

David Maze
David Maze

Reputation: 158778

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.

Upvotes: 2

Related Questions