在 Docker 中運行 OpenOffice
Docker與GUI應用
Docker是開源的容器技術,容器是比虛擬機更輕量的虛擬化技術,優勢是隔離軟件的運行環境并且最小化其額外的開銷。隔離運行環境的好處之一就是可以輕易創建干凈的開發環境,而在我第一次Docker分享中,大家最關心的問題就是“Docker可以運行GUI應用嗎”。
Docker作為虛擬化技術,并沒有改變進程的運行方式和圖像顯示協議,因此Docker是可以運行GUI應用的。就像在裸機中要運行圖形界面,我們有必要了解下Linux的X Window協議。在Linux中,一個GUI應用的顯示都經過X Window這個C/S模型,簡單概括就是X Server在后臺運行,接受X Client的請求,并將顯示的結果通過特定安全的協議返回。
運行Docker GUI應用的原理與之類似,下面將一步步帶領大家創建基于Docker的圖形化程序。
Dockerized OpenOffice
Dockerized-openoffice就是運行在容器內的GUI應用,執行命令docker run -d -p 6080:6080 tobegit3hub/dockerized-openoffice
然后在瀏覽器打開http://127.0.0.1:6080/vnc.html就可以看到圖形界面的OpenOffice應用。
其實玄機就在Dockerfile中,代碼中有安裝apt-get install -y lxde x11vnc xvfb
這一步,就是安裝我們前面提到的X Server,這樣通過特定的VNC客戶端就可以訪問這個GUI應用了。這里我們選擇noVNC客戶端來連接我們的Dockerized應用,導入noVNC源碼,啟動服務器,打開6080端口,這樣我們docker run
以后就可以通過瀏覽器來訪問GUI應用了。
FROM ubuntu:14.04 MAINTAINER Doro Wu <fcwu.tw@gmail.com>ENV DEBIAN_FRONTEND noninteractive ENV HOME /root
setup our Ubuntu sources (ADD breaks caching)
RUN echo "deb http://tw.archive.ubuntu.com/ubuntu/ trusty main\n\ deb http://tw.archive.ubuntu.com/ubuntu/ trusty multiverse\n\ deb http://tw.archive.ubuntu.com/ubuntu/ trusty universe\n\ deb http://tw.archive.ubuntu.com/ubuntu/ trusty restricted\n\ deb http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main\n\ "> /etc/apt/sources.list
no Upstart or DBus
https://github.com/dotcloud/docker/issues/1724#issuecomment-26294856
RUN apt-mark hold initscripts udev plymouth mountall RUN dpkg-divert --local --rename --add /sbin/initctl && ln -sf /bin/true /sbin/initctl
RUN apt-get update \ && apt-get install -y --force-yes --no-install-recommends supervisor \ openssh-server pwgen sudo vim-tiny \ net-tools \ lxde x11vnc xvfb \ gtk2-engines-murrine ttf-ubuntu-font-family \ nodejs \ libreoffice firefox \ && apt-get autoclean \ && apt-get autoremove \ && rm -rf /var/lib/apt/lists/*
ADD noVNC /noVNC/
ADD startup.sh / ADD supervisord.conf / EXPOSE 6080 EXPOSE 5900 EXPOSE 22 WORKDIR /
Remove LibOffice
RUN apt-get remove -y --purge libreoffice libexttextcat-data && sudo apt-get -y autoremove
Install wget
RUN apt-get update -y && \ apt-get install -y wget
Install OpenOffice
RUN wget http://sourceforge.net/projects/openofficeorg.mirror/files/4.0.0/binaries/en-US/Apache_OpenOffice_4.0.0_Linux_x86-64_install-deb_en-US.tar.gz RUN tar -xvf Apache_OpenOffice.tar.gz RUN dpkg -i en-US/DEBS/.deb RUN dpkg -i en-US/DEBS/desktop-integration/*.deb
ENTRYPOINT ["/startup.sh"]</pre>
實際上這里我們把整個Linux桌面管理器都裝了,因此運行Firefox等一切應用都是可能的。
參考鏈接
-
https://github.com/fcwu/docker-ubuntu-vnc-desktop
使用VNC和noVNC搭建Ubuntu環境的實例,無論是源碼還是使用的技術都值得參考。
-
http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
在Docker運行NetBeans等開發環境,雖然使用價值不大但Docker就真的做到了。
-
http://stackoverflow.com/questions/16296753/can-you-run-gui-apps-in-a-docker-container
運行Firefox而不需要安裝Firefox,整個Dockerfile也相當簡單。
-
https://blog.docker.com/2013/07/docker-desktop-your-desktop-over-ssh-running-inside-of-a-docker-container/
Docker官方文檔介紹通過SSH運行桌面系統,使用的是Xpra。