分享

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

 WindySky 2017-11-08

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


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

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


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


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

  1. docker start e60da5191243  


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

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

可以看到镜像的ip是

  1. 172.17.0.2  


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

  1. docker exec -i -t e60da5191243 /bin/bash  



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


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

  1. redis-cli -h 192.168.0.152 -p 56379  

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


参考网址:

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


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多