Docker镜像制作(以Nginx+Keepalived为例)(beta)

 简述

本文主要介绍如何制作镜像。以制作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
View Code

编译成功后使用”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
View Code

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 #调用执行脚本的函数,上面已经定义该函数
    }
}
View Code

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"
View Code

参考:

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

原文地址:https://www.cnblogs.com/whroid/p/13636667.html