有些RUN命令在Docker上无法正常运行,但是将其放在容器中就可以正常运行。

我有一个Dockerfile用于一些与lua和torch相关的任务,并尝试使用luarocks安装一些rock。

FROM ubuntu:14.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

RUN apt-get update -y
RUN apt-get install -y curl git

RUN curl -s https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash
RUN git clone https://github.com/torch/distro.git ~/torch --recursive
RUN cd ~/torch; ./install.sh

RUN source ~/.bashrc

RUN luarocks install nngraph
RUN luarocks install optim
RUN luarocks install nn

RUN luarocks install cltorch
RUN luarocks install clnn

docker build运行良好,直到第一个luarocks调用: RUN luarocks install nngraph此时它停止并抛出错误:

/bin/sh: luarocks: command not found

如果我注释掉luarocks行,构建就可以正常运行。使用该镜像,我可以创建一个容器并使用bash运行luarocks。

当然,我不想每次启动容器时都要这样做,所以我想知道是否有什么我可以做来解决这个问题。我觉得这个问题与行RUN rm /bin/sh && ln -s /bin/bash /bin/sh有关,但我需要这个来运行行RUN source ~/.bashrc

谢谢。

点赞
用户5050071
用户5050071

每个 RUN 命令都在其自己的 shell 中运行,并且会创建一个新的层。

从 Docker 文档中可以了解到:

RUN(在 shell 中运行命令 - /bin/sh -c - shell 表单)

因此,当你运行 luarocks install <app> 时,它并不会在你源文件的 shell 中运行。

你必须提供 luarocks 的完整路径才能运行。下面是我成功运行的修改后的 Dockerfile:

FROM ubuntu:14.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

RUN apt-get update -y
RUN apt-get install -y curl git

RUN curl -s https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash
RUN git clone https://github.com/torch/distro.git ~/torch --recursive
RUN cd ~/torch; ./install.sh

RUN source ~/.bashrc

RUN /root/torch/install/bin/luarocks install nngraph
RUN /root/torch/install/bin/luarocks install optim
RUN /root/torch/install/bin/luarocks install nn
RUN /root/torch/install/bin/luarocks install cltorch
RUN /root/torch/install/bin/luarocks install clnn

更多详情请参见 这里 Docker RUN 文件的文档说明。

2015-10-03 03:41:12
用户2425939
用户2425939

正如Alex da Silva所指出的, 在Dockerfile中, sourcing .bashrc 在另一个shell中进行。

你也可以尝试在同一个shell中执行你的luarocks命令并 source 你的bashrc:

...
RUN source ~/.bashrc && luarocks install nngraph
RUN source ~/.bashrc && luarocks install optim
RUN source ~/.bashrc && luarocks install nn

RUN source ~/.bashrc && luarocks install cltorch
RUN source ~/.bashrc && luarocks install clnn
2015-10-03 05:19:08