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!
3
Answers
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…
Version managers like
nvm
largely don’t work in Docker. In particular, most paths to running Docker containers and individualRUN
commands don’t read the.bashrc
file or any other shell dotfiles; so if the only thing that addsnvm
to$PATH
is code in the.bashrc
file, that is why you’re getting thenvm: command not found
error.You should be able to replace the first two thirds of the Dockerfile with just
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 defaultnode
image is based on Debian, so if you do need to install other packages (like a Python interpreter to runnode-gyp
) you’ll need to useapt-get
and notapk
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 runnpm install
; and then you need toCOPY
the application code in before you run the application itself. A corrected Dockerfile should look more likeI’ve avoided
RUN ["sh", "-c", "..."]
syntax: Docker automatically inserts thesh -c
wrapper ifRUN
orCMD
is a bare string. I’ve also skipped the problematicENTRYPOINT
line, which will limit your image to only running commands that happen to be interpreted as shell scripts.