首页 > 解决方案 > Shell 命令不会在 Docker 构建中运行

问题描述

当我在容器内时,以下工作:

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

标签: linuxbashdocker

解决方案


默认值shellRUN看起来sh与上述命令不兼容。

您可以更改外壳RUNbash使其工作。

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

推荐阅读