GIMP! Running Desktop Applications Inside a Docker Container

Making GIMP, a graphic editor program made in 1996 run from a Docker Container using nothing but Debian run on a MacBook Pro Operating System.

FROM debian:buster-slim

RUN apt-get update && apt-get install -y \
	gimp \
	--no-install-recommends \
	&& rm -rf /var/lib/apt/lists/*

ENTRYPOINT [ "gimp" ]

Compile into a built image.

docker build -t gimp . 

Let’s write a Docker run command for starting our application.

docker run \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=$DISPLAY \  
--net=host \ 
-v $(PWD)/Pictures:/root/Pictures \
gimp:latest 

If you run this command, you would see.** An error: cannot open display.**

$ docker run \
> -v /tmp/.X11-unix:/tmp/.X11-unix \
> -e DISPLAY=unix$DISPLAY \
> --net=host \
> -v $(PWD)/Pictures:/root/Pictures \
> gimp:latest
Cannot open display:

Before running these commands, ensure x11 is installed or available on your machine. PC and Mac will differ when installing.

Depending on your device you may have (x11) or xQuartz already installed.

Visit https://www.xquartz.org/ direct website for more information. This software is required to run an application from your host machine using only a docker container.

Let’s set up our system to handle connections properly. Run this command in a new terminal window, to get your local private IP address.

xauth list

Use **xauth **list to get your machine’s local _private IP address. _You should be able to locate it easily out of the listed numbers.

_O_r you can find your local private IP address on amacOS with Apple > System Preferences > Network tab.

Another alternative option to finding machine’s local private IP address would be using ifconfig

ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

We will need this IP address for xQuartz later.

XXX.XXX.X.X:0

The Default port for your connection should be :0

Add an environment variable named **DISPLAY **with this local private IP address added to your main terminal session.

export DISPLAY=XXX.XXX.X.X:0

We need to grant access to all clients.

Let’s give **x11 **access to your host machine by opening up XQuartz from your application directory and adding this general rule to the new window terminal available. Keep your main terminal session running from before.

xhost +

This should be added within the new terminal window provided by XQuartz named xterm.

An output a message will be generated from xterm.

access control disabled, clients can connect from any host

Giving access to all connections on the machine.

It is recommended only use this if you are having issues with using xhost +local:docker

Here you are attempting to restrict connection access to your machines hosts that are only on the list of xhost.

However, docker containers may not be forgiving with network settings and require a more general rule, so use xhost+ to allow all connections temporarily.

You can reset this with xhost - to enable only the application is running.

access control enabled, only authorized clients can connecti

Once, access is available.

Use your main session terminal and attempt to reconnect with the environment variable $DISPLAY set within that terminal session. You should see something like this after re-running your export and docker command.

$ export DISPLAY=XXX.XXX.X.X:0

$ docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --net=host -v $(PWD)/Pictures:/root/Pictures gimp:latest

If you have followed closely you should now have a working GIMP! program running from a docker container due to xQuartz and Docker.

Happy Coding!

You have completed the steps needed to run an application from a docker container.

Originally published on Medium

#gimp #xterm #xquartz #docker-container #xhost-docker

GIMP! Running Desktop Applications Inside a Docker Container
4.80 GEEK