build docker container with postgresql on rpi2

69 views Asked by At

I built django project with cookie-cutter-django. Everything works fine on my local, I could build and running stacks. Now, I am trying to deploy it to my raspberry pi 2. But having issue with psycopg.

First, here is the basic config of the project.

  • cookie-cutter-django
  • django 4.2.8
  • python 3.11.7

Secondly, here is my rpi os info.

  • Operating System: Raspbian GNU/Linux 11 (bullseye)
  • Kernel: Linux 6.1.21-v7+
  • Architecture: arm
  • postgresql is not installed

Thirdly, my local device for development.

  • ubuntu 22.04

I cloned the repo and tried to build production.yml. And got this error.

417.5 Collecting prompt-toolkit==3.0.43 (from -r production.txt (line 50))
417.6   Downloading prompt_toolkit-3.0.43-py3-none-any.whl.metadata (6.5 kB)
420.4 ERROR: Could not find a version that satisfies the requirement psycopg-binary==3.1.17 (from versions: none)
420.4 ERROR: No matching distribution found for psycopg-binary==3.1.17

I researched a lot, but couldn't find the right answer. The possible solution I could think of was installing the pure python installation on psycopg official documentation. so I tried to add libpq5 to Dockerfile, but having different error like this screenshot.

image

Here is my production Dockerfile.

FROM node:20-bullseye-slim as client-builder

ARG APP_HOME=/app
WORKDIR ${APP_HOME}

COPY ./package.json ${APP_HOME}
RUN npm install && npm cache clean --force
COPY . ${APP_HOME}
RUN npm run build
# define an alias for the specific python version used in this file.
FROM python:3.11.7-slim-bullseye as python

# Python build stage
FROM python as python-build-stage

ARG BUILD_ENVIRONMENT=production

# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
  # dependencies for building Python packages
  build-essential \
  # psycopg2 dependencies
  libpq-dev \
  libffi-dev

# Upgrade pip, wheel, and setuptools
RUN pip install wheel setuptools pip --upgrade

# Requirements are installed here to ensure they will be cached.
COPY ./requirements .

# Create Python Dependency and Sub-Dependency Wheels.
RUN pip wheel --wheel-dir /usr/src/app/wheels  \
  -r ${BUILD_ENVIRONMENT}.txt


# Python 'run' stage
FROM python as python-run-stage

ARG BUILD_ENVIRONMENT=production
ARG APP_HOME=/app

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${BUILD_ENVIRONMENT}

WORKDIR ${APP_HOME}

RUN addgroup --system django \
  && adduser --system --ingroup django django


# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
  # psycopg2 dependencies
  libpq-dev \
  # Translations dependencies
  gettext \
  # cleaning up unused files
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && rm -rf /var/lib/apt/lists/*

# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels  /wheels/

# use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
  && rm -rf /wheels/


COPY --chown=django:django ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint


COPY --chown=django:django ./compose/production/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
COPY --chown=django:django ./compose/production/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r$//g' /start-celeryworker
RUN chmod +x /start-celeryworker


COPY --chown=django:django ./compose/production/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r$//g' /start-celerybeat
RUN chmod +x /start-celerybeat


COPY --chown=django:django ./compose/production/django/celery/flower/start /start-flower
RUN sed -i 's/\r$//g' /start-flower
RUN chmod +x /start-flower


# copy application code to WORKDIR
COPY --from=client-builder --chown=django:django ${APP_HOME} ${APP_HOME}


# make django owner of the WORKDIR directory as well.
RUN chown django:django ${APP_HOME}

USER django

RUN DATABASE_URL="" \
  CELERY_BROKER_URL="" \
  DJANGO_SETTINGS_MODULE="config.settings.test" \
  python manage.py compilemessages

ENTRYPOINT ["/entrypoint"]

and here is my production.yaml

version: "3"

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}
  production_traefik: {}
  production_django_media: {}

services:
  django: &django
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile

    image: myproject_production_django
    volumes:
      - production_django_media:/app/apps/media
    depends_on:
      - postgres
      - redis
    env_file:
      - ./.envs/.production/.django
      - ./.envs/.production/.postgres
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: myproject_production_postgres
    volumes:
      - production_postgres_data:/var/lib/postgresql/data
      - production_postgres_data_backups:/backups
    env_file:
      - ./.envs/.production/.postgres

  traefik:
    build:
      context: .
      dockerfile: ./compose/production/traefik/Dockerfile
    image: myproject_production_traefik
    depends_on:
      - django
    volumes:
      - production_traefik:/etc/traefik/acme
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
      - "0.0.0.0:5555:5555"

  redis:
    image: redis:6

  celeryworker:
    <<: *django
    image: myproject_production_celeryworker
    command: /start-celeryworker

  celerybeat:
    <<: *django
    image: myproject_production_celerybeat
    command: /start-celerybeat

  flower:
    <<: *django
    image: myproject_production_flower
    command: /start-flower

  nginx:
    build:
      context: .
      dockerfile: ./compose/production/nginx/Dockerfile
    image: myproject_local_nginx
    depends_on:
      - django
    volumes:
      - production_django_media:/usr/share/nginx/media:ro

Is my rpi 2 and os too old? not support by psycopg 3? Please help.

1

There are 1 answers

0
jisoooh0202 On

I couldn't figure out the right way to use psycopg version 3++ so I downgrades to psycopg2 on requirements file for production.