3

When I am inside the container, the following works:

pip uninstall -y -r <(pip freeze)

Yet when I try to do the same in my Dockerfile like this:

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

I get:

------
 > [ 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

How do I re-write this command to make it happy?

Edit:

I am using this work around. Still interested in the one-liner answer however

RUN pip freeze > to_delete.txt
RUN pip uninstall -y -r to_delete.txt
GlaceCelery
  • 921
  • 1
  • 13
  • 30
  • `RUN pip uninstall -y -r <\(pip freeze\)` – spinkus Sep 04 '21 at 03:42
  • That doesn't work, I get: `------ > [ 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 Sep 04 '21 at 03:54
  • However, this works for one package (without the redirected input): `RUN pip uninstall plotly -y` – GlaceCelery Sep 04 '21 at 03:55
  • Basically duplicate of [Difference between `sh` and `bash`](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Sep 09 '21 at 06:34

1 Answers1

5

The default shell for RUN is sh which looks not compatiable with above command.

You could change the shell for RUN to bash to make it work.

Dockerfile:

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

Execution:

$ 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
atline
  • 28,355
  • 16
  • 77
  • 113
  • Thank you. I see in places people use `SHELL ["/bin/bash", "--login", "-c" ]` does the `login` do anything helpful? – GlaceCelery Sep 04 '21 at 04:00
  • This related to `login shell` VS `non login shell`, login shell will execute `.bashrc` etc. when user login into system while `non login shell` not. But in docker scenario, I don't think there are any difference as we won't depend on this feature. – atline Sep 04 '21 at 04:03
  • Thank you. I have a follow on question here, if I could ask another favor... https://stackoverflow.com/questions/69052362/how-to-ignore-a-docker-build-step-fail – GlaceCelery Sep 04 '21 at 04:15