'Failing to install psycopg2-binary on new docker container

I have encountered a problem while trying to run my django project on a new Docker container. It is my first time using Docker and I can't seem to find a good way to run a django project on it. Having tried multiple tutorials, I always get the error about psycopg2 not being installed.

requirements.txt:

-i https://pypi.org/simple
asgiref==3.2.7
django-cors-headers==3.3.0
django==3.0.7
djangorestframework==3.11.0
gunicorn==20.0.4
psycopg2-binary==2.8.5
pytz==2020.1
sqlparse==0.3.1

Dockerfile:

# pull official base image
FROM python:3.8.3-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip

COPY ./requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

# set project environment variables
# grab these via Python's os.environ
# these are 100% optional here

ENV PORT=8000
ENV SECRET_KEY_TWITTER = "***"

While running docker-compose build, I get the following error:

Error: pg_config executable not found.

pg_config is required to build psycopg2 from source. Please add the directory

containing pg_config to the $PATH or specify the full executable path with the

option:

python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

If you prefer to avoid building psycopg2 from source, please install the PyPI

'psycopg2-binary' package instead.

I will gladly answer any questions that might lead to the solution. Also, maybe someone can recommend me a good tutorial on dockerizing django apps?



Solution 1:[1]

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.

Solution 2:[2]

I made it work. This is the code:

FROM python:3.8.3-slim #Image python:3.9.5-slim also works # Image python:3.9.5-slim-buster also works

RUN apt-get update \
    && apt-get -y install libpq-dev gcc \
    && pip install psycopg2
    

Solution 3:[3]

This scripts work on MacBook Air M1

Dockerfile

FROM ubuntu:20.04
RUN apt-get update && apt-get -y install libpq-dev gcc && pip install psycopg2
COPY requirements.txt /cs_account/
RUN pip3 install -r requirements.txt

requirements.txt

psycopg2-binary~=2.8.6

Updated answer from the answer of Zoltán Buzás

Solution 4:[4]

This worked for me. Try slim-buster image.

In your Dockerfile

FROM python:3.8.7-slim-buster

and in your requirements.txt file

psycopg2-binary~= <<version_number>>

Solution 5:[5]

I've made a custom image with

FROM python:alpine
ADD requirements.txt /
RUN apk update --no-cache \
&& apk add build-base postgresql-dev libpq --no-cache --virtual .build-deps \
&& pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r /requirements.txt \
&& apk del .build-deps
RUN apk add postgresql-libs libpq --no-cache

and requirements.txt

django
djangorestframework
psycopg2-binary

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Itamar Turner-Trauring
Solution 2
Solution 3 Sadidul Islam
Solution 4 Anil Poudyal
Solution 5 Aleksandr