Issues with x11vnc and ENTRYPOINT script after restarting container

This script works perfectly the first time I start the container, but if I restart the container it has issues with the display and I get the following error. If I try to connect via VNC Viewer the container will stop.
“(EE) Server is already active for display 0
If this server is no longer running, remove /tmp/.X0-lock
and start again.”

Sometimes after restarting 2 or 3 times it will suddenly work but I don’t know what to do to resolve this.

Here is the script that I use

"#!/bin/bash
export DISPLAY=${DISPLAY:-:0} # Select screen 0 by default.

if which x11vnc &>/dev/null; then
! pgrep -a x11vnc && x11vnc -create -bg -forever -passwd access1 -quiet -display WAIT$DISPLAY &
fi
! pgrep -a Xvfb && Xvfb $DISPLAY -screen 0 1024x768x16 &
sleep 1
if which fluxbox &>/dev/null; then
! pgrep -a fluxbox && fluxbox 2>/dev/null &
fi

wait"

I’m using base image i386/ubuntu

Seems I fixed the issue by adding the following line at the beginning of the script

rm -rf /tmp/{,.} #not sure why this site doesn’t show the wildcards but it’s {“wildcard”,.“wildcard”}

This removes the following file and folder that was created in tmp that were causing server error for display
.X0-lock
.X11-unix