{"id":18446,"date":"2025-02-25T12:25:24","date_gmt":"2025-02-25T12:25:24","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=18446"},"modified":"2025-02-28T10:37:43","modified_gmt":"2025-02-28T10:37:43","slug":"forward-x11-to-macos-docker","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=18446","title":{"rendered":"Forward X11 to macOS (Docker)"},"content":{"rendered":"<p>xsddiagram is used as an example application.<\/p>\n<hr>\n<p><strong>Dockerfile<\/strong><\/p>\n<pre>\r\nFROM debian:bullseye-slim\r\n\r\n# Install packages\r\nRUN apt update && apt-get install -y xsddiagram libgtk2.0-0\r\n\r\n# Allow X11 forwarding\r\nENV DISPLAY=host.docker.internal:0\r\n\r\n# Run xsddiagram\r\nCMD [\"xsddiagram\"]\r\n<\/pre>\n<p><strong>xsddiagram.py<\/strong><\/p>\n<pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport subprocess\r\nimport sys\r\n\r\ndef run_command(command):\r\n    try:\r\n        return subprocess.run(command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\r\n    except subprocess.CalledProcessError as e:\r\n        print(f&quot;Error: {e}&quot;)\r\n        sys.exit(1)\r\n\r\ndef is_xquartz_installed():\r\n    result = run_command(&quot;brew list --cask | grep xquartz&quot;)\r\n    return result.returncode == 0\r\n\r\ndef is_docker_image_built():\r\n    result = run_command(&quot;docker image ls | grep xsddiagram&quot;)\r\n    return result.returncode == 0\r\n\r\ndef install_and_configure_xquartz():\r\n    if not is_xquartz_installed():\r\n        print(&quot;Installing XQuartz...&quot;)\r\n        run_command(&quot;brew install --cask xquartz&quot;)\r\n\r\n    print(&quot;Configuring XQuartz...&quot;)\r\n    run_command(&quot;defaults write org.xquartz.X11 nolisten_tcp -bool false&quot;)\r\n    run_command(&quot;\/opt\/X11\/bin\/xhost +localhost&quot;)\r\n\r\ndef build_and_run_docker_image():\r\n    if not is_docker_image_built():\r\n        print(&quot;Building Docker image...&quot;)\r\n        run_command(&quot;docker build --tag xsddiagram .&quot;)\r\n\r\n    print(&quot;Running Docker container...&quot;)\r\n    mount_path = os.path.expanduser(&quot;~&quot;)\r\n    run_command(f&quot;docker run --rm --volume {mount_path}:{mount_path} xsddiagram&quot;)\r\n\r\ndef main():\r\n    if sys.platform != &quot;darwin&quot;:\r\n        print(&quot;This script is for macOS only.&quot;)\r\n        sys.exit(1)\r\n\r\n    install_and_configure_xquartz()\r\n    build_and_run_docker_image()\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    main()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>xsddiagram is used as an example application. Dockerfile FROM debian:bullseye-slim # Install packages RUN apt update &#038;&#038; apt-get install -y<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[82],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18446"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18446"}],"version-history":[{"count":26,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18446\/revisions"}],"predecessor-version":[{"id":18492,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/18446\/revisions\/18492"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}