Location>code7788 >text

Problems with using python-slim mirrors that do not work with PostgreSQL

Popularity:644 ℃/2024-08-20 16:14:04

preamble

Didn't you re-do the DjangoStarter docker solution before?

At first the demo was deployed with a SQLite database, which worked fine, but I soon ran into problems when switching to PostgreSQL...

report an error

After docker starts, the app container reports an error

: Error loading psycopg2 or psycopg module

debugging

At first I thought it waspsycopg2 The library won't load, or it's in thepython_builder stage dependencies are not correctly copied to thefinal Stage containers, but changing a couple of places later and retrying didn't work.

next usepython src/ shell Go to the shell and test to see if you can importpsycopg2 storehouse

>>> import psycopg2
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/project/.venv/lib/python3.11/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: .5: cannot open shared object file: No such file or directory

Okay, those of you who are often pitted against docker should already see that the problem is with the system environment

Instead of the slim image it works fine with the normal python image...

settle (a dispute)

OK, now that the cause of the problem has been identified, it's time to get to work on fixing it.

Using psycopg2-binary

The easiest way to do this is to replace the psycopg2 library with psycopg2-binary, a precompiledpsycopg2 package, no need to compile the environment, easier to install.

Installing the libpq library manually

Since the libpq library is missing, install it in the docker image.

I haven't tested this method yet, it's cumbersome and inelegant

# Using the Python 3.11 slim base image
FROM python:3.11-slim

# Install the psycopg2 dependency
RUN apt-get update && apt-get install -y \
    libpq-dev \
    gcc \
    # Clear cache to reduce image size
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

Different python base mirrors

The python image is based on Debian and uses the latest stable version of Debian by default, or you can specify your own version, such aspython3.11-slim-bookworm This means that Debian 12 ("bookworm") is specified as the base image.

  • The python3.11-slim-bookworm base image is 424MB, and python3.11-slim is the same size.
  • The python 3.11 base image is 1.3 gigabytes, which is several times the size of the python 3.11 base image.

There's also the option of alpine mirroring, but you may run into more compatibility issues, so I won't toss it, I'm tired.

wrap-up

In general, slim mirrors are fine, and based on Debian, you don't need to worry about compatibility issues.

Occasional minor problems are still relatively easy to solve

The alpine mirrors are different in so many ways, it's too much of a toss up...