Docker 安裝 Redis
方法一、通過 Dockerfile 構建
創建Dockerfile
首先,創建目錄redis,用於存放後面的相關東西。
run@run:~$ mkdir -p ~/redis ~/redis/data
data目錄將映射為redis容器配置的/data目錄,作為redis數據持久化的存儲目錄
進入創建的redis目錄,創建Dockerfile
FROM debian:jessie# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get addedRUN groupadd -r redis && useradd -r -g redis redis
RUN apt-get update && apt-get install -y --no-install-recommends
ca-certificates
wget && rm -rf /var/lib/apt/lists/*
# grab gosu for easy step-down from root
ENV GOSU_VERSION 1.7
RUN set -x
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)"
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc"
&& export GNUPGHOME="$(mktemp -d)"
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc
&& chmod +x /usr/local/bin/gosu
&& gosu nobody true
ENV REDIS_VERSION 3.2.0
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-3.2.0.tar.gz
ENV REDIS_DOWNLOAD_SHA1 0c1820931094369c8cc19fc1be62f598bc5961ca
# for redis-sentinel see: http://redis.io/topics/sentinel
RUN buildDeps="gcc libc6-dev make"
&& set -x
&& apt-get update && apt-get install -y $buildDeps --no-install-recommends
&& rm -rf /var/lib/apt/lists/*
&& wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL"
&& echo "$REDIS_DOWNLOAD_SHA1 *redis.tar.gz" | sha1sum -c -
&& mkdir -p /usr/src/redis
&& tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1
&& rm redis.tar.gz
&& make -C /usr/src/redis
&& make -C /usr/src/redis install
&& rm -r /usr/src/redis
&& apt-get purge -y --auto-remove $buildDeps
RUN mkdir /data && chown redis:redis /data
VOLUME /data
WORKDIR /data
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 6379
CMD [ "redis-server" ]
通過Dockerfile創建一個鏡像,替換成你自己的名字
run@run:~/redis$ docker build -t redis:3.2 .
創建完成後,我們可以在本地的鏡像列表裡查找到剛剛創建的鏡像
run@run:~/redis$ docker images redis
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 3.2 43c923d57784 2 weeks ago 193.9 MB
方法二、docker pull redis:3.2
查找Docker Hub上的redis鏡像
run@run:~/redis$ docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source ... 2321 [OK] sameersbn/redis 32 [OK]torusware/speedus-redis Always updated official ... 29 [OK]bitnami/redis Bitnami Redis Docker Image 22 [OK]anapsix/redis 11MB Redis server image ... 6 [OK]webhippie/redis Docker images for redis 4 [OK]clue/redis-benchmark A minimal docker image t... 3 [OK]williamyeh/redis Redis image for Docker 3 [OK]unblibraries/redis Leverages phusion/baseim... 2 [OK]greytip/redis redis 3.0.3 1 [OK]servivum/redis Redis Docker Image 1 [OK]...
這裡我們拉取官方的鏡像,標籤為3.2
run@run:~/redis$ docker pull redis:3.2
等待下載完成後,我們就可以在本地鏡像列表裡查到REPOSITORY為redis,標籤為3.2的鏡像。
使用redis鏡像
運行容器
run@run:~/redis$ docker run -p 6379:6379 -v $PWD/data:/data -d redis:3.2 redis-server --appendonly yes43f7a65ec7f8bd64eb1c5d82bc4fb60e5eb31915979c4e7821759aac3b62f330runoob@runoob:~/redis$
命令說明:
-p 6379:6379 : 將容器的6379埠映射到主機的6379埠
-v $PWD/data:/data : 將主機中當前目錄下的data掛載到容器的/data
redis-server --appendonly yes : 在容器執行redis-server啟動命令,並打開redis持久化配置
查看容器啟動情況
run@run:~/redis$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES43f7a65ec7f8 redis:3.2 "docker-entrypoint.sh" ... 0.0.0.0:6379->6379/tcp agitated_cray
連接、查看容器
使用redis鏡像執行redis-cli命令連接到剛啟動的容器,主機IP為172.17.0.1
run@run:~/redis$ docker exec -it 43f7a65ec7f8 redis-cli172.17.0.1:6379> info# Serverredis_version:3.2.0redis_git_sha1:00000000redis_git_dirty:0redis_build_id:f449541256e7d446
redis_mode:standalone
os:Linux 4.2.0-16-generic x86_64
arch_bits:64multiplexing_api:epoll...
※jQuery UI 實例-縮放(Resizable)
※jQuery UI 實例-摺疊面板(Accordion)
TAG:程序員小新人學習 |