Cannot import PyTorch in Alpine Docker Container

The MRE below should be enough to explain what I’m trying to do. Google doesn’t help as it seems to be a unique error, which surprised me since Alpine & PyTorch are both fairly popular.

Dockerfile:

FROM nginx:stable-alpine as production
WORKDIR /app
RUN apk update && apk add --no-cache python3 bash && \
  python3 -m ensurepip && \
  rm -r /usr/lib/python*/ensurepip && \
  pip3 install --upgrade pip setuptools && \
  if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
  if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
  rm -r /root/.cache
RUN apk update && apk add gcc g++ python3-dev musl-dev
RUN pip install torch==1.10.1 --find-links https://download.pytorch.org/whl/torch_stable.html

Build & bash into it:

sudo docker build -t mini .
sudo docker run --name torch-pls -it --entrypoint bash mini

Launch Python:

bash-5.1# python
Python 3.9.5 (default, Nov 24 2021, 21:19:13) 
[GCC 10.3.1 20210424] on linux
Type "help", "copyright", "credits" or "license" for more information.

Import PyTorch and see it fail:

>>> import torch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/site-packages/torch/__init__.py", line 196, in <module>
    _load_global_deps()
  File "/usr/lib/python3.9/site-packages/torch/__init__.py", line 149, in _load_global_deps
    ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
  File "/usr/lib/python3.9/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: Error relocating /usr/lib/python3.9/site-packages/torch/lib/libgomp-a34b3233.so.1: pthread_attr_setaffinity_np: symbol not found

Can anyone help?

I’m not sure but perhaps this author can help you out https://github.com/petronetto/pytorch-alpine

I’m assuming things work if you a non alpine starting image?