Search code examples
dockerdockerfilenvm

Installing NVM with Dockerfile: You have $NVM_DIR set to "~/.nvm", but that directory does not exist. Check your profile files and environment


I am building a new image with a Dockerfile and I need to install NVM. Unfortunately I am getting the error:

> docker build -t blingcontainer.azurecr.io/blingdemofy22h2:v3.1 .
[+] Building 1.5s (6/6) FINISHED                                                                   docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 440B                                                                               0.0s
 => [internal] load metadata for docker.io/library/debian:latest                                                   1.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [1/3] FROM docker.io/library/debian:latest@sha256:45f2e735295654f13e3be10da2a6892c708f71a71be845818f605898276  0.0s
 => CACHED [2/3] RUN apt-get update     && apt-get install -y curl     && apt-get -y autoclean                     0.0s
 => ERROR [3/3] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash                0.5s
------
 > [3/3] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash:
0.237   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
0.237                                  Dload  Upload   Total   Spent    Left  Speed
100 16555  100 16555    0     0  90914      0 --:--:-- --:--:-- --:--:-- 90961
0.422 You have $NVM_DIR set to "~/.nvm", but that directory does not exist. Check your profile files and environment.
------
Dockerfile:15
--------------------
  13 |     ENV ANGULAR_VERSION=14.2.13
  14 |
  15 | >>> RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
--------------------
ERROR: failed to solve: process "/bin/sh -c curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash" did not complete successfully: exit code: 1

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/p497n5djh15gza2r2fx7em7ob

Here is my dockerfile:

# update the repository sources list
# and install dependencies
RUN apt-get update \
    && apt-get install -y curl \
    && apt-get -y autoclean

# Install NVM, Node.js v16, and Angular
ENV NVM_DIR=~/.nvm
ENV NODE_VERSION=16.20.2
ENV ANGULAR_VERSION=14.2.13

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash

I find that if I comment out the line ENV NVM_DIR=~/.nvm then my build goes through fine. Why this and how can I fix it? I need this variable for some other lines I need to add later in my Dockerfile.


Solution

  • Installing NVM in an image doesn't make much sense when you can just use the appropriately tagged base image, eg node:16.

    You can even use build args to dynamically set the version

    ARG NODE_VERSION=16.20.2
    FROM node:${NODE_VERSION}-alpine # or whatever OS type you prefer
    
    # ...
    

    The primary purpose of NVM is to be able to switch versions in your development environment but since you're using containers, there won't be any switching.