Why Is My Build Failing?

Disclaimer

I know that what you are about to see is probably REALLY gross - so you might want to make sure you’re near a proper receptacle in case you can’t hold back…

Problem Description

I made my first own Dockerfile of something I actually want (not just one of the practice exercises). I ran each of the commands I put in the RUN directive on the official base image in an interactive terminal before writing my Dockerfile and it worked just fine on the official base image.

docker pull ubuntu:22.04 && docker run -it ubuntu:22.04
# then ran each command seen in my Dockerfile in the same order they appear there

Dockerfile

FROM ubuntu:22.04
RUN apt update\
    && apt install curl -y\
    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash\
    && source ~/.bashrc\
    && nvm install --lts\
    && npm install -g npm

Output

$ docker build -t my-node:ubuntu-22.04 .
[+] Building 27.1s (5/5) FINISHED                                                                                         
 => [internal] load .dockerignore                                                                                    0.0s
 => => transferring context: 2B                                                                                      0.0s
 => [internal] load build definition from Dockerfile                                                                 0.0s
 => => transferring dockerfile: 265B                                                                                 0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                                      0.0s
 => CACHED [1/2] FROM docker.io/library/ubuntu:22.04                                                                 0.0s
 => ERROR [2/2] RUN apt update    && apt install curl -y    && curl -o- https://raw.githubusercontent.com/nvm-sh/n  27.0s
------                                                                                                                    
 > [2/2] RUN apt update    && apt install curl -y    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash    && source ~/.bashrc    && nvm install --lts    && npm install -g npm:                                    
#0 0.242                                                                                                                  
#0 0.242 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.                                  
#0 0.242                                                                                                                  
#0 1.125 Get:1 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
#0 1.125 Get:2 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
#0 2.261 Get:3 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [869 kB]
#0 2.480 Get:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
#0 2.954 Get:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]
#0 3.296 Get:6 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [23.2 kB]
#0 3.301 Get:7 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [829 kB]
#0 3.645 Get:8 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [17.5 MB]
#0 4.003 Get:9 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [905 kB]
#0 12.86 Get:10 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [266 kB]
#0 12.86 Get:11 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 Packages [164 kB]
#0 12.87 Get:12 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB]
#0 13.49 Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1199 kB]
#0 13.52 Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [28.6 kB]
#0 13.53 Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [885 kB]
#0 14.22 Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1142 kB]
#0 15.15 Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [49.0 kB]
#0 15.87 Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
#0 15.92 Fetched 26.3 MB in 16s (1675 kB/s)
#0 15.92 Reading package lists...
#0 16.43 Building dependency tree...
#0 16.54 Reading state information...
#0 16.54 All packages are up to date.
#0 16.55 
#0 16.55 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
#0 16.55 
#0 16.57 Reading package lists...
#0 17.05 Building dependency tree...
#0 17.15 Reading state information...
#0 17.23 The following additional packages will be installed:
#0 17.23   ca-certificates libbrotli1 libcurl4 libldap-2.5-0 libldap-common
#0 17.23   libnghttp2-14 libpsl5 librtmp1 libsasl2-2 libsasl2-modules
#0 17.23   libsasl2-modules-db libssh-4 openssl publicsuffix
#0 17.23 Suggested packages:
#0 17.23   libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal
#0 17.23   libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql
#0 17.25 The following NEW packages will be installed:
#0 17.25   ca-certificates curl libbrotli1 libcurl4 libldap-2.5-0 libldap-common
#0 17.26   libnghttp2-14 libpsl5 librtmp1 libsasl2-2 libsasl2-modules
#0 17.26   libsasl2-modules-db libssh-4 openssl publicsuffix
#0 18.01 0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
#0 18.01 Need to get 2977 kB of archives.
#0 18.01 After this operation, 7100 kB of additional disk space will be used.
#0 18.01 Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 openssl amd64 3.0.2-0ubuntu1.8 [1184 kB]
#0 19.81 Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 ca-certificates all 20211016ubuntu0.22.04.1 [144 kB]
#0 20.22 Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libnghttp2-14 amd64 1.43.0-1build3 [76.3 kB]
#0 20.23 Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 libpsl5 amd64 0.21.0-1.2build2 [58.4 kB]
#0 20.23 Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 publicsuffix all 20211207.1025-1 [129 kB]
#0 20.28 Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 libbrotli1 amd64 1.0.9-2build6 [315 kB]
#0 20.28 Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsasl2-modules-db amd64 2.1.27+dfsg2-3ubuntu1.2 [20.5 kB]
#0 20.28 Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsasl2-2 amd64 2.1.27+dfsg2-3ubuntu1.2 [53.8 kB]
#0 20.28 Get:9 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libldap-2.5-0 amd64 2.5.14+dfsg-0ubuntu0.22.04.1 [183 kB]
#0 20.28 Get:10 http://archive.ubuntu.com/ubuntu jammy/main amd64 librtmp1 amd64 2.4+20151223.gitfa8646d.1-2build4 [58.2 kB]
#0 20.28 Get:11 http://archive.ubuntu.com/ubuntu jammy/main amd64 libssh-4 amd64 0.9.6-2build1 [184 kB]
#0 21.30 Get:12 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libcurl4 amd64 7.81.0-1ubuntu1.10 [290 kB]
#0 22.67 Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 curl amd64 7.81.0-1ubuntu1.10 [194 kB]
#0 22.75 Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libldap-common all 2.5.14+dfsg-0ubuntu0.22.04.1 [15.9 kB]
#0 22.76 Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsasl2-modules amd64 2.1.27+dfsg2-3ubuntu1.2 [68.8 kB]
#0 23.03 debconf: delaying package configuration, since apt-utils is not installed
#0 23.05 Fetched 2977 kB in 6s (522 kB/s)
#0 23.06 Selecting previously unselected package openssl.
(Reading database ... 4395 files and directories currently installed.)
#0 23.08 Preparing to unpack .../00-openssl_3.0.2-0ubuntu1.8_amd64.deb ...
#0 23.08 Unpacking openssl (3.0.2-0ubuntu1.8) ...
#0 23.11 Selecting previously unselected package ca-certificates.
#0 23.11 Preparing to unpack .../01-ca-certificates_20211016ubuntu0.22.04.1_all.deb ...
#0 23.11 Unpacking ca-certificates (20211016ubuntu0.22.04.1) ...
#0 23.14 Selecting previously unselected package libnghttp2-14:amd64.
#0 23.14 Preparing to unpack .../02-libnghttp2-14_1.43.0-1build3_amd64.deb ...
#0 23.14 Unpacking libnghttp2-14:amd64 (1.43.0-1build3) ...
#0 23.16 Selecting previously unselected package libpsl5:amd64.
#0 23.16 Preparing to unpack .../03-libpsl5_0.21.0-1.2build2_amd64.deb ...
#0 23.16 Unpacking libpsl5:amd64 (0.21.0-1.2build2) ...
#0 23.18 Selecting previously unselected package publicsuffix.
#0 23.18 Preparing to unpack .../04-publicsuffix_20211207.1025-1_all.deb ...
#0 23.18 Unpacking publicsuffix (20211207.1025-1) ...
#0 23.20 Selecting previously unselected package libbrotli1:amd64.
#0 23.20 Preparing to unpack .../05-libbrotli1_1.0.9-2build6_amd64.deb ...
#0 23.20 Unpacking libbrotli1:amd64 (1.0.9-2build6) ...
#0 23.22 Selecting previously unselected package libsasl2-modules-db:amd64.
#0 23.22 Preparing to unpack .../06-libsasl2-modules-db_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
#0 23.22 Unpacking libsasl2-modules-db:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 23.24 Selecting previously unselected package libsasl2-2:amd64.
#0 23.24 Preparing to unpack .../07-libsasl2-2_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
#0 23.24 Unpacking libsasl2-2:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 23.26 Selecting previously unselected package libldap-2.5-0:amd64.
#0 23.26 Preparing to unpack .../08-libldap-2.5-0_2.5.14+dfsg-0ubuntu0.22.04.1_amd64.deb ...
#0 23.26 Unpacking libldap-2.5-0:amd64 (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 23.28 Selecting previously unselected package librtmp1:amd64.
#0 23.28 Preparing to unpack .../09-librtmp1_2.4+20151223.gitfa8646d.1-2build4_amd64.deb ...
#0 23.28 Unpacking librtmp1:amd64 (2.4+20151223.gitfa8646d.1-2build4) ...
#0 23.30 Selecting previously unselected package libssh-4:amd64.
#0 23.30 Preparing to unpack .../10-libssh-4_0.9.6-2build1_amd64.deb ...
#0 23.30 Unpacking libssh-4:amd64 (0.9.6-2build1) ...
#0 23.32 Selecting previously unselected package libcurl4:amd64.
#0 23.32 Preparing to unpack .../11-libcurl4_7.81.0-1ubuntu1.10_amd64.deb ...
#0 23.32 Unpacking libcurl4:amd64 (7.81.0-1ubuntu1.10) ...
#0 23.34 Selecting previously unselected package curl.
#0 23.34 Preparing to unpack .../12-curl_7.81.0-1ubuntu1.10_amd64.deb ...
#0 23.34 Unpacking curl (7.81.0-1ubuntu1.10) ...
#0 23.36 Selecting previously unselected package libldap-common.
#0 23.36 Preparing to unpack .../13-libldap-common_2.5.14+dfsg-0ubuntu0.22.04.1_all.deb ...
#0 23.36 Unpacking libldap-common (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 23.38 Selecting previously unselected package libsasl2-modules:amd64.
#0 23.38 Preparing to unpack .../14-libsasl2-modules_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
#0 23.38 Unpacking libsasl2-modules:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 23.40 Setting up libpsl5:amd64 (0.21.0-1.2build2) ...
#0 23.40 Setting up libbrotli1:amd64 (1.0.9-2build6) ...
#0 23.41 Setting up libsasl2-modules:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 23.42 Setting up libnghttp2-14:amd64 (1.43.0-1build3) ...
#0 23.43 Setting up libldap-common (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 23.43 Setting up libsasl2-modules-db:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 23.44 Setting up librtmp1:amd64 (2.4+20151223.gitfa8646d.1-2build4) ...
#0 23.45 Setting up libsasl2-2:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 23.45 Setting up libssh-4:amd64 (0.9.6-2build1) ...
#0 23.46 Setting up openssl (3.0.2-0ubuntu1.8) ...
#0 23.47 Setting up publicsuffix (20211207.1025-1) ...
#0 23.47 Setting up libldap-2.5-0:amd64 (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 23.48 Setting up ca-certificates (20211016ubuntu0.22.04.1) ...
#0 23.53 debconf: unable to initialize frontend: Dialog
#0 23.53 debconf: (TERM is not set, so the dialog frontend is not usable.)
#0 23.53 debconf: falling back to frontend: Readline
#0 23.53 debconf: unable to initialize frontend: Readline
#0 23.53 debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.34.0 /usr/local/share/perl/5.34.0 /usr/lib/x86_64-linux-gnu/perl5/5.34 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.34 /usr/share/perl/5.34 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
#0 23.53 debconf: falling back to frontend: Teletype
#0 23.70 Updating certificates in /etc/ssl/certs...
#0 23.95 124 added, 0 removed; done.
#0 23.96 Setting up libcurl4:amd64 (7.81.0-1ubuntu1.10) ...
#0 23.97 Setting up curl (7.81.0-1ubuntu1.10) ...
#0 23.98 Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
#0 24.01 Processing triggers for ca-certificates (20211016ubuntu0.22.04.1) ...
#0 24.02 Updating certificates in /etc/ssl/certs...
#0 24.18 0 added, 0 removed; done.
#0 24.18 Running hooks in /etc/ca-certificates/update.d...
#0 24.19 done.
#0 24.21   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
#0 24.21                                  Dload  Upload   Total   Spent    Left  Speed
100 15916  100 15916    0     0  13420      0  0:00:01  0:00:01 --:--:-- 13431
#0 25.40 => Downloading nvm as script to '/root/.nvm'
#0 26.93 
#0 26.93 => Appending nvm source string to /root/.bashrc
#0 26.94 => Appending bash_completion source string to /root/.bashrc
#0 26.96 => Close and reopen your terminal to start using nvm or run the following to use it now:
#0 26.96 
#0 26.96 export NVM_DIR="$HOME/.nvm"
#0 26.96 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
#0 26.96 [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
#0 26.96 /bin/sh: 1: source: not found
------
Dockerfile:2
--------------------
   1 |     FROM ubuntu:22.04
   2 | >>> RUN apt update\
   3 | >>>     && apt install curl -y\
   4 | >>>     && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash\
   5 | >>>     && source ~/.bashrc\
   6 | >>>     && nvm install --lts\
   7 | >>>     && npm install -g npm
--------------------
ERROR: failed to solve: process "/bin/sh -c apt update    && apt install curl -y    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash    && source ~/.bashrc    && nvm install --lts    && npm install -g npm" did not complete successfully: exit code: 127

Observations

I notice the line ... /bin/sh: 1: source: not found in the output but when I ran the source command in the official base image (to test) it worked fine and I was able to execute the succeeding commands without any issue. I did not have to install the source command in the official image I use for my base iin Dockerfile - it was just there available by default.

Unless that line in the output is referring to something else.

Humble Request

Can anyone identify what I’m doing wrong and offer suggestions for improvement?

Note:

The Dockerfile formatting seems to have been mangled here in this post and I haven’t been able to get the editor to stop doing it.

System Details

Host Operating System: Ubuntu 20.04
Docker Version: 23.0.1, build a5ee5b1

source is a built-in command in bash. You are using sh, which does not provide source as built-in command. You could try if replacing source ~/.bashrc with . ~/bashrc does the trick. Though if install.sh adds commands to the bashrc that can’t be processed by sh then it won’t help-

Though, you can add a SHELL intruction before your RUN instructions to make the RUN instructions use the shell you definine, instead of /bin/sh -c:

SHELL ["/bin/bash", "-c"]

New Attempt

Tried running the build again with docker build -t my-node:ubuntu-22.04 . (same command as befroe) - after adding SHELL ["/bin/bash", "-c"] to the line above my RUNdirective

Using Altered Dockerfile

FROM ubuntu:22.04
SHELL ["/bin/bash", "-c"]
RUN apt update\
    && apt install curl -y\
    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash\
    && source ~/.bashrc\
    && nvm install --lts\
    && npm install -g npm

Result

Failed yielding the following output…

$ docker build -t my-node:ubuntu-22.04 .
[+] Building 16.6s (5/5) FINISHED                                                                                        
 => [internal] load .dockerignore                                                                                   0.0s
 => => transferring context: 2B                                                                                     0.0s
 => [internal] load build definition from Dockerfile                                                                0.0s
 => => transferring dockerfile: 291B                                                                                0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                                     0.0s
 => CACHED [1/2] FROM docker.io/library/ubuntu:22.04                                                                0.0s
 => ERROR [2/2] RUN apt update    && apt install curl -y    && curl -o- https://raw.githubusercontent.com/nvm-sh/  16.6s
------                                                                                                                   
 > [2/2] RUN apt update    && apt install curl -y    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash    && source ~/.bashrc    && nvm install --lts    && npm install -g npm:                                  
#0 0.308                                                                                                                 
#0 0.308 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.                                 
#0 0.308                                                                                                                 
#0 0.832 Get:1 http://archive.ubuntu.com/ubuntu jammy InRelease [270 kB]
#0 0.848 Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
#0 1.224 Get:3 http://security.ubuntu.com/ubuntu jammy-security/restricted amd64 Packages [836 kB]
#0 1.668 Get:4 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [869 kB]
#0 1.678 Get:5 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
#0 1.954 Get:6 http://security.ubuntu.com/ubuntu jammy-security/multiverse amd64 Packages [23.2 kB]
#0 1.960 Get:7 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [906 kB]
#0 2.229 Get:8 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [107 kB]
#0 2.572 Get:9 http://archive.ubuntu.com/ubuntu jammy/main amd64 Packages [1792 kB]
#0 3.639 Get:10 http://archive.ubuntu.com/ubuntu jammy/multiverse amd64 Packages [266 kB]
#0 3.743 Get:11 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages [17.5 MB]
#0 8.704 Get:12 http://archive.ubuntu.com/ubuntu jammy/restricted amd64 Packages [164 kB]
#0 8.741 Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [1142 kB]
#0 9.060 Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/multiverse amd64 Packages [28.6 kB]
#0 9.069 Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [1200 kB]
#0 9.434 Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/restricted amd64 Packages [885 kB]
#0 9.917 Get:17 http://archive.ubuntu.com/ubuntu jammy-backports/universe amd64 Packages [22.4 kB]
#0 10.10 Get:18 http://archive.ubuntu.com/ubuntu jammy-backports/main amd64 Packages [49.0 kB]
#0 10.21 Fetched 26.3 MB in 10s (2659 kB/s)
#0 10.21 Reading package lists...
#0 10.72 Building dependency tree...
#0 10.82 Reading state information...
#0 10.83 All packages are up to date.
#0 10.84 
#0 10.84 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
#0 10.84 
#0 10.86 Reading package lists...
#0 11.34 Building dependency tree...
#0 11.46 Reading state information...
#0 11.56 The following additional packages will be installed:
#0 11.56   ca-certificates libbrotli1 libcurl4 libldap-2.5-0 libldap-common
#0 11.56   libnghttp2-14 libpsl5 librtmp1 libsasl2-2 libsasl2-modules
#0 11.56   libsasl2-modules-db libssh-4 openssl publicsuffix
#0 11.56 Suggested packages:
#0 11.56   libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal
#0 11.56   libsasl2-modules-ldap libsasl2-modules-otp libsasl2-modules-sql
#0 11.58 The following NEW packages will be installed:
#0 11.58   ca-certificates curl libbrotli1 libcurl4 libldap-2.5-0 libldap-common
#0 11.58   libnghttp2-14 libpsl5 librtmp1 libsasl2-2 libsasl2-modules
#0 11.58   libsasl2-modules-db libssh-4 openssl publicsuffix
#0 11.83 0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
#0 11.83 Need to get 2977 kB of archives.
#0 11.83 After this operation, 7100 kB of additional disk space will be used.
#0 11.83 Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 openssl amd64 3.0.2-0ubuntu1.8 [1184 kB]
#0 12.56 Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 ca-certificates all 20211016ubuntu0.22.04.1 [144 kB]
#0 12.60 Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libnghttp2-14 amd64 1.43.0-1build3 [76.3 kB]
#0 12.63 Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 libpsl5 amd64 0.21.0-1.2build2 [58.4 kB]
#0 12.65 Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 publicsuffix all 20211207.1025-1 [129 kB]
#0 12.69 Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 libbrotli1 amd64 1.0.9-2build6 [315 kB]
#0 12.78 Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsasl2-modules-db amd64 2.1.27+dfsg2-3ubuntu1.2 [20.5 kB]
#0 12.79 Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsasl2-2 amd64 2.1.27+dfsg2-3ubuntu1.2 [53.8 kB]
#0 12.80 Get:9 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libldap-2.5-0 amd64 2.5.14+dfsg-0ubuntu0.22.04.1 [183 kB]
#0 12.86 Get:10 http://archive.ubuntu.com/ubuntu jammy/main amd64 librtmp1 amd64 2.4+20151223.gitfa8646d.1-2build4 [58.2 kB]
#0 12.88 Get:11 http://archive.ubuntu.com/ubuntu jammy/main amd64 libssh-4 amd64 0.9.6-2build1 [184 kB]
#0 13.40 Get:12 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libcurl4 amd64 7.81.0-1ubuntu1.10 [290 kB]
#0 14.35 Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 curl amd64 7.81.0-1ubuntu1.10 [194 kB]
#0 14.43 Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libldap-common all 2.5.14+dfsg-0ubuntu0.22.04.1 [15.9 kB]
#0 14.44 Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libsasl2-modules amd64 2.1.27+dfsg2-3ubuntu1.2 [68.8 kB]
#0 14.53 debconf: delaying package configuration, since apt-utils is not installed
#0 14.55 Fetched 2977 kB in 3s (1038 kB/s)
#0 14.57 Selecting previously unselected package openssl.
(Reading database ... 4395 files and directories currently installed.)
#0 14.59 Preparing to unpack .../00-openssl_3.0.2-0ubuntu1.8_amd64.deb ...
#0 14.60 Unpacking openssl (3.0.2-0ubuntu1.8) ...
#0 14.63 Selecting previously unselected package ca-certificates.
#0 14.63 Preparing to unpack .../01-ca-certificates_20211016ubuntu0.22.04.1_all.deb ...
#0 14.63 Unpacking ca-certificates (20211016ubuntu0.22.04.1) ...
#0 14.66 Selecting previously unselected package libnghttp2-14:amd64.
#0 14.66 Preparing to unpack .../02-libnghttp2-14_1.43.0-1build3_amd64.deb ...
#0 14.66 Unpacking libnghttp2-14:amd64 (1.43.0-1build3) ...
#0 14.68 Selecting previously unselected package libpsl5:amd64.
#0 14.68 Preparing to unpack .../03-libpsl5_0.21.0-1.2build2_amd64.deb ...
#0 14.68 Unpacking libpsl5:amd64 (0.21.0-1.2build2) ...
#0 14.70 Selecting previously unselected package publicsuffix.
#0 14.70 Preparing to unpack .../04-publicsuffix_20211207.1025-1_all.deb ...
#0 14.70 Unpacking publicsuffix (20211207.1025-1) ...
#0 14.72 Selecting previously unselected package libbrotli1:amd64.
#0 14.72 Preparing to unpack .../05-libbrotli1_1.0.9-2build6_amd64.deb ...
#0 14.72 Unpacking libbrotli1:amd64 (1.0.9-2build6) ...
#0 14.74 Selecting previously unselected package libsasl2-modules-db:amd64.
#0 14.74 Preparing to unpack .../06-libsasl2-modules-db_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
#0 14.75 Unpacking libsasl2-modules-db:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 14.76 Selecting previously unselected package libsasl2-2:amd64.
#0 14.76 Preparing to unpack .../07-libsasl2-2_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
#0 14.77 Unpacking libsasl2-2:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 14.78 Selecting previously unselected package libldap-2.5-0:amd64.
#0 14.79 Preparing to unpack .../08-libldap-2.5-0_2.5.14+dfsg-0ubuntu0.22.04.1_amd64.deb ...
#0 14.79 Unpacking libldap-2.5-0:amd64 (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 14.81 Selecting previously unselected package librtmp1:amd64.
#0 14.81 Preparing to unpack .../09-librtmp1_2.4+20151223.gitfa8646d.1-2build4_amd64.deb ...
#0 14.81 Unpacking librtmp1:amd64 (2.4+20151223.gitfa8646d.1-2build4) ...
#0 14.83 Selecting previously unselected package libssh-4:amd64.
#0 14.83 Preparing to unpack .../10-libssh-4_0.9.6-2build1_amd64.deb ...
#0 14.83 Unpacking libssh-4:amd64 (0.9.6-2build1) ...
#0 14.85 Selecting previously unselected package libcurl4:amd64.
#0 14.85 Preparing to unpack .../11-libcurl4_7.81.0-1ubuntu1.10_amd64.deb ...
#0 14.85 Unpacking libcurl4:amd64 (7.81.0-1ubuntu1.10) ...
#0 14.87 Selecting previously unselected package curl.
#0 14.87 Preparing to unpack .../12-curl_7.81.0-1ubuntu1.10_amd64.deb ...
#0 14.89 Unpacking curl (7.81.0-1ubuntu1.10) ...
#0 14.90 Selecting previously unselected package libldap-common.
#0 14.90 Preparing to unpack .../13-libldap-common_2.5.14+dfsg-0ubuntu0.22.04.1_all.deb ...
#0 14.91 Unpacking libldap-common (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 14.92 Selecting previously unselected package libsasl2-modules:amd64.
#0 14.92 Preparing to unpack .../14-libsasl2-modules_2.1.27+dfsg2-3ubuntu1.2_amd64.deb ...
#0 14.93 Unpacking libsasl2-modules:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 14.95 Setting up libpsl5:amd64 (0.21.0-1.2build2) ...
#0 14.95 Setting up libbrotli1:amd64 (1.0.9-2build6) ...
#0 14.96 Setting up libsasl2-modules:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 14.97 Setting up libnghttp2-14:amd64 (1.43.0-1build3) ...
#0 14.98 Setting up libldap-common (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 14.98 Setting up libsasl2-modules-db:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 14.99 Setting up librtmp1:amd64 (2.4+20151223.gitfa8646d.1-2build4) ...
#0 15.00 Setting up libsasl2-2:amd64 (2.1.27+dfsg2-3ubuntu1.2) ...
#0 15.00 Setting up libssh-4:amd64 (0.9.6-2build1) ...
#0 15.01 Setting up openssl (3.0.2-0ubuntu1.8) ...
#0 15.02 Setting up publicsuffix (20211207.1025-1) ...
#0 15.03 Setting up libldap-2.5-0:amd64 (2.5.14+dfsg-0ubuntu0.22.04.1) ...
#0 15.03 Setting up ca-certificates (20211016ubuntu0.22.04.1) ...
#0 15.09 debconf: unable to initialize frontend: Dialog
#0 15.09 debconf: (TERM is not set, so the dialog frontend is not usable.)
#0 15.09 debconf: falling back to frontend: Readline
#0 15.09 debconf: unable to initialize frontend: Readline
#0 15.09 debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.34.0 /usr/local/share/perl/5.34.0 /usr/lib/x86_64-linux-gnu/perl5/5.34 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.34 /usr/share/perl/5.34 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
#0 15.09 debconf: falling back to frontend: Teletype
#0 15.26 Updating certificates in /etc/ssl/certs...
#0 15.50 124 added, 0 removed; done.
#0 15.52 Setting up libcurl4:amd64 (7.81.0-1ubuntu1.10) ...
#0 15.53 Setting up curl (7.81.0-1ubuntu1.10) ...
#0 15.53 Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
#0 15.58 Processing triggers for ca-certificates (20211016ubuntu0.22.04.1) ...
#0 15.58 Updating certificates in /etc/ssl/certs...
#0 15.75 0 added, 0 removed; done.
#0 15.75 Running hooks in /etc/ca-certificates/update.d...
#0 15.76 done.
#0 15.78   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
#0 15.78                                  Dload  Upload   Total   Spent    Left  Speed
100 15916  100 15916    0     0  57611      0 --:--:-- --:--:-- --:--:-- 57458
#0 16.06 => Downloading nvm as script to '/root/.nvm'
#0 16.47 
#0 16.48 => Appending nvm source string to /root/.bashrc
#0 16.49 => Appending bash_completion source string to /root/.bashrc
#0 16.51 => Close and reopen your terminal to start using nvm or run the following to use it now:
#0 16.51 
#0 16.51 export NVM_DIR="$HOME/.nvm"
#0 16.51 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
#0 16.51 [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
#0 16.51 /bin/bash: line 1: nvm: command not found
------
Dockerfile:3
--------------------
   2 |     SHELL ["/bin/bash", "-c"]
   3 | >>> RUN apt update\
   4 | >>>     && apt install curl -y\
   5 | >>>     && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash\
   6 | >>>     && source ~/.bashrc\
   7 | >>>     && nvm install --lts\
   8 | >>>     && npm install -g npm
--------------------
ERROR: failed to solve: process "/bin/bash -c apt update    && apt install curl -y    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash    && source ~/.bashrc    && nvm install --lts    && npm install -g npm" did not complete successfully: exit code: 127

Observations

The line /bin/bash: line 1: nvm: command not found in the above output is different than the line /bin/sh: 1: source: not found in the output shown in my original post. The build fails but (seemingly) for a different reason.

Additional Attempt

Tried removing the | bash from the end of the curl command - thinking it may be causing a problem.

Result

Failed yielding the same output as shown above (in this reply).

Using Altered Dockerfile

FROM ubuntu:22.04
SHELL ["/bin/bash", "-c"]
RUN apt update\
    && apt install curl -y\
    && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh\
    && source ~/.bashrc\
    && nvm install --lts\
    && npm install -g npm

Observations

The line /bin/bash: line 1: nvm: command not found remains the same with and without | bash at the end of the curl command when SHELL ["/bin/bash", "-c"] is added to the file above the RUN directive.

I do not know what | bash` does or why I though it would affect the outcome but the results are failure to build in any case.

Additional Resource

If it helps any I have this short video showing how the commands run in an interactive terminal of the official ubuntu 22.04 image just fine and the results of build attempts using the original Dockerfile content as well as the two subsequent attempts using modifications of that Dockerfile.

-------------------------------------------------------- Update --------------------------------------------------------

Using Dockerfile

FROM ubuntu:22.04
RUN ["/bin/bash", "-c", "apt update", " apt install curl -y", "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh", "source ~/.bashrc", "nvm install --lts", "npm install -g npm"]

Output

$ docker build -t my-node:ubuntu-22.04 --no-cache .
[+] Building 16.5s (6/6) FINISHED                                                                                        
 => [internal] load .dockerignore                                                                                   0.0s
 => => transferring context: 2B                                                                                     0.0s
 => [internal] load build definition from Dockerfile                                                                0.0s
 => => transferring dockerfile: 257B                                                                                0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.04                                                     0.0s
 => CACHED [1/2] FROM docker.io/library/ubuntu:22.04                                                                0.0s
 => [2/2] RUN ["/bin/bash", "-c", "apt update", " apt install curl -y", "curl -o- https://raw.githubusercontent.c  16.3s
 => exporting to image                                                                                              0.2s
 => => exporting layers                                                                                             0.2s
 => => writing image sha256:abdb335287c4df97ce8e0863bfbc1f3889d2f039ea6bcc957f881f8743102cd9                        0.0s 
 => => naming to docker.io/library/my-node:ubuntu-22.04 

Result

In an interactive terminal on that image docker run -it my-node:ubuntu-22.04 when I run nvm -v, node -v or npm -v I get ... command not found for each of those commands run in the image.

What gives!

I’m not really sure why what I’m trying to do isn’t working…

What would a working Dockerfile look like that uses Ubuntu 22.04 as its base image and installs the latest lts version of node and the truly latest version of npm using nvm to do so?

A Solution…

Ok, so, I found a working way to do it w/ the help of folks on the #docker irc channel

Dockerfile

FROM ubuntu:22.04
RUN ["/bin/bash", "-c", "apt update && apt install curl -y && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash && source ~/.nvm/nvm.sh && nvm install --lts && npm install -g npm"]

The above works but it was mentioned that maybe I should use a different way to install node (a way that does not use a tool made for interactive use - ie: nvm). This makes snese and I wonder if there is a better way to go about this? Maybe to install a specific version of node via some other route that is more direct? What would that look like though?

You could use the official node images, or get some inspiration from the Dockerfiles used to create those image, like this one: docker-node/Dockerfile at main · nodejs/docker-node · GitHub

I hope someone who actually works with node pick up from here.

1 Like

Well those official node images do no provide an image based on Ubuntu (which I want). - but I will check out their Dockerfiles. Seems like I saw the source being hosted on github.

I linked the alpine image as example.

They provide Debian based images as well: bullseye and bullseye-slim,

They should be easily adaptable to Ubuntu.

The links I shared are from the github repo of the official images.