Package: dockerx
DescriptionThis repository contains a Python script that allows you to launch a docker
container with X11 graphics support.Typical use caseA typical use case of this script is when you are connecting via ssh from your
laptop to a remote computer (e.g. a DGX server) and you want to launch a docker
container inside the remote computer with X11 support.A quick diagram:Laptop => Remote computer (connected via ssh) => Docker containerYou want to launch a graphical application inside the Docker container and see the GUI in your laptop.RequirementsThis package requires Python >= 3.9. Followingtheseinstructions you can easily get any version of Python quickly up and running.If you are launching this script on a server (e.g. DGX) you need to edit the
configuration file of the SSH server, which is/etc/ssh/sshd_config, and
add the option:X11UseLocalhost noTo edit/etc/ssh/sshd_configyou need superuser access. After editing
this file you need to run:$sudoservicesshreloadThis will reload the SSH server configuration without disconnecting existing
sessions.This package requires Python >= 3.9. If you do not know how to easily switch between Python versions,hereyou have a tutorial on how to do it withpyenv.Install using pip$python3-mpipinstalldockerx--userInstall this package from source$sudoaptinstallpython3python3-pip
$python3-mpipinstalldockerargparse--user
$gitclonehttps://github.com/luiscarlosgph/dockerx.git
$cddockerx
$python3setup.pyinstall--userLaunch containers from your terminalTo launch a container and execute a specific command inside the container:$python3-mdockerx.run--name<container_name>--image<image_name>--nvidia<0_or_1>--command<command>--env<key=value>--volume<src>:<dst>Options:--name: name that you want to give to the container.--image: name of the Docker image you want to deploy as a container.--nvidia: flag to activate the NVIDIA runtime, necessary to run CUDA applications. Requiresnvidia-docker2, if you do not have it installed, checkthislink.--command: use this parameter to launch jobs inside the
container that require graphical (i.e. X11) support. The syntax is--command <path_to_program_in_container> <parameters>. As this package is meant to run graphical applications, no terminal output will be shown. If--commandis not specified, the default command executed inside the container is that
defined by theCMDkeyword in the Dockerfile of your image. If None is defined (as happens for
many images such asubuntuornvidia/cuda:11.7.1-base-ubuntu20.04), the container will start,
do nothing, and stop immediately.--env: flag used to define an environment variable that will be accessible from within the deployed container. You can define as many of them as you want. The syntax is--env <key=value>, e.g.--env DISPLAY=:0 --env PATH=/usr/bin.--volume: flag used to mount a volume within the container, it can be a Docker volume or a folder from the host computer, the syntax is the same for both. You can define as many of them as you want. The syntax is--volume <src>:<dst>, e.g.--volume /tmp/host_folder:/tmp/container_folder --volume /media/usb0:/mnt/usb0(obviously, for this to work, the source folders must exist in the host computer). The source can also be an existing Docker volume, e.g. you create a volume withdocker volume create helloand then mount it inside the container with--volume hello:/tmp/hello.--network: use this option to specify the network that you want your container to connect to. If this option is not specified, the container is connected to the default Docker network.Exemplary command to launch a container and runPyCharmfrom within the container:$ python3 -m dockerx.run --name wild_turin --image luiscarlosgph/pycharm:latest --nvidia 1 --command /home/docker/pycharm/bin/pycharm.shThis should displayPyCharmin your screen.If you want to run multiple commands, for example to install a graphical application and then run it, you can do it like this:$ python3 -m dockerx.run --image nvidia/cuda:11.7.1-base-ubuntu20.04 --nvidia 1 --command '/bin/bash -c "apt update && apt install -y x11-apps && xclock"'This should displayxclockin your screen.If you want to run a container foreverso you can 1) bash into it withdocker exec -it <container id> /bin/bashand 2) run GUIs inside the container, you can usesleep infinityas your command:$python3-mdockerx.run--image<imagename>--nvidia<0or1>--command'sleep infinity'For example, to run just anubuntucontainer:$python3-mdockerx.run--imageubuntu--command'sleep infinity'Togetacontainerterminalrun:dockerexec-itb05bd722477e/bin/bash
Tokillthecontainerrun:dockerkillb05bd722477e
Toremovethecontainerrun:dockerrmb05bd722477e

$dockerexec-itb05bd722477e/bin/bash
root@b05bd722477e:/#aptupdate&&aptinstall-yx11-apps
root@b05bd722477e:/#xclockAfter runningxclockabove you should see a clock in your local screen.To run anubuntucontainerwith CUDA support:$python3-mdockerx.run--imagenvidia/cuda:11.7.1-base-ubuntu20.04--nvidia1--command'sleep infinity'Togetacontainerterminalrun:dockerexec-it0b2b964b8b8f/bin/bash
Tokillthecontainerrun:dockerkill0b2b964b8b8f
Toremovethecontainerrun:dockerrm0b2b964b8b8f

$dockerexec-it0b2b964b8b8f/bin/bash
root@0b2b964b8b8f:/#nvidia-smi
TueSep2711:12:562022+-----------------------------------------------------------------------------+|NVIDIA-SMI515.65.01DriverVersion:515.65.01CUDAVersion:11.7||-------------------------------+----------------------+----------------------+|GPUNamePersistence-M|Bus-IdDisp.A|VolatileUncorr.ECC||FanTempPerfPwr:Usage/Cap|Memory-Usage|GPU-UtilComputeM.||||MIGM.||===============================+======================+======================||0NVIDIATITANX...Off|00000000:01:00.0On|N/A||23%35CP817W/250W|369MiB/12288MiB|0%Default||||N/A|+-------------------------------+----------------------+----------------------++-----------------------------------------------------------------------------+|Processes:||GPUGICIPIDTypeProcessnameGPUMemory||IDIDUsage||=============================================================================|+-----------------------------------------------------------------------------+
root@0b2b964b8b8f:/#aptupdate&&aptinstall-yx11-apps
root@0b2b964b8b8f:/#xclockAs in the example above,xclockshould be now shown in your local display.
However, this container has CUDA support. GPU applications can now be executed
and displayed from within the container.Launch containers from your Python codeExemplary code snippet that shows different ways to launch containers using the
Python module in this repo.importdockerxdl=dockerx.DockerLauncher()# If no command is specified here, the CMD in your Dockerfile will be executed, if there is no CMD in your# Dockerfile either, then this container will be created and immediately destroyedcontainer_0=dl.launch_container('ubuntu')print(container_0.id)# If a command is specified here, the CMD in your Dockerfile will be ignored and overridden by the command# specified herecontainer_1=dl.launch_container('ubuntu',command='sleep infinity')print(container_1.id)# Launch a container with CUDA support (as a command is specified, the CMD in your Dockerfile will be ignored)container_2=dl.launch_container('nvidia/cuda:11.7.1-base-ubuntu20.04',command='sleep infinity',nvidia_runtime=True)print(container_2.id)Run unit tests$python3tests/test_docker_launcher.pyAuthorLuis Carlos Garcia-Peraza Herrera (luiscarlos.gph@gmail.com).LicenseThe code in this repository is released under anMIT license.