gpt4 book ai didi

docker - 为什么这个docker-compose yml文件不执行该dockerfile中详细说明的命令?

转载 作者:行者123 更新时间:2023-12-02 20:58:29 36 4
gpt4 key购买 nike

我有两个docker镜像:生产者和RabbitMQ队列。我的目标是让生产者(Python)将条目吐入队列。然后,我想调用docker-compose up并观看生产者将内容添加到队列中。

现在,我不是盒子里最亮的蜡笔,如果有人能为我指出为什么事情没有按我的预期去做,我将不胜感激。我的生产者是./producer目录中的两个简单文件。该目录的dockerfile如下所示:

FROM ubuntu:latest

ENV DEBIAN_FRONTEND=noninteractive

# Prepare to install 3.7 + GDAL libraries
RUN apt-get update --fix-missing
RUN apt-get install -y software-properties-common apt-utils
RUN add-apt-repository ppa:ubuntugis/ppa
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update --fix-missing

# Install the packages for GDAL
RUN apt-get install libgdal-dev gdal-bin -y
# Install the packages for python
RUN apt-get install python3.7 python3-pip python3.7-dev -y

RUN python3.7 -m pip install pika

COPY ./example.py example.py

CMD [ "/bin/bash", "-c \"python3.7 example.py\"" ]

我一直在交换最后一行与 ENTRYPOINTCMD,各个版本的 /bin/bashpython3python3.7。运行 docker build -t blah && docker run --rm blah会产生我期望的行为(无法连接到队列,因为它没有运行异常)。

在保存 ./producer的目录中,我有一个 docker-compose.yml文件,如下所示:
version: "3.3"
services:

# RabbitMQ that connects the backend to the frontend
queue:
image: rabbitmq:3-management
expose:
# The standard AMQP protocol port
- 5672
ports:
# HTTP management UI
- '15672:15672'
environment:
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
networks:
rakan:
ipv4_address: 172.16.238.10
deploy:
replicas: 1
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
window: 120s
healthcheck:
test: [ "CMD", "nc", "-z", "localhost", "5672" ]
interval: 5s
timeout: 15s
retries: 1


producer:
depends_on:
- queue
build:
context: ./producer
dockerfile: dockerfile
# entrypoint: "/bin/bash -c \"python example.py\""
expose:
- "5672"
networks:
rakan:
ipv4_address: 172.16.238.11
links:
- "queue"
networks:
- rakan

networks:
rakan:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"

令人沮丧的是, docker-compose up不再执行 ./producer/dockerfile中的命令。它将启动队列,但是它将选择忽略我想要的命令,说:
Attaching to toyexample_queue_1, toyexample_producer_1
toyexample_producer_1 exited with code 0
... All the logs of the RabbitMQ ...

为什么会这样呢?为什么执行docker run时,它的行为方式不一样? 任何帮助将不胜感激。

我尝试过的事情:
  • entrypoint添加到docker-compose.yml中,形式为python3.7 example.pybin/bash python3.7 example.pybash python example.py。所有命令都将以错误代码127终止,声称pythonpython3.7是无法识别的命令。
  • cmd添加到docker-compose.yml中,其结果与最后一个项目符号点
  • 完全相同
  • CMD中的ENTRYPOINT替换./producer/dockerfile
  • 尝试在pythonpython3.7中使用不同的docker-compose.ymldockerfile变体。

  • 我不知道的事情是相关的,但可能会有所帮助
  • 我正在Windows 10(AMD Ryzen CPU)上运行docker v19.03.8
  • 0个其他容器正在运行
  • docker-compose downdocker network/container/image prune是我在这段史诗般的旅程中尝试过的所有命令
  • example.py中的代码是这样的:
  • import pika

    connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.238.10'))
    channel = connection.channel()

    for i in range(100):
    # make a lot of noise
    channel.queue_declare(queue=f'hello_{i}')
    channel.basic_publish(exchange='',
    routing_key='hello_{i}',
    body='Hello World!')
    print(f" [x] Sent 'Hello World!' to {i}")

    connection.close()
  • 强制性:“这不是家庭作业,我不是在试图作弊,等等。”
  • 最佳答案

    首先,请注意以下几点:

    Dockerfile中的

  • EXPOSEexpose中的docker-compose.yml在大多数情况下都是不必要的,除了作为一种文档形式。
  • 只需将您的Dockerfile命名为“Dockerfile”,而不是覆盖docker-compose.yml中的名称即可。减少无关的配置数量使事情更容易阅读(对于其他人和您以后的人)。
  • 不要将IP地址硬编码到您的代码中。请改用容器名称。
  • 同样,请勿在撰写文件中使用links关键字。 docker提供的DNS更加灵活。
  • depends_on几乎完全没有用。它对应用程序一无所知,因此尽管它将确保一个容器在另一个容器之后启动,但它不知道您所依赖的服务是否实际上已启动并正在运行。在大多数情况下,您希望摆脱depends_on并仅在应用程序中实现重新连接逻辑。

  • 最后一个项目符号实际上可能是问题的根源:至少在我的测试中,RabbitMQ开始接受请求所花的时间比 producer容器开始所花的时间长得多。

    考虑到所有这些,我将您的 docker-compose.yml重写为如下所示:
    version: "3.3"

    services:

    # RabbitMQ that connects the backend to the frontend
    queue:
    image: rabbitmq:3-management
    environment:
    RABBITMQ_DEFAULT_USER: "guest"
    RABBITMQ_DEFAULT_PASS: "guest"

    producer:
    build:
    context: ./producer

    您的 example.py看起来像这样:
    import pika
    import time

    while True:
    try:
    connection = pika.BlockingConnection(pika.ConnectionParameters('queue'))
    except pika.exceptions.AMQPConnectionError as err:
    print('rabbitmq connection failed; retrying in 1 second...')
    time.sleep(1)
    else:
    break

    channel = connection.channel()

    for i in range(100):
    # make a lot of noise
    channel.queue_declare(queue=f'hello_{i}')
    channel.basic_publish(exchange='',
    routing_key='hello_{i}',
    body='Hello World!')
    print(f" [x] Sent 'Hello World!' to {i}")

    connection.close()

    运行上述配置会(每次)生成:
    [lars@madhatter npengra317] (master *)$ docker-compose up
    Starting npengra317_queue_1 ... done
    Starting npengra317_producer_1 ... done
    Attaching to npengra317_queue_1, npengra317_producer_1
    queue_1 | 2020-05-09 03:38:25.676 [info] <0.9.0> Feature flags: list of feature flags found:
    .
    .
    .
    queue_1 | 2020-05-09 03:38:26.599 [info] <0.9.0> Server startup complete; 3 plugins started.
    queue_1 | * rabbitmq_management
    queue_1 | * rabbitmq_web_dispatch
    queue_1 | * rabbitmq_management_agent
    queue_1 | completed with 3 plugins.
    queue_1 | 2020-05-09 03:38:26.868 [info] <0.653.0> accepting AMQP connection <0.653.0> (172.25.0.3:34874 -> 172.25.0.2:5672)
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | rabbitmq connection failed; retrying in 1 second...
    producer_1 | [x] Sent 'Hello World!' to 0
    producer_1 | [x] Sent 'Hello World!' to 1
    .
    .
    .
    producer_1 | [x] Sent 'Hello World!' to 97
    producer_1 | [x] Sent 'Hello World!' to 98
    producer_1 | [x] Sent 'Hello World!' to 99
    npengra317_producer_1 exited with code 0

    关于docker - 为什么这个docker-compose yml文件不执行该dockerfile中详细说明的命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61691238/

    36 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com