如果有人已經(jīng)將hg server的image做好了,那么我還要自己作嗎?答案是拿來用吧。
用hg為關(guān)鍵詞搜索,得出以下結(jié)果:
$ docker search hg
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hgomez/gatling 1 [OK]
v7soft/hgdns 0 [OK]
hg8496/gridvis-service 0 [OK]
hgomez/di-centos6-myjenkins-lts 0 [OK]
jrandall/hgi-project 0 [OK]
hgomez/di-centos6-myartifactory 0 [OK]
hgomez/di-centos6-myjenkins 0 [OK]
hgomez/di-centos6-mynexus 0 [OK]
hgomez/di-centos6-myarchiva 0 [OK]
hg8496/piwigo 0 [OK]
hg8496/apache 0 [OK]
hgomez/di-centos6-mygitblit 0 [OK]
hgomez/di-centos6-mygitbucket 0 [OK]
jyotisingh/ubuntu-hg 0
hg8496/dokuwiki 0 [OK]
hg8496/owncloud 0 [OK]
misshie/ucsc-blat-hg19 0 [OK]
ussie/hg-exec adds mercurial to ubuntu:14.04. 0
misshie/ucsc-blat-hg38 0 [OK]
hg8496/gridvis-pc 0 [OK]
Test. Automated builds for this repo are b... 0 [OK]
hg8496/rsync 0 [OK]
secondbit/hgbundler 0
uotbw/hgamer3d Docker image for hgamer3d, see www.hgamer3... 0
hgomez/di-centos6-base 0 [OK]
hgweb貌似不錯的選擇,在github上的主頁是https://github.com/amclain/docker-hgweb 。
將其pull下來,在漫長的等待中我也在思考著如何啟動它。
主頁上提供了它的Dockerfile,通過它我們就可以了解這個image是如果構(gòu)造的。先來說說什么是Dockerfile。
它是用戶創(chuàng)建自定義鏡像的文件。它通常分為四部分:基礎(chǔ)鏡像信息,維護者信息,鏡像操作指令和容器啟動時的指令。
#基礎(chǔ)系統(tǒng)信息,基于ubuntu 14.04構(gòu)建的
FROM ubuntu:14.04
MAINTAINER Alex McLain
RUN apt-get -qq update
#安裝apache、hg、php5
RUN apt-get -y install apache2 apache2-utils curl mercurial php5 php5-cli php5-mcrypt
# TODO: Remove
#是的,vim確實很大,不安裝為好
RUN apt-get -y install vim
RUN echo "colorscheme delek" > ~/.vimrc
# Configure hgweb
ADD hg/add.php /etc/default/hgweb/hg/
ADD hg/hgweb.config /etc/default/hgweb/hg/
ADD hg/hgweb.cgi /etc/default/hgweb/hg/
ADD hg/hgusers /etc/default/hgweb/hg/
# Configure Apache
ADD apache/hg.conf /etc/default/hgweb/apache/
RUN rm /etc/apache2/sites-enabled/*
RUN a2enmod rewrite && a2enmod cgi
ADD load-default-scripts /bin/
RUN chmod u+x /bin/load-default-scripts
#創(chuàng)建一個掛載點,本機或其他容器可以將其掛載。啟動時用-v參數(shù)進行掛載
VOLUME /var/hg
VOLUME /etc/apache2/sites-available
#暴露的端口號,啟動時要通過-p參數(shù)指定
EXPOSE 80
#啟動時執(zhí)行的命令
CMD load-default-scripts && service apache2 start && /bin/bash
有了上述的背景,我們知道啟動時要做兩件事:指定端口號、掛載本地目錄。
比如還是使用端口號80,那么只需用 -p 80:80即可。
比如本機目錄hg-repos用來做hg repo的放置目錄,只需 -v /home/linc/hg-repos:/var/hg/repos 掛載即可。
另外,我們還要將其啟動在后臺(Daemonized),加上-d參數(shù)。
完整啟動命令如下:
docker run -idt -p 80:80 -v /home/linc/hg-repos:/var/hg/repos amclain/hgweb
1.attach方法
docker 自帶attach命令,但此命令的不方便之處在于,多個窗口(同時attach此容器)會同步顯示操作,并且當(dāng)一個窗口exit時,所有窗口都會退出,后臺運行的容器也停止了。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b22cc1880b7a amclain/hgweb:latest "/bin/sh -c 'load-de 3 hours ago Up 3 hours 0.0.0.0:80->80/tcp high_almeida
$ docker attach b22cc1880b7a
root@b22cc1880b7a:/#
2.nsenter
此工具需要從源碼安裝:
$ cd /tmp; curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-; cd util-linux-2.24;
$ ./configure --without-ncurses
$ make nsenter && sudo cp nsenter /usr/local/bin
直接用nsenter命令交互很繁瑣,然后有人寫了配置文件放到bashrc中,就可以方便的使用了。
#docker
export DOCKER_HOST=tcp://localhost:4243
alias docker-pid="sudo docker inspect --format '{{.State.Pid}}'"
alias docker-ip="sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}'"
#the implementation refs from https://github.com/jpetazzo/nsenter/blob/master/docker-enter
function docker-enter() {
if [ -e $(dirname "$0")/nsenter ]; then
# with boot2docker, nsenter is not in the PATH but it is in the same folder
NSENTER=$(dirname "$0")/nsenter
else
NSENTER=nsenter
fi
[ -z "$NSENTER" ] && echo "WARN Cannot find nsenter" && return
if [ -z "$1" ]; then
echo "Usage: `basename "$0"` CONTAINER [COMMAND [ARG]...]"
echo ""
echo "Enters the Docker CONTAINER and executes the specified COMMAND."
echo "If COMMAND is not specified, runs an interactive shell in CONTAINER."
else
PID=$(sudo docker inspect --format "{{.State.Pid}}" "$1")
if [ -z "$PID" ]; then
echo "WARN Cannot find the given container"
return
fi
shift
OPTS="--target $PID --mount --uts --ipc --net --pid"
if [ -z "$1" ]; then
# No command given.
# Use su to clear all host environment variables except for TERM,
# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,
# and start a login shell.
#sudo $NSENTER "$OPTS" su - root
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid su - root
else
# Use env to clear all host environment variables.
sudo $NSENTER --target $PID --mount --uts --ipc --net --pid env -i $@
fi
fi
}
其中有兩個alias和一個function,使用docker-enter會很容易于容器交互并沒有attach中的副作用。如下:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
beb178cd9335 amclain/hgweb:latest "/bin/sh -c 'load-de 11 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp stoic_yonath
$ docker-enter beb178cd9335
root@beb178cd9335:~# ls
root@beb178cd9335:~# pwd
/root
咱也寫個alias放子bashrc中,如下:
alias docker-load-hg-server="sudo docker run -idt -p 80:80 -v /home/linc/hg-repos:/var/hg/repos amclain/hgweb"
啟動它:
$ docker-load-hg-server
[sudo] password for linc:
beb178cd933502970fd12d9a4babecef5475a52d85a207066c665b4a620c5a62
改進
對于文件的掛載,其實直接掛鏡像的/var/hg更好,這樣里面的幾個配置文件如hgusers hgweb.cgi hgweb.config,我們可以直接進行配置。
sudo docker run -idt -p 80:80 -v /home/linc/hg-repos:/var/hg amclain/hgweb
更多建議: