本帖最后由 java 于 2017-12-27 15:00 编辑
http://www.runoob.com/docker/docker-install-redis.html
通过 Dockerfile 构建创建Dockerfile 首先,创建目录redis,用于存放后面的相关东西。 - runoob@runoob:~$ 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 added
- RUN 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 [url]http://download.redis.io/releases/redis-3.2.0.tar.gz[/url]
- ENV REDIS_DOWNLOAD_SHA1 0c1820931094369c8cdocker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"docker-entrypoint.sh\": executable file not found in $PATH".c19fc1be62f598bc5961ca
- # for redis-sentinel see: [url]http://redis.io/topics/sentinel[/url]
- 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" ]
复制代码 docker-entrypoint.sh 内容- #!/bin/sh
- 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 gosu redis "$0" "$@"
- fi
- exec "$@"
复制代码更多内容参考:
https://github.com/docker-librar ... 342a2fc00644c4f/3.2
通过Dockerfile创建一个镜像,替换成你自己的名字
- runoob@runoob:~/redis$ docker build -t redis:3.2 .
复制代码 创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
- runoob@runoob:~/redis$ docker images redis
- REPOSITORY TAG IMAGE ID CREATED SIZE
- redis 3.2 43c923d57784 2 weeks ago 193.9 MB
复制代码 使用redis镜像运行容器
- runoob@runoob:~/redis$ docker run --name redis -p 6379:6379 -v $PWD/data:/data -d redis:3.2 redis-server --appendonly yes
复制代码命令说明: -p 6379:6379 :将容器的6379端口映射到主机的6379端口 -v $PWD/data:/data :将主机中当前目录下的data挂载到容器的/data redis-server --appendonly yes :在容器执行redis-server启动命令,并打开redis持久化配置
出现如下错误: docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"docker-entrypoint.sh\": executable file not found in $PATH". 在COPY后加上下面一句话: - RUN chmod +x /usr/local/bin/docker-entrypoint.sh
复制代码
验证: 进入docker容器 docker exec -it redis bash 登录本机redis ~ redis-cli
redis 127.0.0.1:6379> 设置 一个键值 127.0.0.1:6379> set key1 "tutut" OK 127.0.0.1:6379> get key1 "tutut"
远程连接 测试
redis-cli -h 192.168.134.106 -p 6379
192.168.134.106:6379> get key1
"tutut"
exit退出
测试 连接 成功!
连接、查看容器使用redis镜像执行redis-cli命令连接到刚启动的容器,主机IP为172.17.0.1 - runoob@runoob:~/redis$ docker run -it redis:3.2 redis-cli -h 172.17.0.1
- 172.17.0.1:6379> info
- # Server
- redis_version:3.2.0
- redis_git_sha1:00000000
- redis_git_dirty:0
- redis_build_id:f449541256e7d446
- redis_mode:standalone
- os:Linux 4.2.0-16-generic x86_64
- arch_bits:64
- multiplexing_api:epoll
- ...
复制代码
|