psycopg2 installation for python:2.7-alpine in Docker

salehinejad picture salehinejad · Jan 10, 2017 · Viewed 22.8k times · Source

To use PostgreSql in python I need to

pip install psycopg2   

However, it has dependency on libpq-dev and python-dev. I wonder how can I install the dependencies in alpine? Thanks.

Here is a Dockerfile:

FROM python:2.7-alpine

RUN apk add python-dev libpq-dev
RUN pip install psycopg2

and the output is:

Step 3 : RUN apk add python-dev libpq-dev ---> Running in 3223b1bf7cde WARNING: Ignoring APKINDEX.167438ca.tar.gz: No such file or directory WARNING: Ignoring APKINDEX.a2e6dac0.tar.gz: No such file or directory ERROR: unsatisfiable constraints: libpq-dev (missing): required by: world[libpq-dev] python-dev (missing): required by: world[python-dev] ERROR: Service 'service' failed to build: The command '/bin/sh -c apk add python-dev libpq-dev' returned a non-zero code: 2

Answer

Sant picture Sant · Feb 14, 2017

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-alpine

RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add postgresql-dev

RUN pip install psycopg2