Skip to content

Commit f7ceb4e

Browse files
committed
Simplify nvm installation within Dockerfile
Finally found out that Dockerfile's SHELL `bash -l` works for .profile but not .bashrc. Thus, we can simply change the nvm-install.sh script to modify PATH in .profile and remove the duplicated env vars within Dockerfile.
1 parent 3660424 commit f7ceb4e

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Dockerfile

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ FROM ubuntu:20.04
1313
# Change default shell to Bash for better feature support/easier usage.
1414
# Note: Removes the need for specifying an `ENTRYPOINT` since Bash will now
1515
# be the default fallback.
16-
SHELL [ "/bin/bash", "-c" ]
16+
#
17+
# Login shells will correctly source `$HOME/.profile` but the if-statements
18+
# for sourcing .bashrc don't work as expected within Docker. Thus, any modifications
19+
# to the environment should be done within .profile (e.g. installing nvm).
20+
# See:
21+
# - https://stackoverflow.com/questions/55206227/why-bashrc-is-not-executed-when-run-docker-container/74017557#74017557
22+
SHELL [ "/bin/bash", "-lc" ]
1723
ENV SHELL=/bin/bash
1824

1925
# `docker` flags useful for debugging:
@@ -56,15 +62,14 @@ WORKDIR /home
5662
# Copy the entire app (server/client) from the local filesystem to the Docker image
5763
COPY . .
5864

59-
# Even if we add nvm vars to .bashrc and set SHELL to `bash -l -c`, Docker won't
60-
# pick them up so node won't be on PATH.
61-
# Thus, manually set them here.
62-
RUN ./nvm-install.sh
65+
# Install nvm. Either set vars in .profile and use `bash -lc` as SHELL in Docker,
66+
# or set the vars below in Dockerfile.
6367
# Note: HOME isn't defined during image-building, so we defined it above
64-
ENV NVM_DIR="$HOME/.nvm"
65-
ENV NVM_SYMLINK_CURRENT=true
66-
ENV NVM_CURRENT_HOME="$NVM_DIR/current"
67-
ENV PATH="$NVM_CURRENT_HOME/bin:$PATH"
68+
# ENV NVM_DIR="$HOME/.nvm"
69+
# ENV NVM_SYMLINK_CURRENT=true
70+
# ENV NVM_CURRENT_HOME="$NVM_DIR/current"
71+
# ENV PATH="$NVM_CURRENT_HOME/bin:$PATH"
72+
RUN ./nvm-install.sh
6873

6974
ENV ROOT_DIR=./
7075
ENV CLIENT_DIR="${ROOT_DIR}/client"

nvm-install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export NVM_DIR=\"\$([[ -n \"\$XDG_CONFIG_HOME\" ]] && echo \"\$XDG_CONFIG_HOME/n
8787
export NVM_SYMLINK_CURRENT=true # Makes a symlink at ~/.nvm/current/bin/node so you don't have to change IDEs' configurations when changing node versions
8888
export NVM_CURRENT_HOME=\"\$NVM_DIR/current\"
8989
export PATH=\"\$NVM_CURRENT_HOME/bin:\$PATH\"
90-
" >> $HOME/.bashrc
90+
" >> $HOME/.profile
9191

9292

9393
# Create `.nvm/current/` dir

0 commit comments

Comments
 (0)