Docker构建中无法运行Shell命令

3

当我在容器内部时,以下操作有效:

pip uninstall -y -r <(pip freeze)

但是当我尝试像这样在Dockerfile中执行相同的操作:

RUN pip uninstall -y -r <(pip freeze)

我会收到以下错误:

------
 > [ 7/10] RUN pip uninstall -y -r <(pip freeze):
#11 0.182 /bin/sh: 1: Syntax error: "(" unexpected
------
executor failed running [/bin/sh -c pip uninstall -y -r <(pip freeze)]: exit code: 2

我该如何重写这个命令才能使其正常运行?

编辑:

我正在使用这个解决方法。但我仍然对短一点的答案感兴趣。

RUN pip freeze > to_delete.txt
RUN pip uninstall -y -r to_delete.txt

RUN pip uninstall -y -r <\(pip freeze\) - spinkus

这不起作用,我得到了以下错误信息:`------ > [ 7/10] RUN pip uninstall -y -r <(pip freeze): #11 0.206 /bin/sh: 1: cannot open (pip: No such file

executor failed running [/bin/sh -c pip uninstall -y -r <(pip freeze)]: exit code: 2`
- GlaceCelery
然而,这仅适用于一个软件包(没有重定向输入):RUN pip uninstall plotly -y - GlaceCelery
基本上是 Difference between sh and bash 的副本。 - tripleee
1个回答

5

RUN 的默认 shellsh,看起来与上面的命令不兼容。

你可以将 RUN 的 shell 更改为 bash 以使其正常工作。

Dockerfile:

FROM python:3
SHELL ["/bin/bash", "-c"]
RUN pip install pyyaml
RUN pip uninstall -y -r <(pip freeze)

执行:

$ docker build -t abc:1 .
Sending build context to Docker daemon   5.12kB
Step 1/4 : FROM python:3
 ---> da24d18bf4bf
Step 2/4 : SHELL ["/bin/bash", "-c"]
 ---> Running in da64b8004392
Removing intermediate container da64b8004392
 ---> 4204713810f9
Step 3/4 : RUN pip install pyyaml
 ---> Running in 17a91e8cc768
Collecting pyyaml
  Downloading PyYAML-5.4.1-cp39-cp39-manylinux1_x86_64.whl (630 kB)
Installing collected packages: pyyaml
Successfully installed pyyaml-5.4.1
WARNING: You are using pip version 20.3.3; however, version 21.2.4 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Removing intermediate container 17a91e8cc768
 ---> 1e6dba7439ac
Step 4/4 : RUN pip uninstall -y -r <(pip freeze)
 ---> Running in 256f7194350c
Found existing installation: PyYAML 5.4.1
Uninstalling PyYAML-5.4.1:
  Successfully uninstalled PyYAML-5.4.1
Removing intermediate container 256f7194350c
 ---> 10346fb83333
Successfully built 10346fb83333
Successfully tagged abc:1

谢谢。我看到有些人在某些地方使用 SHELL ["/bin/bash", "--login", "-c" ]login 有什么有用的作用吗? - GlaceCelery
这与 login shellnon login shell 相关,login shell 在用户登录系统时会执行 .bashrc 等文件,而 non login shell 则不会。但在 Docker 场景中,我认为没有任何区别,因为我们不依赖于这个特性。 - atline
谢谢。我这里有一个后续问题,如果可以的话,能否再帮个忙... https://stackoverflow.com/questions/69052362/how-to-ignore-a-docker-build-step-fail - GlaceCelery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接