How to run bash function in Dockerfile

I have a bash nvm function defined in /root/.profile . docker build could not find this function when I call it in the RUN step.

 RUN apt-get install -y curl build-essential libssl-dev && \ curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh RUN nvm install 0.12 && \ nvm alias default 0.12 && \ nvm use 0.12 

Mistake

 Step 5 : RUN nvm install 0.12 ---> Running in b639c2bf60c0 /bin/sh: nvm: command not found 

I managed to call nvm , wrapping it with bash -ic , which will load /root/.profile .

 RUN bash -ic "nvm install 0.12" && \ bash -ic "nvm alias default 0.12" && \ bash -ic "nvm use 0.12" 

The above method works fine, but it has a warning

 bash: cannot set terminal process group (1): Inappropriate ioctl for device bash: no job control in this shell 

And I want to know if there is a simpler and clearer way to call the bash function directly, like regular binary code without bash -ic ? Maybe something like

 RUN load_functions && \ nvm install 0.12 && \ nvm alias default 0.12 && \ nvm use 0.12 
+7
bash docker
source share
1 answer

Docker RUN does not run a command in the shell. Therefore, shell functions and shell syntax (for example, cmd1 & cmd2 ) cannot be used out of the box. You must explicitly call the shell:

 RUN bash -c 'nvm install 0.12 && nvm alias default 0.12 && nvm use 0.12' 

If you are afraid of this long command line, put these commands in a shell script and call the script using RUN :

script.sh

 #!/bin/bash nvm install 0.12 && \ nvm alias default 0.12 && \ nvm use 0.12 

and make it doable:

 chmod +x script.sh 

In the dockerfile put:

 RUN /path/to/script.sh 
+4
source share

All Articles