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
Recursive Definitions in Pandas
Unicodeencodeerror: 'Ascii' Codec Can't Encode Character '\Xe9' - -When Using Urlib.Request Python3
Query Mongodb on Month, Day, Year... of a Datetime
Smtplib Sends Blank Message If the Message Contain Certain Characters
Python: Why Does My List Change When I'm Not Actually Changing It
Weighted Choice Short and Simple
Simple Animation Using Tkinter
Why Can't I Repeat the 'For' Loop for CSV.Reader
How to Get All Combinations of Length N in Python
In Python, How to Test If I'm in Google App Engine Sdk
Managing Dynamic Plotting in Matplotlib Animation Module
How to Use 'Cv2.Findcontours' in Different Opencv Versions
Is There a Python Module to Solve Linear Equations
Reverse a Get_Dummies Encoding in Pandas
Python: Sort Function Breaks in the Presence of Nan
Using Python's Multiprocessing Module to Execute Simultaneous and Separate Seawat/Modflow Model Runs