psycopg2 installation for python:2.7-alpine in Docker
If you only need to install psycopg2 for python 2.7 on Docker image based on python:2.7-alpine then following code for Dockerfile will be nice for you:
FROM python:2.7-alpineRUN apk update && \ apk add --virtual build-deps gcc python-dev musl-dev && \ apk add postgresql-devRUN pip install psycopg2
An explanation before compile/install psycopg2
libpq
is the client library for PostgreSQLpostgresql-dev
are the package with the source headers to linklibpq
in a library/binary in a compilation, in this case when pip compilespsycopg
.
I use the following configuration in alpine 3.7
, I add some comments to explain it.
# Installing client libraries and any other package you needRUN apk update && apk add libpq# Installing build dependencies# For python3 you need to add python3-dev *please upvote the comment# of @its30 below if you use this*RUN apk add --virtual .build-deps gcc python-dev musl-dev postgresql-dev# Installing and build python moduleRUN pip install psycopg2# Delete build dependenciesRUN apk del .build-deps
I couldn't get it to install from python:2.7.13-alpine
. Ended up with this:
FROM gliderlabs/alpine:3.3RUN apk add --no-cache --update \ python \ python-dev \ py-pip \ build-baseRUN apk add --virtual build-deps gcc python-dev musl-dev && \ apk add --no-cache --update postgresql-dev && \ pip install psycopg2==2.7.1