2

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
Simon
  • 1,754
  • 14
  • 32
  • you sure it works without the script, when you exec into the container? – The Fool Feb 11 '22 at 09:41
  • No, sorry that wasn't clear from my part. The one with `exec` is when I do it manually *by hand*. Since I want to script it and just need to spin the container up for the duration of my build commands I would want to use `run` – Simon Feb 11 '22 at 09:44
  • does it work with exec or not? – The Fool Feb 11 '22 at 09:45
  • It works with exec yes. Manually in a terminal window step by step. Launching a terminal session in the docker container and `npm`works there – Simon Feb 11 '22 at 09:46
  • 1
    When you do it manually, how does the container stay alive? You've not defined a CMD or ENTRYPOINT, so it uses 'bash' as the command from the base Ubuntu image. Since it doesn't run in interactive mode, it will exit immediately. – Hans Kilian Feb 11 '22 at 09:59
  • My bad. Corrected the `run` command to what I use for the container to stay up. Had it in the code comment but forgot the `-dt`in the command – Simon Feb 11 '22 at 10:08

1 Answers1

1

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.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35