Docker入門與實戰系列:深刻

Docker入門與實戰——《Docker ABC》電子書 https://github.com/gudaoxuri/Docker_ABC java

8. 高級

本節我介紹如何本身構建一個Docker鏡像,咱們將構建一個支持scala的運行( http://www.scala-lang.org )環境,而且對外開放SSH服務。linux

8.1. 構建鏡像的兩種方法

構建鏡像有兩種方法:c++

  1. 運行並進行一個基礎容器(如centos , docker run -it centos /bin/bash),而後安裝須要的環境,再用commit 將容器保存成新的鏡像git

  2. 使用Dockerfile構建,這也是推薦的作法github

本節咱們僅介紹第2種方法。docker

8.2. Dockerfile介紹

Dokcerfile是用於構建Docker鏡像的元文件,它的說明及語法見: http://docs.docker.com/engine/reference/builder/ubuntu

8.3. 構建規劃

根據需求能夠發現咱們安裝的主要環境有三個:SSH服務、Java環境(Scala依賴它)、Scala環境,與軟件開發同樣,咱們也能夠分模塊構建,即構建三個鏡像:centos

  1. 只包含SSH服務的基礎鏡像bash

  2. 帶SSH及Java環境的鏡像cookie

  3. 帶SSH、Java及Scala環境的鏡像

8.4. SSH鏡像

FROM centos:centos7 <1>
MAINTAINER gudaoxuri <2>

#---------------Use 163 mirrors--------------- <3>
RUN yum install -y wget &&\
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup && \
    wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo && \
    yum clean all && \
    yum makecache <4>

#---------------Install Common Tools---------------
RUN yum install -y sed curl tar gcc gcc-c++ make git passwd sudo

#---------------Modify Time Zone---------------
ENV TZ "Asia/Shanghai" <5>
ENV TERM xterm

RUN yum install -y ntpdate  && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

#---------------Support Chinese---------------
#RUN yum groupinstall -y "Fonts"  && \
#    echo "LANG=\"zh_CN.UTF-8\"" >> /etc/sysconfig/i18n

#---------------Install SSH---------------
RUN yum install -y openssh-server openssh-clients  && \
    sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config  && \
    echo 'root:123456' | chpasswd  && \
    ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key  && \
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  && \
    mkdir /var/run/sshd

#---------------Setting Common Path---------------
RUN chmod 777 -R /opt/  && \
    mkdir /opt/env/  && \
    mkdir /opt/workspaces/

EXPOSE 22 <6>

CMD ["/usr/sbin/sshd", "-D"] <7>

<1> FROM 表示此鏡像是基於哪一個鏡像構建的
<2> MAINTAINER 開發者的信息
<3> # 註釋
<4> RUN 經常使用命令,用於執行Linux的命令,多用於安裝組件
<5> ENV 經常使用命令,用於設置環境變量
<6> EXPOSE 代表默認對外暴露的端口(docker run -P)
<7> CMD 要執行的服務,多條CMD只執行最後一條
RUN 參數的學問,Docker鏡像是分層的,一個鏡像可能由多個層組成,一次RUN實際上就產生了一層,在編譯Dockerfile的過程當中可能出錯,從新編譯時咱們會發現以前已成功編譯的層不會再次被編譯

8.5. 編譯SSH鏡像

root@ubuntu:/opt/test/dockerfile/ssh# ls
Dockerfile
root@ubuntu:/opt/test/dockerfile/ssh# docker build -t gudaoxuri/ssh .
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon 
Step 0 : FROM centos:centos7
Pulling repository centos
ce20c473cd8a: Download complete 
ce20c473cd8a: Pulling image (centos7) from centos 
168a69b62202: Download complete 
812e9d9d677f: Download complete 
4234bfdd88f8: Download complete 
Status: Downloaded newer image for centos:centos7
 ---> ce20c473cd8a
Step 1 : MAINTAINER gudaoxuri
 ---> Running in 889ea744c458
 ---> 5b1151e6cb0b
Removing intermediate container 889ea744c458

...

Step 10 : CMD /usr/sbin/sshd -D
 ---> Running in ce563073b686
 ---> b61a4adad85f
Removing intermediate container ce563073b686
Successfully built b61a4adad85f

root@ubuntu:/opt/test/dockerfile/ssh# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
gudaoxuri/ssh          latest              b61a4adad85f        10 minutes ago      966 MB
...

8.6. Java鏡像

FROM gudaoxuri/ssh:latest <1>
MAINTAINER gudaoxuri

#---------------Install Java---------------
RUN wget -P /opt/env/ --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie"  http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz  && \
    tar -xzf /opt/env/jdk-8u60-linux-x64.tar.gz -C /opt/env/  && \
    rm /opt/env/jdk-8u60-linux-x64.tar.gz  && \
    mv /opt/env/jdk1.8.0_60 /opt/env/java  && \
    echo "export JAVA_HOME=/opt/env/java" >> /etc/profile
ENV  JAVA_HOME /opt/env/java

RUN echo 'PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
ENV PATH $PATH:$JAVA_HOME/bin

<1> gudaoxuri/ssh:latest 就是咱們以前編譯的鏡像

8.7. Scala鏡像

FROM gudaoxuri/java:latest
MAINTAINER gudaoxuri

#---------------Install Scala---------------
RUN wget -P /opt/env/ http://downloads.typesafe.com/scala/2.10.6/scala-2.10.6.tgz  && \
    tar -xzf /opt/env/scala-2.10.6.tgz -C /opt/env/  && \
    rm /opt/env/scala-2.10.6.tgz  && \
    mv /opt/env/scala-2.10.6 /opt/env/scala  && \
    echo "export SCALA_HOME=/opt/env/scala" >> /etc/profile
ENV SCALA_HOME /opt/env/scala

RUN sed /^PATH=/d /etc/profile >> /etc/profile && \
   echo 'PATH=$PATH:$JAVA_HOME/bin:$SCALA_HOME/bin' >> /etc/profile
ENV PATH $PATH:$JAVA_HOME/bin:$SCALA_HOME/bin

8.8. 編譯Java/Scala鏡像

過程同上,略

8.9. 發佈鏡像

有了新的鏡像後咱們但願把這個鏡像發佈到hub.docker.com上去分享,有兩種方式:

  1. 使用 docker push 發佈前要先登陸 docker login

  2. 使用github發佈

介於國內網絡環境惡劣,第1種方式失敗機率很高,由於它上傳的是生成的鏡像(幾百MB到幾G不等),因此推薦使用第2種方式
請先註冊hub.docker.com 及 github.com 的帳號

8.9.1. 使用github發佈鏡像

不要用IE操做,筆者使用IE11hub.docker.com上操做時發生過不小的困擾。
  • hub.docker.com 上創建github鏈接

  • github中設置權限

  • github中建一個開源項目,注意要包含Dockerfile文件

  • github中設置這個項目的權限

  • hub.docker.com 上創建自動構建項目

  • hub.docker.com 上選擇github上的項目

  • hub.docker.com 上設置項目屬性

  • 很少時就構建好了

更詳細的說明見: https://docs.docker.com/docker-hub/github/
相關文章
相關標籤/搜索