DockerApp
Not some time ago, I made a C++ algorithm visualizer app inspired by those on youtube. I was able to visualize one algorithm, the selection sort. However, my goal in this project is to containerize it on Linux and deploy on other platforms like Windows and other distributions through docker.
Overall, I'll detail how to compile from source with just the source files, how to run the application, how I containerized it, and where to retrieve the files. This was a good experiement to see how docker can be used to containerize gui apps and expose myself to how docker works.
Compiling from source
-
Install SFML, I downloaded version 2.6
- My makefile must be in the same directory as the downloaded SFML content. In the following line, its assumed that SFML has both the "lib" and "include" directories from SFML. Additionally, src is where the source code is located. Otherwise, edit my makefile in accordance to the official SFML and Linux guide
$: makefile SFML src -
My makefile is composed of three stages:
- Building the object files
g++ -c ${INPUT_SRC} -I ${SFML_INC}- Linking and Compiling the object files
g++ $(FLAGS) $(INPUT_OBJ) -o $(OUTPUT) -L $(SFML_LIB) -lsfml-graphics -lsfml-window -lsfml-system- Cleaning Up
rm -f $(INPUT_OBJ) -
Check my makefile linked below to see what the variables within each line expands to
- My source files, makefile, and other goodies will be hosted here:
Notes about the source code
- Note that my program needs a font to be chosen, since this project is focused on the containerized version and not the application itself, if you want this to work the font must be
chosen. Here are the locations to change the font within the code to make this work.
- main.cpp in line 15
- sort_algor.cpp in line 82
- Either edit LD_LIBRARY_PATH or edit /etc/ld.so.conf to have access to library when running program
- LD_LIBRARY_PATH is quick but unstable if handled incorrectly, use for debugging
- set this to "/path/to/SFML/lib", so like "~/SFML/lib" in my case
- Not permanent
- /etc/ld.so.conf is permanent and much more stable
- This is what I used for the container image to use this anytime
- Added "/path/to/SFML/lib" to the file
- Used ldconfig to update linker in runtime
- LD_LIBRARY_PATH is quick but unstable if handled incorrectly, use for debugging
How to containerize
- After getting comfortable with the docker tutorial, I began to build an image to containerize this program using the Debian image as the base image
- VERY BAREBONES Debian image, simply using apt update will allow the container to grab needed packages
- Required packages:
- build-essential (To compile with g++)
- libx11-dev (This and below for SFML)
- libgl1-mesa-dev
- libudev-dev
- libxrandr-dev
- libxcursor-dev
- libfreetype6-dev
- libopenal-dev
- libflac-dev
- libvorbis-dev
- Compile the program from source
- import the source code, makefile, and SFML libraries to the container
- Set the font
- Use the makefile to compile the application
- Update ld.so.conf, simply added the path to SFML library within the config file
- use ldconfig to update the linker
Running the container image and getting the GUI to work
-
First, docker and the built image is required
- If you made your own use that, otherwise you can use my image
- docker pull dankjoker/algovisualizer
-
X11 Fowarding and Environmental Variable for DISPLAY must be passed into the following:
- First give docker permission to use X11 fowarding with:
xhost +local:docker sudo docker run -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix algovisual-itruns the container within an interactive terminal-e DISPLAY=$DISPLAYpasses the host's display so the visualizer can display-v /tmp/.X11-unix:/tmp/.X11-unixconnects the unit socket files for X11 allowing the visualizer to pass through X11algovisualruns this command within the container, so running the container image will automatically run the visualizer
- After Running use
xhost -local:dockerto remove local permissions from docker for security purposes
- First give docker permission to use X11 fowarding with:
Automating this process
- Once this image has been set up to run SFML, the executable is present, and everything works, it's time to automate this process so the end user simply runs the docker image, sees the gui, and once the user chooses to leave the gui the container stops running automatically.
- We accomplish this using a compose.yaml file allowing us to break down the previous docker run command and give the appropiate variables.
-
A bash script is then used to launch this compose file and automatically handle the xhost command
-
- First the compose file selects the container image, "algovisual"
- Second, the file runs the executable residing in the container
- Thirdly, the composer then passes the display variable into the container so the GUI can display
- Finally, it connects the unit socket files for X11, allowing the GUI to pass from the container to the host system
-
- First command gives Docker permission to use X11 server for the host to display the visualizer. The next command uses the docker compose command on the compose file to start the process. The final command then removes Docker's permission to use X11 server for security purposes once the user exists the application.
- The -f option allows you to specify where the compose file is at.
Expirementing On Windows
- Implementation on Windows within Docker was successful!
- Windows launched the image and was able to run the application, did not need to install SFML or compile from source. Although, it was laggy due to Window's not natively supporting X11
- Tools needed, Docker Desktop and X11 server on Windows
- Docker Desktop
- XMing, was the tool I used
- Note I pursued this project before WSL2.0 was released, WSL2.0 is said to have X11 compatability it may be worthy to expirement with that