簡體   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