簡述
本文主要介紹如何制作鏡像。以制作nginx-keepalived為例並提交到私服;
鏡像制作思路
基於Nginx官方鏡像安裝Keepalived等軟件;
第一步,編寫制作Dockerfile文件(也可通過其他方式:例如通過docker commit來制作新的鏡像)
FROM nginx ARG TZ="Asia/Shanghai" ENV TZ ${TZ} COPY keepalived/ /etc/keepalived COPY docker-entrypoint-n.sh / RUN apt-get update \ && apt-get install -y keepalived \ && apt install -y procps \ && apt install -y vim \ && chmod +x /etc/keepalived/chk_nginx.sh \ && chmod +x /docker-entrypoint-n.sh ENTRYPOINT ["/docker-entrypoint-n.sh"] CMD ["nginx", "-g", "daemon off;" ]
第二步,編譯dockerfile文件
在終端cd進入Dockerfile所在文件夾目錄,執行編譯命令
#最后點號是表示編譯的目錄,具體命令解釋見參考鏈接
docker build -t nginx-keepalived:0.0.1 .
執行過程如下
whroiddeMacBook-Pro:keepalived-nginx whroid$ docker build -t nginx-keepalived:0.0.1 . Sending build context to Docker daemon 16.38kB Step 1/8 : FROM nginx ---> 4bb46517cac3 Step 2/8 : ARG TZ="Asia/Shanghai" ---> Using cache ---> 01e995bfa2bb Step 3/8 : ENV TZ ${TZ} ---> Using cache ---> 25e3fadc1f14 Step 4/8 : COPY keepalived/ /etc/keepalived ---> Using cache ---> 3ba0f80fcae8 Step 5/8 : COPY docker-entrypoint-n.sh / ---> Using cache ---> a6d47fcd5a3a Step 6/8 : RUN apt-get update && apt-get install -y keepalived && apt install -y procps && apt install -y vim && chmod +x /etc/keepalived/chk_nginx.sh && chmod +x /docker-entrypoint-n.sh ---> Using cache ---> b6a28651011b Step 7/8 : ENTRYPOINT ["/docker-entrypoint-n.sh"] ---> Using cache ---> 3270b0c5f3f8 Step 8/8 : CMD ["nginx", "-g", "daemon off;" ] ---> Using cache ---> 0599e6de7c88 Successfully built 0599e6de7c88 Successfully tagged nginx-keepalived:0.0.1
編譯成功后使用”docker images“查看編譯的鏡像信息
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-keepalived 0.0.1 0599e6de7c88 41 hours ago 249MB
第三步,運行鏡像容器,並驗證是否符合要求
進入容器:docker run -d --privileged --name nginx-keepalived001 nginx-keepalived:0.0.1
以下相關運行情況

第四步,上傳鏡像文件到私服
這里的私服是直接上傳到阿里雲,如果自己搭建私服也是一樣的操作
推送鏡像到阿里雲私服Registry,具體的倉庫名稱以及私服地址根據實際情況調整 $ sudo docker login --username=用戶名 registry.cn-zhangjiakou.aliyuncs.com $ sudo docker tag [ImageId] registry.cn-zhangjiakou.aliyuncs.com/whroid/nginx-keepalived:[鏡像版本號] $ sudo docker push registry.cn-zhangjiakou.aliyuncs.com/whroid/nginx-keepalived:[鏡像版本號]
注:需要先登錄到具體私服,然后給鏡像建立tag,之后將tag推送到遠端私服上;
經過上面一步操作之后,在本地通過docker images命令可以查看
![]()
查看到鏡像已經上傳到阿里雲私服

第五步,從私服下載鏡像
$ sudo docker pull registry.cn-zhangjiakou.aliyuncs.com/whroid/nginx-keepalived:[鏡像版本號]
注:
1,鏡像不帶倉庫地址的默認使用的都是官方公開倉庫。https://hub.docker.com/
附錄
鏡像文件其他文件
1,鏡像目錄結構
2,chk_nginx.sh文件
#!/bin/bash run=`ps -C nginx --no-header | wc -l` if [ $run -eq 0 ]; then service nginx start sleep 3 if [ `ps -C nginx --no-header | wc -l` -eq 0 ]; then service keepalived stop fi fi
3,keepalived.conf文件
global_defs { router_id NKEEP_MASTER #唯一標識,不能重復 vrrp_skip_check_adv_addr #vrrp_strict vrrp_garp_interval 1 vrrp_gna_interval 1 } vrrp_script chk_nginx { script "/etc/keepalived/chk_nginx.sh" interval 2 } vrrp_instance VI_1 { state MASTER interface eth0 #所屬網絡 virtual_router_id 51 priority 100 #權重 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.17.0.100 #vip虛擬地址 } track_script { chk_nginx #調用執行腳本的函數,上面已經定義該函數 } }
4,docker-entrypoint-h.sh
#!/bin/sh # vim:sw=4:ts=4:et set -e if [ -z "${NGINX_ENTRYPOINT_QUIET_LOGS:-}" ]; then exec 3>&1 else exec 3>/dev/null fi if [ "$1" = "nginx" -o "$1" = "nginx-debug" ]; then if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -maxdepth 1 -type f -print -quit 2>/dev/null | read v; then echo >&3 "$0: /docker-entrypoint.d/ is not empty, will attempt to perform configuration" echo >&3 "$0: Looking for shell scripts in /docker-entrypoint.d/" find "/docker-entrypoint.d/" -follow -type f -print | sort -n | while read -r f; do case "$f" in *.sh) if [ -x "$f" ]; then echo >&3 "$0: Launching $f"; "$f" else # warn on shell scripts without exec bit echo >&3 "$0: Ignoring $f, not executable"; fi ;; *) echo >&3 "$0: Ignoring $f";; esac done echo >&3 "$0: Configuration complete; ready for start up" else echo >&3 "$0: No files found in /docker-entrypoint.d/, skipping configuration" fi fi exec "$@" exec "service keepalived start"
參考:
1,docker基礎教程:https://www.runoob.com/docker/docker-dockerfile.html
2,docker官方文檔:https://docs.docker.com/engine/reference/builder/
3,keepalived源碼編譯安裝:https://www.cnblogs.com/yuanpeng-java/p/10022061.html
4,keepalived官方地址:https://www.keepalived.org/
5,nginx官方下載地址:http://nginx.org/en/download.html
