This article introduce how to run GUI apps and Firefox with Docker for Mac, the purpose is just for fun, maybe can be used for build Website test automation with docker, you can check this.
All the commands below are tested with Docker 17.03.0-ce, macOS 10.12.3 and XUbuntu 16.04.2.
It include three sections here:
-
How to run GUI for Linux
-
How to run GUI for macOS
-
How to write Firefox Dockerfile
If you don't have so many times, you can just read Finally commands on each end of section :)
Let's start with a normal GUI app xeyes, here is the Dockerfile based on Alpine Linux:
FROM alpine:latest
MAINTAINER [email protected]
RUN apk add --no-cache --update xeyes
CMD xeyes
You can built your own images with docker build -t xeyes .
.
To run Docker GUI on Linux, You need 3 steps here:
-
Add docker's X11 authority
-
Set $DISPLAY environment for Docker
-
Loading X11 sockets for Docker
-
Build Dockerfile
docker build -t xeyes .
-
Add X11 authority
xhost + local:docker
-
Run docker GUI apps
docker run --rm \ -e DISPLAY=unix$DISPLAY \ -v /tmp/.X11-unix:/tmp/.X11-unix \ xeyes
Now you can see an X11 xeyes apps in your screen.
The macOS is a little more complicate for it's lack of X-Window system,
so you need extra two steps to run GUI apps on macOS when you finished build xeyes images.
-
Establish the X11 server
-
Forward linux X11 socket to macOS X11 server ( Important! )
Notes: Lots of articles online lack of the second step, so cannot successfully run GUI on macOS.
For the two steps above, you need install two softwares here:
-
XQuartz, you can install it with
brew cask install xquartz
-
socat, you can install it with
brew install socat
So finally, you need five steps for run GUI apps on macOS:
-
Forward X11 socket
socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
-
Check your macOS's ip
export MAC_IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
-
Add X11 authority ( optional )
xhost + $MAC_IP
-
Open XQuartz software
-
Run Docker GUI apps
docker run --rm \ -e DISPLAY=$MAC_IP:0 \ xeyes
Now, you can see xeyes on your macOS.
For Firefox, all the command steps are same, but you can not just simply replace the package xeyes with Firefox. The Firefox's Dockerfile need some extra steps below to reach the goal:
-
Install dbus-x11 package
-
Install a fonts for Firefox
-
Change to normal user to run Firefox
-
Prepare a profile folder for Firefox or use
firefox --new-instance
The Dockerfile example is here:
FROM alpine:latest
MAINTAINER [email protected]
ENV URL https://www.google.com
RUN apk --no-cache --update add firefox-esr dbus-x11 ttf-ubuntu-font-family \
&& adduser -S normaluser \
&& echo "normaluser:normaluser" | chpasswd
USER normaluser
CMD firefox --new-instance ${URL}
You can also check my GitHub from more detail.
For Linux Platform, you can run Firefox with docker with:
-
Add X11 authority
xhost + local:docker
-
Run Firefox in Docker
docker run --rm \ -e DISPLAY=unix:$DISPLAY \ -v /tmp/.X11-unix:/tmp/.X11-unix \ --name firefox \ playniuniu/docker-gui-firefox
For macOS platform, same five steps as above:
-
Forward X11 socket
socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
-
Check your macOS's ip
export MAC_IP=$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')
-
Add X11 authority ( optional )
xhost + $MAC_IP
-
Open XQuartz software
-
Run Docker GUI apps
docker run --rm \ -e DISPLAY=$MAC_IP:0 \ playniuniu/docker-gui-firefox