6,docker基础之---Dockerfile构建redis
2023-05-09 06:04:45 阅读次数:124
centos,docker,redis
本机部署redis
编写redis编译安装shell脚本redis_instll.sh
#!/bin/bash
yum install gcc gcc-c++ make openssl openssl-devel -y
cd /home/redis-4.0.9
make && make PREFIX=/usr/local/redis install
mkdir -p /usr/local/redis/conf/
cp /home/redis-4.0.9/redis.conf /usr/local/redis/conf/
上传压缩包到/home/目录下:
[root@docker home]# ll
总用量 1700
-rw-r--r-- 1 root root 1737022 2月 5 15:40 redis-4.0.9.tar.gz
[root@docker home]#
本机使用shell脚本安装:
[root@docker home]# cat redis_install.sh
#!/bin/bash
yum install gcc gcc-c++ make openssl openssl-devel -y
cd /home/redis-4.0.9
make && make PREFIX=/usr/local/redis install
mkdir -p /usr/local/redis/conf/
cp /home/redis-4.0.9/redis.conf /usr/local/redis/conf/
[root@docker home]# shredis_install.sh
启动:
[root@docker redis]# /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
dockerfile部署redis
FROM centos:7
ADD redis-4.0.9.tar.gz /home
COPY redis_install.sh /home
RUN sh /home/redis_install.sh
ENTRYPOINT /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
[root@docker home]# cat dockerfile
FROM centos:7
ADD redis-4.0.9.tar.gz /home
COPY redis_install.sh /home
RUN sh /home/redis_install.sh
ENTRYPOINT /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
[root@docker home]#
执行:
[root@docker home]# docker build -t mycentos:redis .
查看:
[root@docker home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos redis 4581526f39e6 11 seconds ago 599MB
centos 7 eeb6ee3f44bd 16 months ago 204MB
[root@docker home]#
启动容器:
[root@docker home]# docker run -itd mycentos:redis
044dc83758de255a0ed9f80e34572e9cd14b58f23b2e34cbb89570d01f657f1c
[root@docker home]#
进入容器:
[root@docker home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
044dc83758de mycentos:redis "/bin/sh -c '/usr/lo…" 2 minutes ago Up 2 minutes peaceful_almeida
[root@docker home]# docker exec -it 044dc83758de /bin/bash
[root@044dc83758de /]#
查看进程:(redis默认端口号是6379)
[root@044dc83758de /]# ps -ef | grep redis
root 1 0 0 08:19 pts/0 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 27 10 0 08:23 pts/1 00:00:00 grep --color=auto redis
[root@044dc83758de /]#
用redis客户端进入redis验证:
[root@044dc83758de /]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379> set name lengyingxi
OK
127.0.0.1:6379> get name
"lengyingxi"
127.0.0.1:6379> exit
[root@044dc83758de /]# exit
exit
[root@docker home]#
本机连接redis:
重新启动一台redis容器先把端口映射的宿主机:
[root@docker home]# docker run -itd -p 6380:6379 mycentos:redis
89fb6dccdcca3fb5bf90dd15512c4385bca7b38051e7f9c45455ee3cbb3e8f9d
[root@docker home]#
登录客户端:
[root@docker home]# /usr/local/redis/bin/redis-cli -p 6380
127.0.0.1:6380>
127.0.0.1:6380>
127.0.0.1:6380> set name lengyingxi
Error: Connection reset by peer #出现这个说明redis出现了保护模式需要进行修该一下
[root@docker home]#
进入:
[root@docker home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89fb6dccdcca mycentos:redis "/bin/sh -c '/usr/lo…" 3 minutes ago Up 3 minutes 0.0.0.0:6380->6379/tcp, :::6380->6379/tcp happy_neumann
044dc83758de mycentos:redis "/bin/sh -c '/usr/lo…" 12 minutes ago Up 12 minutes peaceful_almeida
[root@docker home]# docker exec -it 89fb6dccdcca /bin/bash
[root@89fb6dccdcca /]#
进入容器之后编辑配置文件:
[root@89fb6dccdcca /]# vi /usr/local/redis/conf/redis.conf
下框搜索:/protected-mode
或者在shell脚本里面再添加两行执行的时候也就直接执行了:
sed -i '69s/127.0.0.1/0.0.0.0/' /usr/local/redis/conf/redis.conf
sed -i '88s/protect-mode yes/protect-mode no/' /usr/local/redis/conf/redis.conf
进行重启容器:
[root@docker home]# docker restart 89fb6dccdcca
89fb6dccdcca
[root@docker home]#
然后用主机客户端去连接:
[root@docker home]# /usr/local/redis/bin/redis-cli -p 6380
127.0.0.1:6380> set name lengyingxi
OK
127.0.0.1:6380> get name
"lengyingxi"
127.0.0.1:6380> exit
[root@docker home]#
查看详情信息:
[root@docker home]# docker inspect 89fb6dccdcca
注意这里的端口是6379是redis的默认端口6380是咱们映射过去的而且进入之后也会显示ip及端口
[root@docker home]# /usr/local/redis/bin/redis-cli -h 172.17.0.3 -p 6379
172.17.0.3:6379> get name
"lengyingxi"
172.17.0.3:6379> set name lyx
OK
172.17.0.3:6379> get name
"lyx"
172.17.0.3:6379> exit
[root@docker home]#
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/lyx888/6099015,作者:冷影玺,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:Docker的数据管理
下一篇:MySql 索引是怎么回事? MySql杂谈