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

 

已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>I have a docker-compose file with several containers in, two of which are supposed to communicate via a Redis DB. Both containers have connections to Reids and I can read/write from both. However I'd like a container to be triggered every time the something is added from the other container. I thought I could accomplish this via Redis Sub/Pub but it when I run the code it never triggers anything, even when I can see I've added new items to the Redis queue. </p> <p>From this I have two questions: 1. Is what I'm looking to do even possible? Can I do publish/subscribe in in two separate docker containers and expect it work as described above? 2. If it is possible, can someone please point me below where I"m going wrong with this tools? </p> <p>This is my function that I add new data to the Redis queue and then publish the data in Docker container 1. </p> <pre><code>func redisShare(key string, value string) { jobsQueue.Set(key, value, 0) //setting in the queue jobsQueue.Publish(key, value) //publishing for the other docker container to notice fmt.Println("added ", key, "with a value of ", value, "to the redis queue") } </code></pre> <p>I'm using this line in my other docker container to subscribe to the Redis queue and listen for changes: <code>redisdb.Subscribe()</code></p> <p>I would expect if something was added to the redis queue it would share the data to the other container and I'd see the message received, but right now Docker Container 2 just runs and then closes. </p> <p>Thanks! </p> </div>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页