How to run bash function in Dockerfile

I have a bash function nvm defined in /root/.profile . docker build failed to find that function when I call it in 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

The error is

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

I managed to call nvm by 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 is there a easier and cleaner way to call the bash function directly as it's normal binary without the bash -ic wrapping? Maybe something like

RUN load_functions && 
    nvm install 0.12 && 
    nvm alias default 0.12 && 
    nvm use 0.12

Docker's RUN doesn't start the command in a shell. That's why shell functions and shell syntax (like cmd1 && cmd2 ) cannot being used out of the box. You need to call the shell explicitly:

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

If you are afraid of that long command line, put those commands into a shell script and call the script with RUN :

script.sh

#!/bin/bash

nvm install 0.12 && 
nvm alias default 0.12 && 
nvm use 0.12

and make it executable:

chmod +x script.sh

In Dockerfile put:

RUN /path/to/script.sh
链接地址: http://www.djcxy.com/p/88000.html

上一篇: 包装的iOS CATextLayer无法按预期工作

下一篇: 如何在Dockerfile中运行bash函数