-1

Here I'm going to use flake8 within a docker container. I installed flake8 using the following command and everything installed successfully.

$ sudo -H pip3 install flake8 // worked fine

and it's location path is-

/usr/local/lib/python3.8/dist-packages

Then I executed the following command but result was not expected.

$ sudo docker-compose run --rm app sh -c "flake8"

It says,

sh: flake8: not found

May be, the flake8 package is not installed in the correct location path. Please help me with this issue.

  • Hi @MarcinOrlowski, how can I add '/usr/local/lib/python3.8/dist-packages' to PATH? – Mizanur Rahman Dec 01 '22 at 05:19
  • I tried with $ sudo docker-compose run --rm app sh -c "/usr/local/lib/python3.8/dist-packages/flake8" ; but same returns -> sh: flake8: not found – Mizanur Rahman Dec 01 '22 at 05:25
  • 1) Are you installing flake8 inside the container, or outside the container? 2) You're using `--rm`. This removes changes to the container after each command is executed. – Nick ODell Dec 01 '22 at 05:46
  • `sudo pip install` is a bad idea to begin with -- you'd probably do better to use a virtualenv or a local install – anthony sottile Dec 01 '22 at 05:52
  • Hi @NickODell, I've installed flake8 with '$ sudo -H pip3 install flake8' – Mizanur Rahman Dec 01 '22 at 05:59
  • @MarcinOrlowski `/usr/local/lib/python3.8/dist-packages` is a directory for *libraries*, it should not be added to PATH; the corresponding `/bin/` directory should. – phd Dec 01 '22 at 12:42
  • That's right but I meant location of the `flake8` binary, whenever OP put it. None the less, I think @David-Maze's answer nails the exact culprit here. – Marcin Orlowski Dec 01 '22 at 13:11

1 Answers1

2

When you docker-compose run a container, it is in a new container in a new isolated filesystem. If you ran pip install in a debugging shell in another container, it won't be visible there.

As a general rule, never install software into a running container, unless it's for very short-term debugging. This will get lost as soon as the container exits, and it's very routine to destroy and recreate containers.

If you do need a tool like this, you should install it in your Dockerfile instead:

FROM python:3.10
...
RUN pip3 install flake8

However, for purely developer-oriented tools like linters, it may be better to not install them in Docker at all. A Docker image contains a fixed copy of your application code and is intended to be totally separate from anything on your host system. You can do day-to-day development, including unit testing and style checking, in a non-Docker virtual environment, and use docker-compose up --build to get a container-based integration-test setup.

David Maze
  • 130,717
  • 29
  • 175
  • 215