1. 程式人生 > 實用技巧 >Docker (五) 利用Dockerfile建立Nginx映象

Docker (五) 利用Dockerfile建立Nginx映象

版本一

第一步:下載基礎映象

本例項是基於centos7映象進行操作的,所以需要下載docker的centos7映象

官方映象連線:https://hub.docker.com/search?q=&type=image

[root@wallace ~] docker pull centos:centos7

第二步:建立一個測試目錄,用來儲存該專案所需要的所有檔案資訊

[root@wallace ~]# mkdir myNginx
[root@wallace ~]# cd myNginx/
[root@wallace mydocker]# touch Dockerfile
[root@wallace mydocker]# ll
總用量
0 -rw-r--r-- 1 root root 0 1月 10 17:34 Dockerfile

第三步:編寫Dockerfile

FROM centos:centos7

MAINTAINER wallace@163.com
# 安裝一些常用工具包
RUN yum install make wget bzip2 -y
# 載入一下阿里的映象源,方便下載編譯的包
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://
mirrors.aliyun.com/repo/Centos-7.repo # 安裝依賴包 RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y # 複製包到docker容器 COPY nginx-1.8.0.tar.gz /opt/nginx/ COPY pcre-8.37.tar.bz2 /opt/pcre/ RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/ RUN tar -xvzf /opt/nginx/nginx-1.8
.0.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx WORKDIR /usr/local/src/nginx-1.8.0 # 編譯安裝 RUN ./configure \
--prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd ENV PATH=/usr/local/nginx/sbin:$PATH EXPOSE 80

第四步:執行建立

[root@server mydocker]# ll
總用量 4
-rw-r--r-- 1 root root 1268 1月  10 17:40 Dockerfile
[root@server mydocker]# docker build -t centos_nginx:v1 .

第五步:建立docker容器

[root@wallace mydocker]# docker run -d -p 80:80 mynginx:v1 nginx -g "daemon off;"
7caa6019653f92db0e85f511aed29f3a56529321e08f08811658758a705f29b4
[root@wallace mydocker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
7caa6019653f        mynginx:v1          "nginx -g 'daemon of…"   5 seconds ago       Up 3 seconds        0.0.0.0:80->80/tcp   vigorous_khorana

第六步:登入網頁測試

版本二:新增CMD指令的方式

第一步:編寫檔案,在之前基礎上新增CMD指令

FROM centos:centos7

MAINTAINER wallace@163.com


RUN yum install make wget bzip2 -y

RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y


COPY nginx-1.8.0.tar.gz /opt/nginx/
COPY pcre-8.37.tar.bz2 /opt/pcre/

RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/

RUN tar -xvzf /opt/nginx/nginx-1.8.0.tar.gz -C /usr/local/src/ \
    && useradd -M -s /sbin/nologin nginx

WORKDIR /usr/local/src/nginx-1.8.0

RUN ./configure --prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd ENV PATH=/usr/local/nginx/sbin:$PATH EXPOSE 80 # 比之前多一行啟動命令,注意是前臺啟動 CMD /bin/sh -c 'nginx -g "daemon off;"'

新增加的CMD /bin/sh -c 'nginx -g "daemon off;"' 表示

當啟動一個容器時候預設執行的命令,如果在啟動容器時賦予了command的話,那麼定義的CMD中的命令將不會被執行,而會去執行command的命令

第二部:構建映象

[root@wallace mydocker]# docker build -t mynginx:v2 .
Sending build context to Docker daemon  2.394MB
Step 1/15 : FROM centos:centos7
 ---> 7e6257c9f8d8
Step 2/15 : MAINTAINER wallace@163.com
 ---> Using cache
 ---> acc7f716a143
Step 3/15 : RUN yum install make wget bzip2 -y
 ---> Using cache
 ---> e63c027f7961
Step 4/15 : RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
 ---> Using cache
 ---> fe16788aee95
Step 5/15 : RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
 ---> Using cache
 ---> 99892504975c
Step 6/15 : RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y
 ---> Using cache
 ---> 55a4588aa407
Step 7/15 : COPY nginx-1.8.0.tar.gz /opt/nginx/
 ---> Using cache
 ---> e69eab8dd722
Step 8/15 : COPY pcre-8.37.tar.bz2 /opt/pcre/
 ---> Using cache
 ---> 671eb0829d88
Step 9/15 : RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/
 ---> Using cache
 ---> 5e32435598c7
Step 10/15 : RUN tar -xvzf /opt/nginx/nginx-1.8.0.tar.gz -C /usr/local/src/     && useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 859c0fc26b64
Step 11/15 : WORKDIR /usr/local/src/nginx-1.8.0
 ---> Using cache
 ---> ebfbec88bba0
Step 12/15 : RUN ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd
 ---> Using cache
 ---> 3229dbf0ae6a
Step 13/15 : ENV PATH=/usr/local/nginx/sbin:$PATH
 ---> Using cache
 ---> 74bf35748279
Step 14/15 : EXPOSE 80
 ---> Using cache
 ---> 0572e560441e
Step 15/15 : CMD /bin/sh -c 'nginx -g "daemon off;"'
 ---> Running in 87bf69fddefa
Removing intermediate container 87bf69fddefa
 ---> 974db7c9c776
Successfully built 974db7c9c776
Successfully tagged mynginx:v2

由於在構建的過程中docker 會採用快取機制,上面構建過程中包含很多using cache,所以這次構件非常快,如果需要重新構建,不想使用cache 需要新增 --no-cache

第三步:啟動並測試

[root@wallace mydocker]# docker run -d -p 81:80 mynginx:v2
79f40500a1e623ee8866cbdd63265fde58c1373b0e5fef914fb98cb9bc20ee83

第四步:檢視效果

版本三:新增ENTRYPOINT

第一步:編寫Dockerfile

FROM centos:centos7

MAINTAINER wallace@163.com

RUN yum install make wget bzip2 -y

RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y

COPY nginx-1.8.0.tar.gz /opt/nginx/

COPY pcre-8.37.tar.bz2 /opt/pcre/

RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/

RUN tar -xvzf /opt/nginx/nginx-1.8.0.tar.gz -C /usr/local/src/ \
    && useradd -M -s /sbin/nologin nginx

WORKDIR /usr/local/src/nginx-1.8.0

RUN ./configure \
--prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd ENV PATH=/usr/local/nginx/sbin:$PATH EXPOSE 80 # 使用ENTRYPOINT ENTRYPOINT ["nginx"] CMD ["-g", "daemon off;"]

第二步:構建映象

[root@wallace mydocker]# docker build -t mynginx:v3 .

第三步:建立容器並測試

[root@wallace mydocker]# docker run -d -p 82:80 mynginx:v3

第四步:測試

第五步:測試啟動後自動關閉的模式daemon off

FROM centos:centos7

MAINTAINER wallace@163.com

RUN yum install make wget bzip2 -y

RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y

COPY nginx-1.8.0.tar.gz /opt/nginx/

COPY pcre-8.37.tar.bz2 /opt/pcre/

RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/

RUN tar -xvzf /opt/nginx/nginx-1.8.0.tar.gz -C /usr/local/src/ \
    && useradd -M -s /sbin/nologin nginx

WORKDIR /usr/local/src/nginx-1.8.0

RUN ./configure \
--prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd

ENV PATH=/usr/local/nginx/sbin:$PATH

EXPOSE 80
# 使用ENTRYPOINT
ENTRYPOINT ["nginx"]

CMD ["-g", "daemon on;"]

測試:

[root@wallace mydocker]# docker run -d -p 84:80 mynginx:v3
e4b88651637f2f0edf1f90150b6ad66679d182ee2400237634c5f79ce1e8308f
[root@wallace mydocker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                NAMES
e4b88651637f        mynginx:v3          "nginx -g 'daemon on…"   16 seconds ago      Exited (0) 15 seconds ago                        modest_wilson

啟動後發現自動退出了容器,就是應為我們使用的是daemonon,如果要正常啟動可以加引數:

[root@wallace mydocker]# docker run -d -p 85:80 mynginx:v3 -g "daemon off;"
455b8dd411bc4fbf4228335e979e983c5ad2c64571a83fe2538b7c310c162182
[root@wallace mydocker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS                NAMES
455b8dd411bc        mynginx:v3          "nginx -g 'daemon of…"   3 seconds ago        Up 2 seconds                    0.0.0.0:85->80/tcp   cranky_chebyshev

然後就可以正常訪問了

版本四:新增VOLUMN

第一步:編寫Dockerfile新增 VOLUMN指令

FROM centos:centos7

MAINTAINER wallace@163.com

RUN yum install make wget bzip2 -y

RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y

COPY nginx-1.8.0.tar.gz /opt/nginx/

COPY pcre-8.37.tar.bz2 /opt/pcre/

RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/

RUN tar -xvzf /opt/nginx/nginx-1.8.0.tar.gz -C /usr/local/src/ \
    && useradd -M -s /sbin/nologin nginx
VOLUME ["/usr/local/nginx/html"]

WORKDIR /usr/local/src/nginx-1.8.0

RUN ./configure \
--prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd

ENV PATH=/usr/local/nginx/sbin:$PATH

EXPOSE 80
# 使用ENTRYPOINT
ENTRYPOINT ["nginx"]

CMD ["-g", "daemon off;"]

第二步:建立映象

[root@wallace mydocker]# docker build -t mynginx:v5 .

第三步:啟動一個映象

[root@wallace mydocker]# docker run -d -p 85:80 --name mynginx1 mynginx:v5 
# 然後檢視映象資訊, 如下所示
[root@wallace mydocker]# docker inspect mynginx1
 "Mounts": [
            {
                "Type": "volume",
                "Name": "c2b140d1df296d5ded06a330e424a81ff5bff02ecb864da938bbfbeb00209e49",
                "Source": "/var/lib/docker/volumes/c2b140d1df296d5ded06a330e424a81ff5bff02ecb864da938bbfbeb00209e49/_data",
                "Destination": "/usr/local/nginx/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

第四步:進入到宿主機的掛載目錄並且檢視目錄內的檔案:

[root@wallace ~]# cd /var/lib/docker/volumes/c2b140d1df296d5ded06a330e424a81ff5bff02ecb864da938bbfbeb00209e49/_data/
[root@wallace _data]# ls
[root@wallace _data]# echo aaa > index.html
[root@wallace _data]# ls
index.html

第五步:進入到docker檢視

[root@wallace _data]# docker exec -it mynginx1 /bin/bash
[root@0adc63e5721b nginx-1.8.0]# cd /usr/local/nginx/html/
[root@0adc63e5721b html]# ls
[root@0adc63e5721b html]# ls
index.html

第六步:驗證

通過訪問發現,在宿主機上面進行了更改,容器內部也發生了變化,這樣就動態的實現網站資料動態更改。

版本五: ONBUILD的使用

Dockerfile1中base image 為A映象,並在Dockefile1中定義ONBUILD指令,構建成新的映象B映象

Dockerfile2中base image 為B映象,構建成新映象C

當使用映象B啟動容器1不會執行OBNUILD中定義的內容,而使用C映象啟動的容器2則會執行ONBUILD定義的內容

第一步:編寫Dockerfile新增ONBUILD指令

FROM centos:centos7

MAINTAINER wallace@163.com

RUN yum install make wget bzip2 -y

RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

RUN yum install automake gcc gcc-c++ autoconf zlib-devel openssl openssl-devel pcre* pcre-devel zlib -y

COPY nginx-1.8.0.tar.gz /opt/nginx/
COPY pcre-8.37.tar.bz2 /opt/pcre/ RUN tar xf /opt/pcre/pcre-8.37.tar.bz2 -C /usr/local/src/ RUN tar -xvzf /opt/nginx/nginx-1.8.0.tar.gz -C /usr/local/src/ \ && useradd -M -s /sbin/nologin nginx WORKDIR /usr/local/src/nginx-1.8.0 # 新增該指令 ONBUILD VOLUME ["/data"] RUN ./configure --prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.37 && make –j 3 ; make install ; cd ENV PATH=/usr/local/nginx/sbin:$PATH EXPOSE 80 ENTRYPOINT ["nginx"] CMD ["-g", "daemon off;"]

第二步:建立容器並啟動測試

[root@server myNginx]# docker run -d -p 86:80 --name centos1 mycentos:v1
1f3c1672cce45e6f0f4e8d927de017fbe0a16f30858427d5cb3c2ec7f7b55d98
# 檢視是否有/data目錄
[root@server myNginx]# docker exec -it centos1 /bin/bash
[root@1f3c1672cce4 nginx-1.8.0]# ll /data
ls: cannot access /data: No such file or directory

第三步:修改基礎映象,建立並驗證

[root@wallace mydocker]# cat Dockerfile 
# 指定基礎映象
FROM mycentos:v1

# MAINTAINER
MAINTAINER [email protected]
[root@wallace mydocker]# docker build -t mycentos:v3 . Sending build context to Docker daemon 2.394MB Step 1/2 : FROM mycentos:v1 # Executing 1 build trigger ---> Using cache ---> 4a8295471514 Step 2/2 : MAINTAINER [email protected] ---> Using cache ---> 92bfba2c223d Successfully built 92bfba2c223d Successfully tagged mycentos:v3
[root@wallace mydocker]# docker run -d -p 88:80 --name mycentos2 mycentos:v3
3ac3b3b563f45028aeaf56a99aaa43354625555e1d117da134bebcb9e92469b5
[root@wallace mydocker]# docker exec -it mycentos2 /bin/bash
[root@3ac3b3b563f4 nginx-1.8.0]# ls /d
data/ dev/

由此可見映象v6包含了v5所有的內容,並且增加了ONBUILD的內容

[root@server ~]