Error: Pg_Config Executable Not Found When Installing Psycopg2 on Alpine in Docker

Error: pg_config executable not found when installing psycopg2 on Alpine in Docker

Tested with Python 3.4.8, 3.5.5, 3.6.5 and 2.7.14 (just replace 3 with 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"]

Explanation: to build Psycopg you need the packages gcc musl-dev postgresql-dev. Then you also need that pg_config executable: while simply installing postgresql-dev will work, postgresql-libs does fine too and takes up some 12 MB less space.


Here's the original version of the answer (based on this Dockerfile) where I manually install Python onto a pure Alpine image because at that time Python did not provide the Docker image with Python 3.6 and Alpine 3.7. If you want to install Python 2.7 like that, also do apk add py2-pip (called py-pip in older Alpine repos).

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"]

Failing to install psycopg2-binary on new docker container

On Alpine Linux, you will need to compile all packages, even if a pre-compiled binary wheel is available on PyPI. On standard Linux-based images, you won't (https://pythonspeed.com/articles/alpine-docker-python/ - there are also other articles I've written there that might be helpful, e.g. on security).

So change your base image to python:3.8.3-slim-buster or python:3.8-slim-buster and it should work.



Related Topics



Leave a reply



Submit