Docker容器命令仅在交互模式下执行

I have a dockerfile that builds an image off ubuntu. I install nvm, and can use it in the dockerfile. However, I can only seem to run nvm when using interactive shell on the image. I am trying to use nvm from a child_process in my node script, is there any way to get this working?

Here is my dockerfile

FROM ubuntu:bionic-20200403

RUN apt-get update && apt-get install -y \
    curl=7.58.0-2ubuntu3.8 \
  && rm -rf /var/lib/apt/lists/*


# https://stackoverflow.com/a/50958281/1833014
ENV NVM_VERSION 0.35.3
ENV NODE_VERSION 12.13.0
ENV NVM_DIR /usr/local/nvm

WORKDIR $NVM_DIR

RUN curl https://raw.githubusercontent.com/creationix/nvm/v$NVM_VERSION/install.sh | bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION


ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH


# Create app directory
WORKDIR /usr/src/app

# Bundle app bundle
COPY ./dist/index.js .

CMD [ "node", "index.js" ]

I added a few lines to index.js to help debug. Each line has its output commented in.

import child_process from 'child_process';

console.log(process.env.NVM_DIR);
// /usr/local/nvm

child_process.execSync('ls $NVM_DIR', { stdio: 'inherit', shell: '/bin/bash' });
/*
alias
bash_completion
nvm-exec
nvm.sh
versions
*/

child_process.execSync('/usr/local/nvm/nvm.sh -v', {
  stdio: 'inherit',
  shell: '/bin/bash',
});
// /bin/bash: /usr/local/nvm/nvm.sh: Permission denied

child_process.execSync('nvm -v', {
  stdio: 'inherit',
  shell: '/bin/bash',
});
// /bin/bash: nvm: command not found
评论