Lost111
Lost111

Reputation: 93

How to Write a Dockerfile to run Python3 + PyQt5

The story is, I have built a little Python software using Python3.8, PyQt5 and Postgres, so I am trying to create a container in order to dockerize all this stuff, I am thinking to create one Dockerfile to create a container for python + pqt5, another container just for Postgres and then using docker-compose to link everything.

The problem is, when I try to create a container for Python and PyQt5, I am facing this error.

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.

And this is actually the Dockerfile I am talking about

FROM python:3

COPY *.py /code/
COPY requirements.txt /code/

WORKDIR /code/

RUN apt-get update -y && apt-get upgrade -y && \
    apt-get install xvfb -y

RUN pip install --no-cache-dir -r requirements.txt

CMD ["python3", "main.py"]

This is the content of my requirements.txt

psycopg2
requests
PyQt5

I tried all the solutions I found on the web and others on the Docker Hub, but none of them gave me the expected result.

Could any good soul shed light on this problem? Preferably with written code.

Upvotes: 5

Views: 6030

Answers (2)

Cho
Cho

Reputation: 163

use dockerhub

https://hub.docker.com/r/fadawar/docker-pyqt5

The image you want seems to have already been created.

Upvotes: 0

It’s not too difficult. Is the Qt software interactive?

  1. If not, then you need to use another “dummy” platform plugin. The xcb plugin is for use with X displays only.

  2. If yes, then you have several choices:

    a. Run an X server on your desktop. Set the DISPLAY env var in the container, and forward relevant port from the server into the container. The SHM extension will not be available, so the performance will be a bit lower than running the application directly on the desktop.

    b. Run a web browser client using Qt webgl streaming technology. The Qt software will run as a server in the container, and offer it’s GUI to a browser client.

    c. Run the Qt application directly (natively) on the desktop, and run the necessary services in the container. Have the application communicate with the services in the container. You can either have some middleware that exposes an API, or you can talk directly to the database. That will somewhat depend on the needs of your project, and the eventual direction it’s heading.

Upvotes: 1

Related Questions