pythondockerdocker-composewindows-subsystem-for-linux

Docker Compose Fails: error: externally-managed-environment


I am using a windows machine and have installed wsl to be able to use Docker desktop. Of course the build failed and then I observed python3 and pip3 in the dockerfile. So I installed ubuntu and debian via wsl and then tried to run the app (docker-compose up). It still fails and throws the following error:

 ERROR [test 3/5] RUN pip3 install daff==1.3.46                                                                                                                           0.9s
------
 > [test 3/5] RUN pip3 install daff==1.3.46:
0.812 error: externally-managed-environment
0.812
0.812 × This environment is externally managed
0.812 ╰─> To install Python packages system-wide, try apt install
0.812     python3-xyz, where xyz is the package you are trying to
0.812     install.
0.812
0.812     If you wish to install a non-Debian-packaged Python package,
0.812     create a virtual environment using python3 -m venv path/to/venv.
0.812     Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
0.812     sure you have python3-full installed.
0.812
0.812     If you wish to install a non-Debian packaged Python application,
0.812     it may be easiest to use pipx install xyz, which will manage a
0.812     virtual environment for you. Make sure you have pipx installed.
0.812
0.812     See /usr/share/doc/python3.11/README.venv for more information.
0.812
0.812 note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
0.812 hint: See PEP 668 for the detailed specification.
------
failed to solve: process "/bin/sh -c pip3 install daff==1.3.46" did not complete successfully: exit code: 1

Here's the DockerFile:

FROM postgres:12

RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip
RUN pip3 install daff==1.3.46

# Copy project files
COPY . /app
WORKDIR /app
ENV PATH=$PATH:/app/bin

Any idea how to resolve the issue? Because I'm using docker, this How do I solve "error: externally-managed-environment" everytime I use pip3? doesn't help me.


Solution

  • This error occurs due to this: PEP0668.

    I found the resolution for local machines while going through this answer. Specifically for docker, using the pip install command with the flag --break-system-packages worked for me.

    In your script update the line:
    RUN pip3 install daff==1.3.46
    with
    RUN pip3 install daff==1.3.46 --break-system-packages