0

There are various articles like this, this and this and many more, that explains how to use X11 forwarding to run GUI apps on Docker. I am using a Centos Docker container.

However, all of these approaches use

docker run

with all appropriate options in order to visualize the result. Any use of docker run creates a new image and performs the operation on top of that.

A way to work in the same container is to use docker start followed by docker attach and then executing the commands on the prompt of the container. Additionally, the script (let's say xyz.sh) that I intend to run on Docker container resides inside a folder MyFiles in the root directory of the container and accepts a parameter as well

So is there a way to run the script using docker start and/or docker attach while also X11-forwarding it?

This is what I have tried, although would like to avoid docker run and instead use docker start and docker attach

sudo docker run -it \
    --env="DISPLAY" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    centos \
    cd MyFiles \
    ./xyz.sh param1
export containerId='docker ps -l -q'

This in turn throws up an error as below -

/usr/bin/cd: line 2: cd: MyFiles/: No such file or directory
  1. How can I run the script xyz.sh under MyFiles on the Docker container using docker start and docker attach?

  2. Also since the location and the name of the script may vary, I would like to know if it is mandatory to include each of these path in the system path variable on the Docker container or can it be done at runtime also?

Community
  • 1
  • 1
GvanJoic
  • 213
  • 2
  • 14

1 Answers1

2

It looks to me your problem is not with X11 forwarding but with general Docker syntax.

You could do it rather simply:

sudo docker run -it \
    --env="DISPLAY" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    -w MyFiles \
    --rm \
    centos \
    bash -c xyz.sh param1

I added:

  • --rm to avoid stacking old dead containers.
  • -w workdir, obvious meaning
  • /bin/bash -c to get sure your script is interpreted by bash.

How to do without docker run:

run is actually like create then start. You can split it in two steps if you prefer.

If you want to attach to a container, it must be running first. And for it to be running, there must be a process currently running inside.

Herve Nicol
  • 101
  • 6