构建Redis Docker镜像

我们手写一枚Redis镜像Dockerfile,来理解与实践CMD与ENTRYPOINT。

#  Dockerfile - Redis 5.0.5
# ================================
# Using Base OS -> CentOS 7 Latest
# ================================
FROM  centos:latest
# =============================================
# LABEL following the standard set of labels by
# The Open Containers Initiative (OCI)
# =============================================
LABEL      org.opencontainers.image.title="DockerImage - Redis" \
           org.opencontainers.image.version="1.0.0" \
           org.opencontainers.image.licenses="MIT" \
# ==============================================================
RUN        PKG_BASE_DEPS="epel-release \
                          make \
                          gcc \
                          gcc-c++" && \
           REDIS_PKG_PATH="/root/redis-5.0.5.tar.gz" && \
           REDIS_PKG_ROOT="/root/redis-5.0.5/" && \
           REDIS_PKG_INSTALLED="/opt/redis_5.0.5/" && \
           REDIS_PKG_URL="http://download.redis.io/releases/redis-5.0.5.tar.gz" && \
           cp "/usr/share/zoneinfo/Asia/Shanghai" "/etc/localtime" && \
           echo "Asia/Shanghai" > "/etc/timezone" && \
           yum -y install "epel-release" && \
           yum -y install ${PKG_BASE_DEPS} && \
           curl -o ${REDIS_PKG_PATH} ${REDIS_PKG_URL} && \
           cd "/root/" && \
           tar -xvz -f ${REDIS_PKG_PATH} && \
           cd ${REDIS_PKG_ROOT} && \
           make V=1 && \
           make PREFIX=${REDIS_PKG_INSTALLED} install && \
           rm -rf ${REDIS_PKG_ROOT} ${REDIS_PKG_PATH} && \
           yum -y autoremove ${PKG_BASE_DEPS} && \
           yum clean all
# ======================================
WORKDIR    /root/
# ======================================
# Using a Docker `entrypoint.sh` and CMD
# ======================================
COPY ./entrypoint.sh /root/
ENTRYPOINT ["sh", "/root/entrypoint.sh"]
CMD ["redis-cli"]

这里,我们使用一枚启动脚本entrypoint.sh作为容器入口点ENTRYPOINT,该脚本接受命令行参数并执行相应的命令。使用CMD指令为启动脚本提供默认参数redis-cli。

#!/bin/bash
# -------------------------------
set -e
# -------------------------------
REDIS_FLAG=false
REDIS_PATH='/opt/redis_5.0.5/bin'
REDIS_COMMANDS="redis-cli \
                redis-server"
# -------------------------------
for redis_cmd in ${REDIS_COMMANDS}
do
    if [[ ${1} == ${redis_cmd} ]]
    then
        REDIS_FLAG=true
    fi
done
# -------------------------------
if [[ ${REDIS_FLAG} == true ]]
then
    ${REDIS_PATH}/${@}
else
    echo '[ERROR]: No such Redis command...'
    exit 1
fi

使用docker build构建起Redis镜像后,可在docker run后附加容器需执行的Redis命令及其参数。

#!/bin/bash
# ------------------------------
REDIS_COMMAND="redis-server"
REDIS_OPTIONS="--loglevel verbose \
               --bind 0.0.0.0 \
               --port 6379 \
               --requirepass foobar123 \
               --maxclients 10000 \
               --timeout 0 \
               --tcp-keepalive 300 \
               --daemonize no \
               --supervised no \
               --pidfile none \
               --databases 32 \
               --always-show-logo yes \
               --appendonly no \
               --appendfsync no"
# ------------------------------
sudo docker run -d -i -t \
                --env LC_ALL=en_US.UTF-8 \
                --name redis_test \
                -p 0.0.0.0:6379:6379 \
                yunwei/redis:latest \
                ${REDIS_COMMAND} ${REDIS_OPTIONS}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注