Search code examples
dockerjenkinsalpine-linuxpycryptodome

Docker Hub: Alpine Updated - Crash API


I'm trying to run an API via Docker, but I'm having this problem below:

27.47 error: externally-managed-environment
27.47 
27.47 × This environment is externally managed
27.47 ╰─> 
27.47     The system-wide python installation should be maintained using the system
27.47     package manager (apk) only.
27.47     
27.47     If the package in question is not packaged already (and hence installable via
27.47     "apk add py3-somepackage"), please consider installing it inside a virtual
27.47     environment, e.g.:
27.47     
27.47     python3 -m venv /path/to/venv
27.47     . /path/to/venv/bin/activate
27.47     pip install mypackage
27.47     
27.47     To exit the virtual environment, run:
27.47     
27.47     deactivate
27.47     
27.47     The virtual environment is not deleted, and can be re-entered by re-sourcing
27.47     the activate file.
27.47     
27.47     To automatically manage virtual environments, consider using pipx (from the
27.47     pipx package).
27.47 
27.47 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.
27.47 hint: See PEP 668 for the detailed specification.
------
failed to solve: process "/bin/sh -c apk update && apk add --no-cache python3 py3-pip && pip3 install pika google-cloud-bigquery python-dotenv pycryptodome" did not complete successfully: exit code: 1

In fact, until last week the API worked correctly where the container containing alpine was pulling alpine:latest. However, this error above does not appear when I put the version of alpine:3.14, but this version is no longer supported.

It turns out that in fact when leaving version 3.14 I can run the API on my local machine, however when I upload this same API to Jenkins with alpine:3.14 it stops working giving the same error as I had before as shown above.

Would anyone have any idea what I'm forgetting to look at or check please?

I tried alpine versions like 3.15, 3.16, 3.17, 3.18 and 3.19, but they all showed the same error as above. Version 3.14 of alpine works, but it is no longer supported, and, even though it runs on my local machine, it gives an error when I build it in Jenkins, citing the same errors as above.


Solution

  • The solution is in the error message. The PEP 668 standard provides a mechanism for Linux distributions to tell pip that it can't install packages outside a virtualenv, which is why you're having the problem on recent versions of Alpine only.

    Therefore you need to be inside a virtualenv to install packages with pip.

    If you need Python + Alpine the official Python docker registry provides Python images based on Alpine. I would recommend you using these.