【问题标题】:During docker build "socket.gaierror: [Errno -2] Name or service not known" - RabbitMQ Connection在 docker build “socket.gaierror: [Errno -2] Name or service not known” - RabbitMQ Connection
【发布时间】:2022-06-10 20:53:20
【问题描述】:

早上好,

我在编写 RabbitMQ 容器时遇到了一个奇怪的问题。

当我在没有创建 RabbitMQ 测试结构的 python 脚本的情况下构建容器时,它可以正常工作。我访问容器,手动运行脚本,一切都被完美地创建,没有错误。

如果我使用相同的命令(“python3 manager.py”)运行脚本,但在 Dockerfile 的 RUN 条目中,它就像在 RabbitMQ 连接器创建期间突然无法找到主机名或类似的东西。因此,它中止了容器的创建。

我尝试将它作为后台Linux进程执行,并且创建了容器,但是创建RabbitMQ结构总是失败。

Docker-compose

version: "3.8"
services:
  rabbitmq:
    container_name: rabbitmq
    image: rabbitmq
    build: src/server/
    env_file:
      - src/server/server.env
    ports:
      - "15672:15672"
      - "5672:5672"
    hostname: rabbitmq
    networks:
      - rabbitmqnet

networks:
  rabbitmqnet:
    name: rabbitmqnet
    driver: bridge

Dockerfile

FROM rabbitmq:3-management
WORKDIR /app
EXPOSE 15672
COPY . /app
RUN apt-get update -y
RUN apt-get install -y python python3-pip
RUN pip install -r requirements.txt
RUN python3 manager.py

ma​​nager.py

import pika
import config

connection = pika.BlockingConnection(pika.ConnectionParameters(config.server, config.port, '/', pika.PlainCredentials(config.user, config.password)))
channel = connection.channel()

def main():
    createQueue("test-queue")
    createExchange("test-exchange")
    createBinding("test-exchange", "test-queue", "test")

# This method creates a queue.
def createQueue(qName):
    channel.queue_declare(queue=qName)

# This method creates an exchange.
def createExchange(eName):
    channel.exchange_declare(
        exchange=eName, 
        exchange_type='direct'
        )

# This method creates a binding routing key between an exchange and a queue. This allows the publisher to send messages to the queue through the exchange.
def createBinding(eName, qName, routingKey):
    channel.queue_bind(
        exchange=eName, 
        queue=qName, 
        routing_key=routingKey
        )

if __name__ == "__main__":
    main()

config.py

server= 'rabbitmq'
port= 5672
user= 'user'
password= 'password'

错误

[7/7] RUN python3 manager.py:
#0 0.276 Traceback (most recent call last):
#0 0.276   File "manager.py", line 4, in <module>
#0 0.276     connection = pika.BlockingConnection(pika.ConnectionParameters(config.server, config.port, '/', pika.PlainCredentials(config.user, config.password)))
#0 0.276   File "/usr/local/lib/python3.8/dist-packages/pika/adapters/blocking_connection.py", line 360, in init
#0 0.276     self._impl = self._create_connection(parameters, _impl_class)
#0 0.276   File "/usr/local/lib/python3.8/dist-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
#0 0.276     raise self._reap_last_connection_workflow_error(error)
#0 0.276   File "/usr/local/lib/python3.8/dist-packages/pika/adapters/utils/selector_ioloop_adapter.py", line 565, in _resolve
#0 0.276     result = socket.getaddrinfo(self._host, self._port, self._family,
#0 0.276   File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
#0 0.276     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
#0 0.276 socket.gaierror: [Errno -2] Name or service not known
failed to solve: executor failed running [/bin/sh -c python3 manager.py]: exit code: 1

【问题讨论】:

    标签: python docker docker-compose rabbitmq


    【解决方案1】:

    我认为您需要在 dockerfile 中使用 CMD 或 ENTRYPOINT 而不是 RUN。 请阅读有关该主题的更多信息:Difference between RUN and CMD in a Dockerfile

    RUN是一个镜像构建步骤,一个RUN之后容器的状态 命令将提交到容器映像。一个 Dockerfile 可以 有许多相互叠加的 RUN 步骤来构建 图片。

    CMD 是容器启动时默认执行的命令 构建的图像。一个 Dockerfile 只会使用最终定义的 CMD。这 使用 docker run $image 启动容器时可以覆盖 CMD $other_command。

    ENTRYPOINT也和CMD密切相关,可以修改a的方式 容器启动一个图像。

    【讨论】:

    • 我试过了,但输出一直失败,因为另一个错误。如果我在容器内运行脚本,它工作正常...Screenshot
    【解决方案2】:

    解决了!我是码头工人的新手。问题是图像默认以启动 RabbitMQ 结束。

    所以,如果我用 RUN 脚本执行,容器还没有启动,服务器也没有启动,那么肯定无法连接。

    相反,当我使用 CMD 时,我覆盖了默认初始化,因此 RabbitMQ 也从未启动。

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2012-11-06
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多