Encontré un problema al intentar ejecutar mi proyecto django en un nuevo contenedor Docker. Es la primera vez que uso Docker y parece que no puedo encontrar una buena manera de ejecutar un proyecto Django en él. Después de probar varios tutoriales, siempre aparece el error de que psycopg2 no está instalado.
requisitos.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.1archivo acoplable:
# 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 = "***"Mientras ejecuto la compilación docker-compose, aparece el siguiente 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.Con mucho gusto responderé cualquier pregunta que pueda llevar a la solución. Además, tal vez alguien pueda recomendarme un buen tutorial sobre cómo dockerizar aplicaciones Django.
Lo hice funcionar. Este es el código:
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 psycopg2En Alpine Linux, deberá compilar todos los paquetes, incluso si una rueda binaria precompilada está disponible en PyPI. En las imágenes estándar basadas en Linux, no lo hará ( https://pythonspeed.com/articles/alpine-docker-python/ - también hay otros artículos que he escrito allí que pueden ser útiles, por ejemplo, sobre seguridad).
Así que cambie su imagen base a python:3.8.3-slim-buster o python:3.8-slim-buster y debería funcionar.
Estos scripts funcionan en 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.txtrequisitos.txt
psycopg2-binary~=2.8.6Respuesta actualizada de la respuesta de Zoltán Buzás