This is my error log:
% docker run --env=DISPLAY -t my-docker1:latest
Traceback (most recent call last):
File "/app/main.py", line 8, in <module>
matplotlib.use('TkAgg')
File "/usr/local/lib/python3.9/site-packages/matplotlib/__init__.py", line 1144, in use
plt.switch_backend(name)
File "/usr/local/lib/python3.9/site-packages/matplotlib/pyplot.py", line 296, in switch_backend
raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Also sharing my code scripts so it can be repeated
main.py
import matplotlib
import matplotlib.pyplot as plt
import os
print(os.getenv('DISPLAY'))
import numpy as np
matplotlib.use('TkAgg')
plt.switch_backend('tkagg')
plt.imshow(np.random.randn(4, 5))
requirements.txt
cycler==0.11.0
fonttools==4.34.4
kiwisolver==1.4.3
lxml==4.9.1
matplotlib==3.5.2
numpy==1.23.1
packaging==21.3
pyparsing==3.0.9
six==1.16.0
soupsieve==2.3.2.post1
webencodings==0.5.1
lxml==4.9.1
main.py
FROM python:3.9.13-slim-buster
WORKDIR /app
ADD main.py .
ADD requirements.txt .
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get install python3-tk -y
RUN apt-get install libx11-dev -y
RUN apt-get install tk -y
RUN apt-get install python3-matplotlib -y
# RUN yum install python3-tkinter -y
# RUN pip3 install matplotlib
RUN pip3 install -r requirements.txt
RUN pip3 --no-cache-dir install -U --force-reinstall matplotlib
RUN export DISPLAY=:0
RUN export MPLBACKEND=TKAgg
RUN ls
ENTRYPOINT [ "python", "main.py" ]
I've read tons of similar questions(like this one and tried them all too as you can see in my solution, but none worked, unfortunately.
Any inputs or even suggestions to alternate backends(I tried PyQt5, but that too didn't work)/alternate libraries similar to matplotlib would be great. Thanks!