0

I try to dockerize a PyQt Application that connect to a containerized MariaDB server using mysqlalchemy. I am completely new to docker. With help on the Qt forum it seems that I could manage to make my app container communicate with the MariaDB container. My residual problem is how to connect to the X11 server? I found some hints in this question on stackoverflow but when I use this solution, I get the following message

sudo docker-compose run app [sudo] Mot de passe de jaaf : Creating network "project_default" with the default driver Creating mydb2 ... done Creating project_app_run ... error

ERROR: for project_app_run Cannot create container for service app: conflicting options: host type networking can't be used with links. This would result in undefined behavior

ERROR: for app Cannot create container for service app: conflicting options: host type networking can't be used with links. This would result in undefined behavior ERROR: Encountered errors while bringing up the project.

I must add that my system is Fedora 38 using X11 not wayland.

How can I resolve this conflict as I have to connect both to MariaDB and to the X11 server?

Hereafter are the relevant files

docker-compose.yml

version: "3"
volumes:
    data:
services:
    db:
        container_name: mydb2
        image: mariadb:latest
        ports:
            - "32001:3306"
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_USER: root
        volumes:
            - data:/var/lib/mysql
    app:
        container_name: biere
        links:
            - "db"
        build: ./
        environment:
            - DISPLAY=$DISPLAY
            - QT_DEBUG_PLUGINS=1
        volumes:
            - /tmp/.X11-unix:/tmp/.X11-unix
            - ~/.Xauthority:/root/.Xauthority
        ports:
            - "5001:5000"
        network_mode: "host"

Dockerfile

FROM fedora:38

ENV DISPLAY=:0
COPY . .
RUN dnf -y upgrade 
RUN dnf -y install python3 python3-pip
RUN python3 -m pip install -r dependencies.txt
RUN dnf install -y mesa-libGLU libxkbcommon libxkbcommon-x11 mesa-libEGL fontconfig dbus-libs xcb-util-cursor xcb-util-wm xcb-util-keysyms xauth
CMD [ "python3", "main.py" ]

db_connection.py

from sqlalchemy import create_engine
from sqlalchemy_utils import create_database, database_exists
from sqlalchemy.orm import sessionmaker, scoped_session
from database.orm_base import Base
db_url="mysql+pymysql://root:root@mydb2:3306/a_db"
if not database_exists(db_url):
    create_database(db_url)
engine = create_engine(db_url,pool_size=5,pool_recycle=3600)
Session =sessionmaker(bind=engine,expire_on_commit=False)
session =Session()
Meaulnes
  • 357
  • 6
  • 20
  • creating a `MARIADB_USER` requires a `MARIADB_PASSWORD` or `MARIADB_PASSWORD_HASH` ([ref](https://github.com/MariaDB/mariadb-docker/blob/master/docker-entrypoint.sh#L406)). – danblack Jul 01 '23 at 00:56

1 Answers1

0

You should delete both the links: and the network_mode: host options.

Links are an artifact of first-generation Docker networking. Docker networks were not part of initial releases of Docker but they are widespread and very mature at this point. The newer Docker-plugin-based version of Compose doesn't support running containers without a Docker network at all. Links have a couple of minor capabilities that networks don't provide, but you almost never need them. If you have links: [simple_name] you can just delete that block without changing anything else.

Host networking completely disables Docker's networking layer. This is occasionally needed for some management-type tools; I more often see it recommended to make localhost be the host system. Using this disables connections between containers by host name, it does not give you the option to hide or remap a container's listening port, and it doesn't really work when you're using Docker Desktop (especially on non-Linux hosts).

The volumes: you show for the app container are probably enough to gain access to the X Window System. You may need to explicitly set an environment variable DISPLAY=:0.0 (the /tmp/.X11-sock socket file corresponds to this exact display address). This does not use TCP networking at all, and so you do not need to change Docker networking options for this.

You do need to make sure you're accessing the database via its Compose service name db; you may need to set an additional environment variable MYSQL_HOST=db or otherwise configure the app container to make this work correctly.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thank you for caring about my problem. I did what you say and it works provided I run this command xhost +local:root. Is there a mean to avoid requiring the user of the docker image typing this and then xhost -local:root. – Meaulnes Jul 01 '23 at 04:44
  • [Can you run GUI applications in a Linux Docker container?](https://stackoverflow.com/questions/16296753/can-you-run-gui-applications-in-a-linux-docker-container) has a recipe to use `xauth` to avoid needing `xhost`. – David Maze Jul 01 '23 at 10:35
  • Which answer number and comment number are you pointing me to ? – Meaulnes Jul 01 '23 at 11:36
  • https://stackoverflow.com/a/25280523 for example uses `xauth` without `xhost`. – David Maze Jul 01 '23 at 11:42
  • I could manage to do this by adding a user with userid 1000 groupid 1000 and running xauth merge /dot.xauthority in the Dockerfile while using volumes: - ~/.Xauthority:/dot/.Xauthority in the docker compose file as explained in [https://janert.me/guides/running-gui-applications-in-a-docker-container/] – Meaulnes Jul 02 '23 at 08:41