Docker学习笔记_12 docker应用 - 部署REDIS服务

选择基镜像

基镜像使用dokcer hub官方提供的redis,https://hub.docker.com/_/redis/

docker pull redis:4.0.10-alpine
docker tag redis:4.0.10-alpine 10.240.4.159/app/redis
docker push 10.240.4.159/app/redis

制作带SSH镜像

mkdir /root/docker_build/redis-ssh
cd /root/docker_build/redis-ssh
cp /usr/share/zoneinfo/Asia/Shanghai .

vi repositories
#------------------------------------------------------------------------
http://mirrors.aliyun.com/alpine/v3.7/main
http://mirrors.aliyun.com/alpine/v3.7/community
#------------------------------------------------------------------------

# docker-entrypoint.sh是原镜像带的,这里只是添加启动ssh命令
vi docker-entrypoint.sh
#------------------------------------------------------------------------
#!/bin/sh
/usr/sbin/sshd -D &
set -e

# first arg is `-f` or `--some-option`
# or first arg is `something.conf`
if [ "${1#-}" != "$1" ] || [ "${1%.conf}" != "$1" ]; then
        set -- redis-server "$@"
fi

# allow the container to be started with `--user`
if [ "$1" = 'redis-server' -a "$(id -u)" = '0' ]; then
        chown -R redis .
        exec su-exec redis "$0" "$@"
fi

exec "$@"
#------------------------------------------------------------------------

vi Dockerfile
#------------------------------------------------------------------------
FROM 10.240.4.159/app/redis:4.0.10-alpine
ADD Shanghai /etc/localtime
ADD repositories /etc/apk/repositories
RUN apk --no-cache update
RUN apk --no-cache add openssh
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" \
    && ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N "" \
    && ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" \
    && ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" \
    && sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config \
    && echo 'root:ydgw.cn' | chpasswd
RUN echo '' > /etc/redis.conf \
    && chown redis:redis /etc/redis.conf \
    && chmod 640 /etc/redis.conf
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD [ "redis-server", "/etc/redis.conf" ]
#------------------------------------------------------------------------

docker build -t 10.240.4.159/app/redis:4.0.10-alpine-ssh .
docker push 10.240.4.159/app/redis:4.0.10-alpine-ssh

创建VOLUME卷

# 在宿主机上执行
mkdir -p /docker_mnt/redis/data

# 拷贝配置好的redis.conf到/docker_mnt/redis目录中
cp redis.conf /docker_mnt/redis/

部署REDIS服务

  1. 登陆Rancher(1.6.18),编排工具用的是默认的Cattle
  2. 应用 - 用户 - 添加应用 - 名称:[REDIS] - 创建
  3. 添加服务 - 在添加服务页面添写配置如下信息 - 创建
    名称: redis
    选择镜像: 10.240.4.159/app/redis:4.0.10-alpine-ssh
    端口映射: 6379:6379/tcp  15921:22/tcp
    
    卷 - 添加卷: /docker_mnt/redis/data:/data
                 /docker_mnt/redis/redis.conf:/etc/redis.conf
    
    网络 - 主机名: redis
    
    调度 - 在指定主机上运行全部容器: docker159
    
文章目录
  1. 1. 选择基镜像
  2. 2. 制作带SSH镜像
  3. 3. 创建VOLUME卷
  4. 4. 部署REDIS服务