Home > Software engineering >  Docker run bash node: command not found
Docker run bash node: command not found

Time:02-11

I've the following Dockerfile

FROM ubuntu:latest

WORKDIR /

# Without interactive dialogue
ARG DEBIAN_FRONTEND=noninteractive

# Install required packages
RUN apt-get update
RUN apt-get install -y wget gnupg2 software-properties-common git apt-utils vim dirmngr apt-transport-https ca-certificates zip

ENV NODE_VERSION=14

RUN wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash \
    && . .$HOME/.nvm/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

# Install Wine from WineHQ Repository
RUN dpkg --add-architecture i386
RUN wget -qO- https://dl.winehq.org/wine-builds/Release.key | apt-key add -
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv F987672F
RUN apt-add-repository 'deb https://dl.winehq.org/wine-builds/ubuntu/ bionic main'
RUN apt-get update
RUN apt-get install -y --install-recommends winehq-stable

# Installing mono
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
RUN sh -c 'echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" > /etc/apt/sources.list.d/mono-official-stable.list'
RUN apt-get update
RUN apt-get install -y mono-complete

RUN PROJECT_DIR=/root/project

WORKDIR /project

Which allows me to build electron Apps in no matter what platform I'm on. Everything works well. Image building and also running it as a container works with no issues.

I then can go ahead and manually do the following:

#build the dockerfile
docker build -t debian-wine-electron-builder:0.1 .

#run the docker image as detached container
docker run -dt --name electron-build -v ${PWD}:/project debian-wine-electron-builder:0.1

#open a bash session in the container
docker exec -it electron-build bash

#and in there execute my commands which will build me the electron application
npm install ... #and so on

I now want to created a bash file to run all the commands making it easier to use. So one just has to run the bash file and it executes all the commands one by one. When I want to use the docker run command followed by a `bash -c "command1 ; command2" I run into the following issue:

#building the image
docker build -t debian-wine-electron-builder:0.1 .

#docker run following commands
docker run --name electron-build -v ${PWD}:/project debian-wine-electron-builder:0.1 bash -c "npm install... ; command2 ..."

Gives me this error when trying to npm install:

bash: npm: command not found

CodePudding user response:

When you run bash interactively, your .bashrc file will be run. When you run the command directly on the docker run command, bash is run non-interactively and .bashrc isn't run.

NVM uses .bashrc to set things up, so it needs to run.

You can force bash into interactive mode with the -i option. Then it'll work, but you'll get some warnings because it tries to do some terminal stuff that fails.

docker run --name electron-build -v ${PWD}:/project debian-wine-electron-builder:0.1 bash -i -c "npm install... ; command2 ..."

If you don't use nvm to switch node versions on the fly, it might be better to install the node version you want without using nvm.

  • Related