最近想通过在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