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
4
  • RUN pip uninstall -y -r <\(pip freeze\) Commented Sep 4, 2021 at 3: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 Commented Sep 4, 2021 at 3:54
  • However, this works for one package (without the redirected input): RUN pip uninstall plotly -y Commented Sep 4, 2021 at 3:55
  • Basically duplicate of Difference between sh and bash Commented Sep 9, 2021 at 6:34

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I see in places people use SHELL ["/bin/bash", "--login", "-c" ] does the login do anything helpful?
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.
Thank you. I have a follow on question here, if I could ask another favor... stackoverflow.com/questions/69052362/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.