简体   繁体   中英

How to use pip in Dockerfile with docker-compose?

I am trying to build a docker image of a python application using a docker-compose.yml file. But I am having errors when it come to using pip to install dependencies. Here is my docker-compose file:

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

My 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

And the error I have:

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

I tried to erase the command RUN pip install --upgrade pip from the Dockerfile but then I have this error:

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

From the error it seems that you need to provide your system's arhitecture.

You can provide the --platform option to docker build (or even to docker-compose) to define the target platform you want to build the image for.

In your case, when building an image:

docker build --platform windows/amd64  .

You can also provide in the FROM statement

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

Try to create file pip.conf<\/code> with the content below and put it into your image

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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