码头工人在启动Y前等待容器X
等,等候
用途:码头等待容器[集装箱.]
阻塞,直到容器停止为止,然后打印其退出代码。
Docker-Compose.yml
worker: build: myapp/. volumes: - myapp/.:/usr/src/app:ro links: - rabbitmq rabbitmq: image: rabbitmq:3-management
PythonHello示例(rabbit.py):
import pika import time import socket pingcounter = 0 isreachable = False while isreachable is False and pingcounter < 5: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(('rabbitmq', 5672)) isreachable = True except socket.error as e: time.sleep(2) pingcounter += 1 s.close() if isreachable: connection = pika.BlockingConnection(pika.ConnectionParameters( host="rabbitmq")) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print (" [x] Sent 'Hello World!'") connection.close()
工人文件:
FROM python:2-onbuild RUN ["pip", "install", "pika"] CMD ["python","rabbit.py"]