Install psycopg2 for python: 2.7-alpine in Docker

To use PostgreSql in python, I need

pip install psycopg2 

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

Here is the Docker file:

 FROM python:2.7-alpine RUN apk add python-dev libpq-dev RUN pip install psycopg2 

and output:

Step 3: RUN apk add python-dev libpq-dev ---> Run at 3223b1bf7cde WARNING: Ignore APKINDEX.167438ca.tar.gz: There is no such file or directory WARNING: Ignore APKINDEX.a2e6dac0.tar.gz: There is no such file or ERROR directory: unsatisfactory restrictions: libpq-dev (missing): Required: world [libpq-dev] python-dev (missing): Required: world [python-dev] ERROR: Service 'service' could not be created: command '/ bin / sh -c apk add python-dev libpq-dev 'returned non-zero code: 2

+15
source share
5 answers

If you need to install psycopg2 for python 2.7 on a python based Docker image : 2.7-alpine , then the 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 
+20
source

Explanation before compiling / installing psycopg2

libpq - client library for PostgreSQL https://www.postgresql.org/docs/9.5/libpq.html

postgresql-dev is a package with headers for linking libpq in a library / binary, just like in psycopg.

I am using the following configuration in alpine 3.7, I am adding some comments to explain this.

 # Installing client libraries and any other package you need RUN 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 module RUN pip install psycopg2 # Delete build dependencies RUN apk del .build-deps 
+6
source

I could not install it from python:2.7.13-alpine . It ended:

 FROM gliderlabs/alpine:3.3 RUN apk add --no-cache --update \ python \ python-dev \ py-pip \ build-base RUN apk add --virtual build-deps gcc python-dev musl-dev && \ apk add --no-cache --update postgresql-dev && \ pip install psycopg2==2.7.1 
+5
source

It seems that you need the libpq package, not lobpq-dev:

https://pkgs.alpinelinux.org/package/edge/main/x86/py2-psycopg2

Look at the dependencies on the right

+4
source

add it to dockerfile

 RUN apk update && apk add --no-cache --virtual .build-deps\ postgresql-dev gcc libpq python3-dev musl-dev linux-headers\ && pip install --no-cache-dir -r requirements.txt\ && apk del .build-deps\ && rm -rf /var/cache/apk/* 
0
source

Source: https://habr.com/ru/post/1013949/


All Articles