docker 實踐(三)dockerfile 說明
阿新 • • 發佈:2018-12-17
一、dockfile 說明
docker 執行一個自定義的image時,就需要自己來定義,這時候就需要dockfile了。
二、docker commit生成新映象
2.1.執行容器
# docker run -it centos /bin/bash
2.2.安裝vim
# yum install vim
2.3.將容器打包成新映象
# docker commit 07929e3af3ef cent-vim
2.4.檢視新映象
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE cent-vim latest 4659d1ec001d 7 seconds ago 225MB centos latest 1e1148e4cc2c 8 days ago 202MB nginx latest 568c4670fa80 2 weeks ago 109MB ubuntu latest 93fd78260bd1 3 weeks ago 86.2MB busybox latest 59788edf1f3e 2 months ago 1.15MB
這樣操作,非常不便,切容易出錯,如何有更好方式來定義映象呢?這就需要dockfile了
三、dockfile
3.1.編輯dockfile檔案
# vim Dockerfile FROM centos RUN yum update && yum install vim -y
3.2.build構建映象
# docker build -t centos-vim . #-t 將新映象命名為centos-vim Sending build context to Docker daemon 16.9kB Step 1/2 : FROM centos #映象來源 ---> 1e1148e4cc2c Step 2/2 : RUN yum update && yum install vim -y #構建時執行的命令 ---> Running in d7a98cfdffb7 Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.cn99.com No packages marked for update Loaded plugins: fastestmirror, ovl Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: mirrors.aliyun.com * updates: mirrors.cn99.com Resolving Dependencies --> Running transaction check ---> Package vim-enhanced.x86_64 2:7.4.160-5.el7 will be installed .... Removing intermediate container d7a98cfdffb7 ---> 82fd472e3387 Successfully built 82fd472e3387 #構建成功,映象ID為82fd472e3387 Successfully tagged centos-vim:latest #為映象新增一個TAG
檢視映象:
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-vim latest 82fd472e3387 2 minutes ago 327MB cent-vim latest 4659d1ec001d About an hour ago 225MB centos latest 1e1148e4cc2c 8 days ago 202MB nginx latest 568c4670fa80 2 weeks ago 109MB ubuntu latest 93fd78260bd1 3 weeks ago 86.2MB busybox latest 59788edf1f3e 2 months ago 1.15MB
3.3.構建映象時拷貝檔案
3.3.1.檔案內容:
# cat ckl.txt The narcissus bloom every year.
3.3.2.dockfile 內容
# vim Dockerfile FROM centos RUN yum update && yum install vim -y COPY ckl.txt /
3.3.3.構建映象
# docker build -t cent-ckl-vim .
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE cent-ckl-vim latest fc090867ce91 42 seconds ago 327MB centos-vim latest 82fd472e3387 16 minutes ago 327MB cent-vim latest 4659d1ec001d About an hour ago 225MB centos latest 1e1148e4cc2c 8 days ago 202MB nginx latest 568c4670fa80 2 weeks ago 109MB ubuntu latest 93fd78260bd1 3 weeks ago 86.2MB busybox latest 59788edf1f3e 2 months ago 1.15MB
3.3.4.執行容器檢視內容
# docker run -it cent-ckl-vim [[email protected] /]# [[email protected] /]# [[email protected] /]# ls anaconda-post.log bin ckl.txt dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [[email protected] /]# [[email protected] /]# [[email protected] /]# cat ckl.txt The narcissus bloom every year. #拷貝的檔案 [[email protected] /]# [[email protected] /]# [[email protected] /]# vim 1.txt #vim 已經按照
3.3.5.拷貝容器的另一種方式
# vim Dockerfile FROM centos RUN yum update && yum install vim -y ADD nginx-1.12.2.tar.gz /
構建映象:
# docker build -t centos-ckl-ng . Sending build context to Docker daemon 1MB Step 1/3 : FROM centos ---> 1e1148e4cc2c Step 2/3 : RUN yum update && yum install vim -y ---> Using cache #之前已經構建過了以上步驟,所以這裡使用了快取 ---> 82fd472e3387 Step 3/3 : ADD nginx-1.12.2.tar.gz / ---> 248da05fb49e Successfully built 248da05fb49e Successfully tagged centos-ckl-ng:latest
檢視映象:
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos-ckl-ng latest 248da05fb49e 23 seconds ago 333MB centos-vim latest 82fd472e3387 24 minutes ago 327MB cent-vim latest 4659d1ec001d About an hour ago 225MB centos latest 1e1148e4cc2c 8 days ago 202MB nginx latest 568c4670fa80 2 weeks ago 109MB ubuntu latest 93fd78260bd1 3 weeks ago 86.2MB busybox latest 59788edf1f3e 2 months ago 1.15MB
執行映象容器:
# docker run -it centos-ckl-ng [[email protected] /]# [[email protected] /]# [[email protected] /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt nginx-1.12.2 opt proc root run sbin srv sys tmp usr var
#tar 包已經被解壓,COPY則不會
3.3.6.構建映象啟動後執行命令CMD
編輯檔案:
# vim Dockerfile RUN yum install -y httpd RUN yum install -y httpd FROM centos RUN yum update && yum install vim -y ADD nginx-1.12.2.tar.gz / RUN yum install -y httpd && yum -y install initscripts pm-utils CMD ["service","httpd","start"]
構建映象:
# docker build -t cent-ckl-http .
檢視映象:
# docker images REPOSITORY TAG IMAGE ID CREATED SIZE cent-ckl-http latest d848cf5482e6 4 seconds ago 443MB centos-ckl-ng latest 248da05fb49e 22 minutes ago 333MB centos-vim latest 82fd472e3387 About an hour ago 327MB cent-vim latest 4659d1ec001d 2 hours ago 225MB centos latest 1e1148e4cc2c 8 days ago 202MB nginx latest 568c4670fa80 2 weeks ago 109MB ubuntu latest 93fd78260bd1 3 weeks ago 86.2MB busybox latest 59788edf1f3e 2 months ago 1.15MB
執行映象的容器:
# docker run -it cent-ckl-http Redirecting to /bin/systemctl start httpd.service #已經執行了CMD的命令 Failed to get D-Bus connection: Operation not permitted
這個的原因是因為dbus-daemon沒能啟動。其實systemctl並不是不可以使用。將你的CMD或者entrypoint設定為/usr/sbin/init即可。會自動將dbus等服務啟動起來。
然後就可以使用systemctl了。
# docker run -it --privileged cent-ckl-http init [FAILED] Failed to start LSB: Bring up/down networking. See 'systemctl status network.service' for details.
沒有搞定!!!
3.3.7.構建映象設定環境變數及目錄
修改dockfile檔案
# vim Dockerfile FROM centos RUN yum update && yum install vim -y && mkdir /tmp/soft ADD nginx-1.12.2.tar.gz /tmp/soft WORKDIR /tmp/soft/nginx-1.12.2 ENV NAME=ckl RUN yum install -y httpd && yum -y install initscripts pm-utils CMD ["service","httpd","start"] CMD ["echo","$NAME"]
構建映象檔案:
# docker build -t cent-ckl-http .
啟動映象容器:
# docker run -it cent-ckl-http /bin/bash [[email protected] nginx-1.12.2]# #當前目錄 [[email protected] nginx-1.12.2]# [[email protected] nginx-1.12.2]# echo $NAME #變數 ckl
三、dockfile 引數說明
Dockerfile 一般分為四部分:基礎映象資訊、維護者資訊、映象操作指令和容器啟動時執行指令 FROM:指定基礎映象,必須為第一個命令 MAINTAINER: 維護者資訊 RUN:構建映象時執行的命令 ADD:將本地檔案新增到容器中,tar型別檔案會自動解壓,可以訪問網路資源,類似wget COPY:功能類似ADD,但是是不會自動解壓檔案,也不能訪問網路資源 CMD:構建容器後呼叫,也就是在容器啟動時才進行呼叫。 ENTRYPOINT:配置容器,使其可執行化。配合CMD可省去"application",只使用引數。 LABEL:用於為映象新增元資料 ENV:設定環境變數 EXPOSE:指定於外界互動的埠 VOLUME:用於指定持久化目錄 WORKDIR:工作目錄,類似於cd命令 USER:指定執行容器時的使用者名稱或 UID,後續的 RUN 也會使用指定使用者。使用USER指定使用者時,可以使用使用者名稱、UID或GID,或是兩者的組合。當服務不需要管理員許可權時,可以通過該命令指定執行使用者。並且可以在之前建立所需要的使用者 ARG:用於指定傳遞給構建執行時的變數 ONBUILD:用於設定映象觸發器 來源參考:https://www.cnblogs.com/panwenbin-logs/p/8007348.html
四、配置nginx安裝示例
4.1.編輯dockfile檔案
vim Dockerfile FROM centos MAINTAINER ckl RUN yum update && yum -y install vim zlib zlib-devel openssl openssl-devel gcc-c++ pcre pcre-devel initscripts pm-utils RUN mkdir /tmp/soft RUN adduser ckl ADD nginx-1.12.2.tar.gz /tmp/soft WORKDIR /tmp/soft/nginx-1.12.2 RUN ./configure --prefix=/usr/local/nginx --user=ckl --group=ckl --with-http_ssl_module && make && make install ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
4.2.構建映象
docker build -t nginx-centos .
4.3.啟動映象容器:
docker run -p 80 --name 'ckl-ng' -d nginx-centos
4.4.已經啟動了nginx
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f68eeb38e28 nginx-centos "/usr/local/nginx/sb…" 7 minutes ago Up 7 minutes 0.0.0.0:32768->80/tcp ckl-ng
4.5.訪問:
# curl http://172.17.0.3 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>