<tfoot id='1CCoR'></tfoot>

        <bdo id='1CCoR'></bdo><ul id='1CCoR'></ul>

      <small id='1CCoR'></small><noframes id='1CCoR'>

    1. <legend id='1CCoR'><style id='1CCoR'><dir id='1CCoR'><q id='1CCoR'></q></dir></style></legend>

        <i id='1CCoR'><tr id='1CCoR'><dt id='1CCoR'><q id='1CCoR'><span id='1CCoR'><b id='1CCoR'><form id='1CCoR'><ins id='1CCoR'></ins><ul id='1CCoR'></ul><sub id='1CCoR'></sub></form><legend id='1CCoR'></legend><bdo id='1CCoR'><pre id='1CCoR'><center id='1CCoR'></center></pre></bdo></b><th id='1CCoR'></th></span></q></dt></tr></i><div id='1CCoR'><tfoot id='1CCoR'></tfoot><dl id='1CCoR'><fieldset id='1CCoR'></fieldset></dl></div>
      1. 获取 python docker 容器与 redis docker 容器交互

        get a python docker container to interact with a redis docker container(获取 python docker 容器与 redis docker 容器交互)

        <tfoot id='19EQ0'></tfoot>

          <tbody id='19EQ0'></tbody>
      2. <small id='19EQ0'></small><noframes id='19EQ0'>

            <bdo id='19EQ0'></bdo><ul id='19EQ0'></ul>

              1. <i id='19EQ0'><tr id='19EQ0'><dt id='19EQ0'><q id='19EQ0'><span id='19EQ0'><b id='19EQ0'><form id='19EQ0'><ins id='19EQ0'></ins><ul id='19EQ0'></ul><sub id='19EQ0'></sub></form><legend id='19EQ0'></legend><bdo id='19EQ0'><pre id='19EQ0'><center id='19EQ0'></center></pre></bdo></b><th id='19EQ0'></th></span></q></dt></tr></i><div id='19EQ0'><tfoot id='19EQ0'></tfoot><dl id='19EQ0'><fieldset id='19EQ0'></fieldset></dl></div>

                  <legend id='19EQ0'><style id='19EQ0'><dir id='19EQ0'><q id='19EQ0'></q></dir></style></legend>
                  本文介绍了获取 python docker 容器与 redis docker 容器交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 docker、redis 和任何类型的网络的新手,(我至少知道 python!).首先,我想出了如何获取 redis docker 映像并在 docker 容器中运行它:

                  I'm new to docker, redis and any kind of networking, (I know python at least!). Firstly I have figured out how to get a redis docker image and run it in a docker container:

                  docker run --name some-redis -d redis
                  

                  据我了解,此 redis 实例具有可用于连接其他容器的端口 6379.

                  As I understand this redis instance has port 6379 available to connect to other containers.

                  docker network inspect bridge
                  
                     "Containers": {
                          "2ecceba2756abf20d5396078fd9b2ecf0d60ab04ca6b8df5e1b631b6fb5e9a85": {
                              "Name": "some-redis",
                              "EndpointID": "09f0069dae3632a2456cb4d82ad5e7c9782a2b58cb7a4ee655f57b5c410c3e87",
                              "MacAddress": "02:42:ac:11:00:02",
                              "IPv4Address": "172.17.0.2/16",
                              "IPv6Address": ""
                          }
                  

                  如果我运行以下命令,我可以与 redis 实例交互并生成键:值对:

                  If I run the following command I can interact with the redis instance and generate key:value pairs:

                  docker run -it --link some-redis:redis --rm redis redis-cli -h redis -p 6379
                  set 'a' 'abc'
                  >OK
                  get 'a'
                  >"abc"
                  quit
                  

                  我已经知道如何制作和运行安装了 redis 库的 docker 容器,该容器将运行以下 python 脚本:

                  I have figured out how to make and run a docker container with the redis library installed that will run a python script as follows:

                  这是我的 Dockerfile:

                  Here is my Dockerfile:

                  FROM python:3
                  ADD redis_test_script.py /
                  RUN pip install redis 
                  CMD [ "python", "./redis_test_script.py" ]
                  

                  这里是redis_test_script.py:

                  Here is redis_test_script.py:

                  import redis
                  print("hello redis-py")
                  

                  构建 docker 镜像:

                  Build the docker image:

                  docker build -t python-redis-py .
                  

                  如果我运行以下命令,脚本将在其容器中运行:

                  If I run the following command the script runs in its container:

                  docker run -it --rm --name pyRed python-redis-py
                  

                  并返回预期的:

                  >hello redis-py
                  

                  看起来两个容器都工作正常,问题是将它们连接在一起,我想最终使用python对redis容器执行操作.如果我按如下方式修改脚本并为 python 容器重建图像,它将失败:

                  It seems like both containers are working ok, the problem is connecting them both together, I would like to ultimately use python to perform operation on the redis container. If I modify the script as follows and rebuild the image for the python container it fails:

                  import redis
                  print("hello redis-py")
                  r = redis.Redis(host="localhost", port=6379, db=0)
                  r.set('z', 'xyz')
                  r.get('z')
                  

                  我收到几个错误:

                  ...
                  OSError: [Errno 99] Cannot assign requested address
                  ...
                  redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.
                  .....
                  

                  看起来他们没有连接,我再次尝试在 python 脚本中使用网桥 IP:

                  It looks like they're not connecting, I tried again using the bridge IP in the python script:

                  r = redis.Redis(host="172.17.0.0/16", port=6379, db=0)
                  

                  并得到这个错误:

                  redis.exceptions.ConnectionError: Error -2 connecting to 172.17.0.0/16:6379. Name or service not known.
                  

                  我尝试了redis子IP:

                  and I tried the redis sub IP:

                  r = redis.Redis(host="172.17.0.2/16", port=6379, db=0)
                  

                  我得到这个错误:

                  redis.exceptions.ConnectionError: Error -2 connecting to 172.17.0.2/16:6379. Name or service not known.
                  

                  感觉好像我从根本上误解了如何让容器相互通信的一些东西.我已经阅读了很多文档和教程,但正如我所说,没有网络经验并且以前没有使用过 docker,所以任何有用的解释和/或解决方案都会非常棒.

                  It feels like I'm fundamentally misunderstanding something about how to get the containers to talk to each other. I've read quite a lot of documentation and tutorials but as I say have no networking experience and have not previously used docker so any helpful explanations and/or solutions would be really great.

                  非常感谢

                  推荐答案

                  这就是 Docker 网络的全部内容.快速解决方案 - 对两个容器使用 host 网络模式.缺点是隔离度低,但你会得到它的快速工作:

                  That's all about Docker networking. Fast solution - use host network mode for both containers. Drawback is low isolation, but you will get it working fast:

                  docker run -d --network=host redis ...
                  docker run --network=host python-redis-py ...
                  

                  然后从 python 连接到 redis 只需使用 localhost 作为主机名.

                  Then to connect from python to redis just use localhost as a hostname.

                  更好的解决方案是使用docker用户定义的桥接网络

                  Better solution is to use docker user-defined bridge network

                  # create network
                  docker network create foo
                  docker run -d --network=foo --name my-db redis ...
                  docker run    --network=foo python-redis-py ...
                  

                  请注意,在这种情况下,您不能使用 localhost 而是使用 my-db 作为主机名.这就是我在启动第一个容器时使用 --name my-db 参数的原因.在用户定义的桥接网络中,容器通过名称相互连接.

                  Note that in this case you cannot use localhost but instead use my-db as a hostname. That's why I've used --name my-db parameter when starting first container. In user-defined bridge networks containers reach each other by theirs names.

                  这篇关于获取 python docker 容器与 redis docker 容器交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  <legend id='NYxOS'><style id='NYxOS'><dir id='NYxOS'><q id='NYxOS'></q></dir></style></legend>

                    • <small id='NYxOS'></small><noframes id='NYxOS'>

                      <tfoot id='NYxOS'></tfoot>
                        <tbody id='NYxOS'></tbody>

                        <i id='NYxOS'><tr id='NYxOS'><dt id='NYxOS'><q id='NYxOS'><span id='NYxOS'><b id='NYxOS'><form id='NYxOS'><ins id='NYxOS'></ins><ul id='NYxOS'></ul><sub id='NYxOS'></sub></form><legend id='NYxOS'></legend><bdo id='NYxOS'><pre id='NYxOS'><center id='NYxOS'></center></pre></bdo></b><th id='NYxOS'></th></span></q></dt></tr></i><div id='NYxOS'><tfoot id='NYxOS'></tfoot><dl id='NYxOS'><fieldset id='NYxOS'></fieldset></dl></div>
                          • <bdo id='NYxOS'></bdo><ul id='NYxOS'></ul>