Docker系列~配置外部访问容器中的Redis服务(十)

最近想通过在centos镜像中配置redis服务,从而使外部机器能访问

操作步骤

1. 创建并启动镜像(我的镜像名字叫做:mycentos-redis:1.0)

docker run -i -t -p 192.168.0.152:56379:6379 mycentos-redis:1.0 /bin/bash

2. 进入到镜像的命令行,下载redis,并且编译redis,此处不再说明。看我的redis系列博客文章。

3. 退出镜像(这时镜像已停止运行),回到宿主机,通过命令再次启动镜像

docker start e60da5191243

4. 使用命令查看镜像的ip地址:docker inspect e60da5191243|grep -i add

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
"redis.conf" 1052L, 46696C written
            "LinkLocalIPv6Address": "",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "IPAddress": "172.17.0.2",
            "MacAddress": "02:42:ac:11:00:02",
                    "IPAddress": "172.17.0.2",
                    "GlobalIPv6Address": "",
                    "MacAddress": "02:42:ac:11:00:02"

可以看到镜像的ip是

172.17.0.2

5. 通过命令再次进去到镜像(通过exec进入镜像再退出,容器还是会继续执行。看我另一篇博客关于docker run和exec命令的区别)

docker exec -i -t e60da5191243 /bin/bash

6. 在镜像中,编辑redis的配置文件,修改bind地址为:172.17.0.2,默认端口是6379,保存退出,并启动redis-server

7. 由于在创建该镜像时已经通过宿主的56379端口映射到6379端口,所以此时已经可以访问到镜像中的redis-server了

redis-cli -h 192.168.0.152 -p 56379

完成。(通过宿主IP映射到镜像IP,非镜像独立ip)

 

 

参考网址:

http://dl528888.blog.51cto.com/2382721/1604167

 

------------------------------------------------------

------------------------------------------------------

 

关于我(个人域名)

我的开源项目集Github

 

期望和大家一起学习,共同进步,共勉,O(∩_∩)O谢谢

欢迎交流问题,可加个人QQ 469580884,

或者,加我的群号 751925591,一起探讨交流问题

不讲虚的,只做实干家

Talk is cheap,show me the code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贺佬湿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值