我正在使用 Docker 编写一个带有 postgresql 的 Django 应用程序。但是,由于某种原因,构建会返回错误"E: Unable to locate package postgresql-server-dev-10 The command '/bin/sh -c apt-get update && apt-get install postgresql-server-dev-10 gcc python3-dev musl-dev' returned a non-zero code: 100
。
我尝试将其更改为sudo apt-get install postgresql postgresql-client
,我还尝试安装 python,但似乎没有任何效果。在我的终端中运行sudo apt install postgresql-server-dev-10
可以工作(我已经安装了它),但是在构建时它失败了(docker-compose build
)。
我的 Dockerfile 是:
FROM python:3.8
# set work directory
WORKDIR /weather_api
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apt-get update \
&& apt-get install postgresql-server-dev-10 gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .
docker-compose.yml 是:
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./:/weather_api/
ports:
- 8000:8000
env_file:
- ./.env.dev
db:
image: postgres:10.19
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=lavinia
- POSTGRES_PASSWORD=#123$$
- POSTGRES_DB=weatherdb
volumes:
postgres_data:
和要求.txt
Django==3.2.9
psycopg2-binary==2.9.1
如果有人能帮助我,我将非常非常感激。我真的在网上找不到任何有用的东西,而且我是初学者。谢谢你!!
答案1
该python:3.8
镜像基于Debian 11,因此您需要的包是postgresql-server-dev-13
。
安装起来会更简单postgresql-server-dev-all
,它会为您引入正确的版本:
FROM python:3.8
# set work directory
WORKDIR /weather_api
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install psycopg2 dependencies
RUN apt-get update \
&& apt-get install -y postgresql-server-dev-all gcc python3-dev musl-dev
# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# copy project
COPY . .