Search code examples
pythondockerpytorch

Could not find a version that satisfies the requirement torch when compiling


I have a code running on a Mac mini M2 that requires torch and torch audio, and that works as expected when testing it in VSCode but when compiling into Docker I get this error, any idea how to fix it? I've found some other posts online regarding the python versions, I did try changing versions (3.10, 3.11) and even using a lower version of torch (1.8.0) but none of that made any difference.

Thank you.

Dockerfile

FROM --platform=linux/arm64 python:3.9-alpine3.15

# Upgrade pip
RUN python -m pip install --upgrade pip

# Install system dependencies
RUN apk add --no-cache build-base \
    && apk add --no-cache ffmpeg-dev ffmpeg \
    && apk add --no-cache pkgconfig

# Copy the project code
ADD . /code
WORKDIR /code

# Install Python dependencies from requirements.txt
RUN pip install -r requirements.txt

# Expose the necessary port
EXPOSE 80

# Define the command to run the application
CMD python app.py

requirements.txt

flask
requests
torch==2.0.0
torchaudio==2.0.1

Error:

Building 25.2s (11/11) FINISHED                                                                                                                     docker:desktop-linux
 => [web internal] load build definition from Dockerfile                                                                                                                0.0s
 => => transferring dockerfile: 559B                                                                                                                                    0.0s
 => [web internal] load metadata for docker.io/library/python:3.9-alpine3.15                                                                                            0.7s
 => [web auth] library/python:pull token for registry-1.docker.io                                                                                                       0.0s
 => [web internal] load .dockerignore                                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                                         0.0s
 => [web 1/6] FROM docker.io/library/python:3.9-alpine3.15@sha256:86d5546236a8f8dd6505a92c40db8a01f8c22b7425f32396b6bfe6ef4bd18230                                      0.0s
 => [web internal] load build context                                                                                                                                   3.5s
 => => transferring context: 8.42MB                                                                                                                                     3.2s
 => CACHED [web 2/6] RUN python -m pip install --upgrade pip                                                                                                            0.0s
 => CACHED [web 3/6] RUN apk add --no-cache build-base     && apk add --no-cache ffmpeg-dev ffmpeg     && apk add --no-cache pkgconfig                                  0.0s
 => [web 4/6] ADD . /code                                                                                                                                              19.1s
 => [web 5/6] WORKDIR /code                                                                                                                                             0.1s
 => ERROR [web 6/6] RUN pip install -r requirements.txt                                                                                                                 1.7s
------                                                                                                                                                                       
 > [web 6/6] RUN pip install -r requirements.txt:                                                                                                                            
1.239 Collecting flask (from -r requirements.txt (line 1))                                                                                                                   
1.416   Downloading flask-3.0.2-py3-none-any.whl.metadata (3.6 kB)                                                                                                           
1.461 Collecting requests (from -r requirements.txt (line 4))                                                                                                                
1.490   Downloading requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)

1.610 ERROR: Could not find a version that satisfies the requirement torch==2.0.0 (from versions: none)
1.610 ERROR: No matching distribution found for torch==2.0.0

Solution

  • The problem is that you are using the Alpine base image, which is not ideal for installing Torch.

    There are a couple of options:

    Option 1: Use Debian base image

    FROM python:3.9
    
    WORKDIR /code
    ADD . .
    
    RUN pip install -r requirements.txt
    
    EXPOSE 80
    
    CMD python app.py
    

    Minimal requirements.txt:

    torch==2.0.0
    torchaudio==2.0.1
    

    Option 2: Use a PyTorch base image

    Here requirements.txt need not include either torch or torchaudio since they are already in the base image.

    FROM pytorch/pytorch:2.0.0-cuda11.7-cudnn8-runtime
    
    WORKDIR /code
    ADD . .
    
    RUN pip install -r requirements.txt
    
    EXPOSE 80
    
    CMD python app.py