繁体   English   中英

Dockerfile中的pip和docker-compose怎么用?

[英]How to use pip in Dockerfile with docker-compose?

我正在尝试使用 docker-compose.yml 文件构建 python 应用程序的 docker 图像。 但是在使用 pip 安装依赖项时出现错误。 这是我的 docker-compose 文件:

version: '3.8'

services:
  scaleway:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./pur_beurre/:/usr/src/pur_beurre/
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev

我的 Dockerfile:

# pull official base image
FROM python:3.8-alpine

# set work directory
WORKDIR /pur_beurre

# 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 . .

# collect static files
RUN python manage.py collectstatic --noinput

# add and run as non-root user
RUN adduser -D myuser
USER myuser

# run gunicorn
CMD gunicorn pur_beurre.wsgi:application --bind 0.0.0.0:$PORT

我的错误是:

Step 5/12 : RUN pip install --upgrade pip
 ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
 ---> Running in e87a839f9da3
Requirement already satisfied: pip in /usr/local/lib/python3.8/site-packages (21.2.4)
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f9cfffcc280>: Failed to establish a new connection: [Errno -3] Try again')': /simple/pip/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f9cfffccbe0>: Failed to establish a new connection: [Errno -3] Try again')': /simple/pip/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f9cfffccd30>: Failed to establish a new connection: [Errno -3] Try again')': /simple/pip/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f9cfffccee0>: Failed to establish a new connection: [Errno -3] Try again')': /simple/pip/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7f9cfffc1610>: Failed to establish a new connection: [Errno -3] Try again')': /simple/pip/
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
The command '/bin/sh -c pip install --upgrade pip' returned a non-zero code: 4294967295: failed to shutdown container: container e87a839f9da317220a77213c962f4c30eb4bc005f990a977f10eb4beda429028 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110): subsequent terminate failed container e87a839f9da317220a77213c962f4c30eb4bc005f990a977f10eb4beda429028 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110)
ERROR: Service 'scaleway' failed to build : Build failed

我试图从 Dockerfile 中删除命令RUN pip install --upgrade pip pip 但随后出现此错误:

Step 6/11 : RUN pip install -r requirements.txt
 ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (windows/amd64) and no specific platform was requested
 ---> Running in 795acf389f1c
container 795acf389f1ca89d04927a93818b745c38e6ab624f34a7190d855797302cf258 encountered an error during hcsshim::System::CreateProcess: failure in a Windows system call: Unspecified error (0x80004005)
[Event Detail: failed to run runc create/exec call for container 795acf389f1ca89d04927a93818b745c38e6ab624f34a7190d855797302cf258: exit status 1 Stack Trace:
github.com/Microsoft/opengcs/service/gcs/runtime/runc.(*container).startProcess
        /go/src/github.com/Microsoft/opengcs/service/gcs/runtime/runc/runc.go:580
github.com/Microsoft/opengcs/service/gcs/runtime/runc.(*runcRuntime).runCreateCommand
        /go/src/github.com/Microsoft/opengcs/service/gcs/runtime/runc/runc.go:471
github.com/Microsoft/opengcs/service/gcs/runtime/runc.(*runcRuntime).CreateContainer
        /go/src/github.com/Microsoft/opengcs/service/gcs/runtime/runc/runc.go:113
github.com/Microsoft/opengcs/service/gcs/core/gcs.(*gcsCore).ExecProcess
        /go/src/github.com/Microsoft/opengcs/service/gcs/core/gcs/gcs.go:351
github.com/Microsoft/opengcs/service/gcs/bridge.(*Bridge).execProcess
        /go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:637
github.com/Microsoft/opengcs/service/gcs/bridge.(*Bridge).execProcess-fm
        /go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:253
github.com/Microsoft/opengcs/service/gcs/bridge.HandlerFunc.ServeMsg
        /go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:72
github.com/Microsoft/opengcs/service/gcs/bridge.(*Mux).ServeMsg
        /go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:146
github.com/Microsoft/opengcs/service/gcs/bridge.(*Bridge).ListenAndServe.func2.1
        /go/src/github.com/Microsoft/opengcs/service/gcs/bridge/bridge.go:335
runtime.goexit
        /usr/lib/go/src/runtime/asm_amd64.s:1333 Provider: 00000000-0000-0000-0000-000000000000]
ERROR: Service 'scaleway' failed to build : Build failed

从错误看来,您需要提供系统的架构。

您可以向 docker build(甚至 docker-compose)提供--platform选项来定义要为其构建映像的目标平台。

在您的情况下,在构建图像时:

docker build --platform windows/amd64  .

您也可以在FROM语句中提供

FROM --platform=windows/amd64 python:3.8-alpine

尝试使用以下内容创建文件pip.conf<\/code>并将其放入您的图像中

点子配置文件<\/strong>

[global]
timeout = 60
index-url = https://pypi.python.org/simple
trusted-host = pypi.python.org

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM