django - 构建 python 镜像时出现 Docker compose 问题,访问被拒绝或存储库不存在

标签 django linux docker docker-compose docker-multi-stage-build

我正在尝试排除故障 a tutorial on udemy在 Windows 10 上,但为什么我的撰写文件无法运行。我尝试重新登录 Docker,并且能够成功运行 Docker build python:3.9-slim-bullseye 。这里有人看到我的问题并帮助我了解发生了什么吗?

我认为我的问题是:

apt-get update && apt-get install --no-install-recommends -y \
    build-essential \
    libpq-dev

教程的original code is here on his github但我的大部分代码都是他的直接副本。这是link to the repo我已经了解了到目前为止所涵盖的内容。

local.yml 文件

version: "3.9"

services:
  api:
    build:
      context: .
      dockerfile: docker/local/django/Dockerfile
    command: /start
    container_name: django-api
    volumes:
      - .:/app
      - static_volume:/app/staticfiles
      - media_volume:/app/mediafiles
    ports:
      - "8000:8000"
    env_file:
      - ./.envs/.local/.django
      - ./.envs/.local/.postgres
    depends_on:
      - postgres
      - mailhog
    networks:
      - authors-api-live

  postgres:
    build:
      context: .
      dockerfile: ./docker/local/postgres/Dockerfile
    container_name: postgres
    # volumes:  # This is also causing issues
    #     - local_postgres_data:/var/lib/postgresql/data
    #     - local_postgres_data_backups:/backups
    env_file:
        - ./.envs/.local/.postgres
    networks:
        - authors-api-live
    
  mailhog:
    image: mailhog/mailhog:v1.0.0
    container_name: mailhog
    ports:
      - "8025:8025"
    networks:
      - authors-api-live

networks:
  authors-api-live:
    driver: bridge

volumes:
  local_postgres_data_backups: {}
  static_volume:
  media_volume:

Django Dockerfile

ARG PYTHON_VERSION=3.9-slim-bullseye

FROM python:${PYTHON_VERSION} as python

# BUILD STAGE
FROM python as pthyon-build-stage
ARG BUILD_ENVIORMENT=local

RUN apt-get update && apt-get install --no-install-recommends -y \
    build-essential \
    libpq-dev

COPY ./requirements . 

RUN pip wheel --wheel-dir /usr/src/app/wheels \
    -r ${BUILD_ENVIORMENT}.txt


# RUN STAGE
FROM python as python-run-stage

ARG BUILD_ENVIORMENT=local
# ARG APP_HOME=/usr/src/app
ARG APP_HOME=/app

ENV PYTHONDONTWRITEBYTECODE 1

ENV UNBUFFERED 1

ENV BUILD_ENVIORMENT=${BUILD_ENVIORMENT}

WORKDIR ${APP_HOME}

RUN apt-get update && apt-get install --no-install-recommends -y \
    libpq-dev \
    gettext \
    && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
    && rm -rf /var/lib/apt/lists/*

# use wheels to make installation faster
COPY --from=python-build-stage /usr/src/app/wheels /wheels/

RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
    && rm -rf /wheels/
    
# Copy in entrypoint to test connection to database
COPY ./docker/local/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint

# check if models are migrated
COPY ./docker/local/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start

# COPY ./docker/local/django/celery/worker/start /start-celeryworker
# RUN sed -i 's/\r$//g' /start-celeryworker
# RUN chmod +x /start-celeryworker

# COPY ./docker/local/django/celery/flower/start /start-flower
# RUN sed -i 's/\r$//g' /start-flower
# RUN chmod +x /start-flower

COPY . ${APP_HOME}

ENTRYPOINT ["/entrypoint"]

错误消息:

PS C:\Users\...\MasterDjangoRESTFrameworkWithDocker\authors-haven-api\authors-src> docker-compose -f local.yml up -d --remove-orphans   
Building api
[+] Building 1.2s (9/18)
 => [internal] load build definition from Dockerfile                                                                                                       0.1s 
 => => transferring dockerfile: 32B                                                                                                                        0.0s 
 => [internal] load .dockerignore                                                                                                                          0.1s 
 => => transferring context: 34B                                                                                                                           0.0s 
 => [internal] load metadata for docker.io/library/python:3.9-slim-bullseye                                                                                0.0s 
 => [internal] load build context                                                                                                                          0.2s 
 => => transferring context: 5.14kB                                                                                                                        0.1s 
 => [python 1/1] FROM docker.io/library/python:3.9-slim-bullseye                                                                                           0.0s 
 => ERROR FROM docker.io/library/python-build-stage:latest                                                                                                 0.9s
 => => resolve docker.io/library/python-build-stage:latest                                                                                                 0.9s 
 => CACHED [python-run-stage  1/11] WORKDIR /app                                                                                                           0.0s 
 => CANCELED [python-run-stage  2/11] RUN apt-get update && apt-get install --no-install-recommends -y     libpq-dev     gettext     && apt-get purge -y   1.0s 
 => [auth] library/python-build-stage:pull token for registry-1.docker.io                                                                                  0.0s 
------
 > FROM docker.io/library/python-build-stage:latest:
------
failed to load cache key: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed  
ERROR: Service 'api' failed to build : Build failed

最佳答案

您分配构建阶段名称的行似乎存在拼写错误。

FROM python as pthyon-build-stage

将其更改为:

FROM python as python-build-stage

并且构建阶段应按照后续阶段的预期命名。

关于django - 构建 python 镜像时出现 Docker compose 问题,访问被拒绝或存储库不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74278629/

相关文章:

如果将输出重定向到文件,Linux 进程行为不当 (MQTT)

linux - 伪装docker接口(interface)和主机接口(interface)

python - 向复杂的 Django View 添加值

django - django中自定义清理表单数据

linux - Docker bash'ing with find

python - 仅使用 Python 访问 Linux 中的 SMART 属性?

docker - 启动用户态代理 : listen tcp 0. 0.0.0:2049 时出错:绑定(bind):地址已在使用中

Python 请求模块卡在套接字连接上,但 cURL 有效

javascript - 显示代码的 Django 模板

django - 我有多个 django 表,想并行查询这些表