web-dev-qa-db-fra.com

Erreur: L'exécutable pg_config est introuvable lors de l'installation de psycopg2 sur Alpine dans Docker.

J'essaie de créer une application Flask à l'aide de Postgres avec Docker. J'aimerais me connecter à une instance AWS RDS de Postgres, mais utiliser Docker pour mon application Flask. Cependant, lors de la tentative d'installation de psycopg2, Une erreur se produit car elle ne trouve pas pg_config. Voici l'erreur:

Building api
Step 1/5 : FROM python:3.6.3-Alpine3.6
 ---> 84c98ca3b5c5
Step 2/5 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 407c158f5ee4
Step 3/5 : COPY . .
 ---> 966df18d329e
Step 4/5 : RUN pip install -r requirements.txt
 ---> Running in 284cc97aeb63
Collecting aniso8601==1.3.0 (from -r requirements.txt (line 1))
  Downloading aniso8601-1.3.0.tar.gz (57kB)
Collecting click==6.7 (from -r requirements.txt (line 2))
  Downloading click-6.7-py2.py3-none-any.whl (71kB)
Collecting Flask==0.12.2 (from -r requirements.txt (line 3))
  Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
Collecting Flask-RESTful==0.3.6 (from -r requirements.txt (line 4))
  Downloading Flask_RESTful-0.3.6-py2.py3-none-any.whl
Collecting Flask-SQLAlchemy==2.3.2 (from -r requirements.txt (line 5))
  Downloading Flask_SQLAlchemy-2.3.2-py2.py3-none-any.whl
Collecting itsdangerous==0.24 (from -r requirements.txt (line 6))
  Downloading itsdangerous-0.24.tar.gz (46kB)
Collecting Jinja2==2.9.6 (from -r requirements.txt (line 7))
  Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
Collecting MarkupSafe==1.0 (from -r requirements.txt (line 8))
  Downloading MarkupSafe-1.0.tar.gz
Collecting psycopg2==2.7.3.1 (from -r requirements.txt (line 9))
  Downloading psycopg2-2.7.3.1.tar.gz (425kB)
    Complete output from command python setup.py Egg_info:
    running Egg_info
    creating pip-Egg-info/psycopg2.Egg-info
    writing pip-Egg-info/psycopg2.Egg-info/PKG-INFO
    writing dependency_links to pip-Egg-info/psycopg2.Egg-info/dependency_links.txt
    writing top-level names to pip-Egg-info/psycopg2.Egg-info/top_level.txt
    writing manifest file 'pip-Egg-info/psycopg2.Egg-info/SOURCES.txt'
    Error: pg_config executable not found.

    Please add the directory containing pg_config to the PATH
    or specify the full executable path with the option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

    ----------------------------------------
Command "python setup.py Egg_info" failed with error code 1 in /tmp/pip-build-01lf5grh/psycopg2/
ERROR: Service 'api' failed to build: The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

Voici mon Dockerfile:

FROM python:3.6.3-Alpine3.6

WORKDIR /usr/src/app

COPY . .

RUN pip install -r requirements.txt

CMD ["python", "app.py"]

Beaucoup d’autres semblent avoir un problème similaire localement, mais aucun d’eux n’implique l’utilisation de Docker. Cela ressemble à un problème Docker car je peux configurer un environnement virtuel local et la configuration fonctionne parfaitement car Postgres est installé localement et il est capable de trouver mon pg_config local.

Il semble que lors de la création/de la configuration du conteneur, Docker tente de trouver pg_config Dans le conteneur. Existe-t-il un moyen d'installer un pg_config Dans le conteneur, même si je n'utiliserai pas d'instance conteneurisée de Postgres, mais plutôt l'instance sur RDS?

Toutes les suggestions sur la façon de contourner ce problème sont les bienvenues. Merci!

30
hidace

Testé avec Python 3.4.8, 3.5.5, 3.6.5 et 2.7.14 (il suffit de remplacer 3 par 2):

# You can use a specific version too, like python:3.6.5-Alpine3.7
FROM python:3-Alpine

WORKDIR /usr/src/app

COPY requirements.txt .

RUN \
 apk add --no-cache postgresql-libs && \
 apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev && \
 python3 -m pip install -r requirements.txt --no-cache-dir && \
 apk --purge del .build-deps

COPY . .

CMD ["python3", "app.py"]

Explication: pour construire Psycopg, vous avez besoin des paquets gcc musl-dev postgresql-dev. Ensuite, vous avez également besoin de cet exécutable pg_config: lors de l’installation simple de postgresql-dev marchera, postgresql-libs fonctionne bien aussi et prend environ 12 Mo de moins d'espace.


Voici la version originale de la réponse (basée sur this Dockerfile ) où j'installe manuellement Python sur une image pure Alpine, car à ce moment-là Python n'a pas fourni l'image Docker avec Python 3.6 et Alpine 3.7. Si vous voulez installer Python 2.7 comme ça, faites aussi apk add py2-pip (appelé py-pip dans les anciens dépôts alpins).

FROM Alpine:3.7

WORKDIR /usr/src/app

COPY requirements.txt .

RUN \
 apk add --no-cache python3 postgresql-libs && \
 apk add --no-cache --virtual .build-deps gcc python3-dev musl-dev postgresql-dev && \
 python3 -m pip install -r requirements.txt --no-cache-dir && \
 apk --purge del .build-deps

COPY . .

CMD ["python3", "app.py"]
52
Norrius

Tu pourrais essayer:

pip install psycopg2-binary
3
Vivin Veerali