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.


FROM nginx:stable-alpine as production
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

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/", line 196, in <module>
  File "/usr/lib/python3.9/site-packages/torch/", line 149, in _load_global_deps
    ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
  File "/usr/lib/python3.9/ctypes/", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: Error relocating /usr/lib/python3.9/site-packages/torch/lib/ pthread_attr_setaffinity_np: symbol not found

Can anyone help?

I’m not sure but perhaps this author can help you out

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