How to use Dockerfile to build the Redis service

Source: Internet
Author: User
Tags chmod mkdir redis centos docker run redis server

Dockerfile


 
ENV REDIS_VERSION = 3.2.1
ENV REDIS_DOWNLOAD_URL = http://download.redis.io/releases/redis-?#redis_version=.tar.gz \
REDIS_DOWNLOAD_SHA1 = 26c0fc282369121b4e278523fce122910b65fbbf
 
RUN \
REDIS_FILE =$ {REDIS_DOWNLOAD_URL ##*/}&&\
Mkdir/tmp/redis &&\
Cd/tmp/redis &&\
Curl-Lk "$ REDIS_DOWNLOAD_URL"-o $ {REDIS_DOWNLOAD_URL ##*/}&&\
Tar xf $ {REDIS_DOWNLOAD_URL ##*/}&&\
Cd restart redis_file=.tar *}&&\
# Yum install-y http://cbs.centos.org/kojifiles/packages/jemalloc/3.6.0/1.el7/x86_64/jemalloc-3.6.0-1.el7.x86_64.rpm \
# Http://cbs.centos.org/kojifiles/packages/jemalloc/3.6.0/1.el7/x86_64/jemalloc-devel-3.6.0-1.el7.x86_64.rpm &&\
Yum install jemalloc-devel-y &&\
Make-j $ (awk '/processor/{I ++} END {print I}'/proc/cpuinfo )&&\
Mkdir-p/usr/local/redis/{bin, etc, var }&&\
Cp-af src/{redis-benchmark, redis-check-aof, redis-check-rdb, redis-cli, redis-sentinel, redis-server}/usr/local/redis/bin /&&\
Cp-a redis. conf/usr/local/redis/etc /&&\
Echo "export PATH =/usr/local/redis/bin: \ $ PATH">/etc/profile. d/redis. sh &&\
Source/etc/profile. d/redis. sh &&\
Useradd-r-s/sbin/nologin-c "Redis Server"-d/data-m-k no redis &&\
# Chmod + x/usr/local/redis/bin/entrypoint. sh &&\
Yum clean all &&\
Rm-rf/tmp/redis
 
COPY entrypoint. sh/usr/local/redis/bin/entrypoint. sh
RUN chmod + x/usr/local/redis/bin/entrypoint. sh
 
VOLUME ["/data"]
WORKDIR/data
 
EXPOSE 6379/tcp
 
ENTRYPOINT ["/usr/local/redis/bin/entrypoint. sh"]
 
CMD ["redis-server"]


Entrypoint. sh script

#! /Bin/bash
######################################## #################################
# File Name: entrypoint. sh
# Author: LookBack
# Email: admin # dwhd.org
# Version:
# Created Time:, Thursday, June 30, 2016
######################################## #################################
 
If! Which redis-server>/dev/null 2> & 1; then source/etc/profile. d/redis. sh; fi
 
Set-e
Sysctl-w net. core. somaxconn = 1024>/dev/null 2> & 1
Sysctl-w vm. overcommit_memory = 1>/dev/null 2> & 1
Echo never>/sys/kernel/mm/transparent_hugepage/enabled
Echo never>/sys/kernel/mm/transparent_hugepage/defrag
 
 
# 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 gosu redis "$0" "$ @"
Fi
 
If ["$1" = 'redis-server']; then
# Disable Redis protected mode [1] as it is unnecessary in context
# Of Docker. Ports are not automatically exposed when running inside
# Docker, but rather explicitely by specifying-p/-P.
# [1] https://github.com/antirez/redis/commit/edd4d555df57dc84265fdfb4ef59a4678832f6da
DoProtectedMode = 1
ConfigFile =
If [-f "$2"]; then
ConfigFile = "$2"
If grep-q '^ protected-mode' "$ configFile"; then
# If a config file is supplied and explicitly specifies "protected-mode", let it win
DoProtectedMode =
Fi
Fi
If ["$ doProtectedMode"]; then
Shift # "redis-server"
If ["$ configFile"]; then
Shift
Fi
Set -- protected-mode no "$ @"
If ["$ configFile"]; then
Set -- "$ configFile" "$ @"
Fi
Set -- redis-server "$ @" # redis-server [config file] -- protected-mode no [other options]
# If this is supplied again, the "latest" wins, so "-- protected-mode no -- protected-mode yes" will result in an enabled status
Fi
Fi
 
Exec "$ @"

 

If you need to build your own image based on this dockerfile, you need the system image pull benyoo/centos: 7.2.1511.20160630, because in order to install the compilation environment and try to describe the image size as short as possible, I directly wrote my own CentOS7.2.1511 system image.
If you do not use the pull benyoo/centos: 7.2.1511.20160630 system image, you must complete the compiling environment on the CentOS system image.
Usage:

Docker run -- name apsaradb for redis-d -- privileged-p 6379: 6379 benyoo/apsaradb for redis: 3.2.1
The -- privileged parameter is required because the system kernel parameter needs to be modified when the redis service is started. If this parameter is not added, a read-only file error is prompted.
Persistent storage:

Docker run -- name redis-server -- privileged-p 6379: 6379-d benyoo/redis: 3.2.1 -- appendonly yes
If you want to store the host machine, you can use-v to mount the directory.

Docker run -- name redis-server -- privileged-p 6379: 6379-v/data/redis:/data-d benyoo/redis: 3.2.1 -- appendonly yes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.