Docker Compose在启动Y之前等待容器X [英] Docker Compose wait for container X before starting Y
问题描述
我正在使用Rabbitmq和此处
和docker-compose。我的问题是我需要等待rabbitmq完全启动。从到目前为止的搜索结果来看,我不知道如何等待容器x(在我的情况下为工作人员)直到y(rabbitmq)启动。
我发现了这个博客文章检查其他主机是否在线。
我还找到了 docker命令:
等待
用法:docker wait容器[容器...]
阻塞直到容器停止,然后打印其退出代码。
等待容器停止不是我想要的,但是如果
是,是否可以在docker-compose.yml中使用该命令?
到目前为止,我的解决方案是等待几秒钟并检查端口,但这是实现此目的的方法吗?如果我不等,就会收到错误消息。
docker-compose.yml
worker:
构建:myapp /。
卷:
-myapp /.:/ usr / src / app:ro
链接:
-rabbitmq
rabbitmq:
image :rabbitmq:3-management
python hello sample(rabbit.py):
导入pika
导入时间
导入套接字
pingcounter = 0
isreachable = False
,而isreachable为False并且pingcounter< 5:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
试试:
s.connect(('rabbitmq',5672))
isreachable = True
除了socket.error如e:
time.sleep(2)
pingcounter + = 1
s.close()
如果可访问:
连接= 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]发送了 Hello World!))
connection.close()
Dockerfile for worker:
FROM python:2-onbuild
RUN [ pip, install, pika]
CMD [ python, rabbit.py]
2015年11月更新:
shell脚本或在程序中等待可能是一种解决方案。但是看到此问题后,我正在寻找docker / docker-compose的命令或功能
他们提到了实施健康检查的解决方案,这可能是最好的选择。打开的tcp连接并不意味着您的服务已准备就绪或可能保持就绪。除此之外,我还需要更改dockerfile中的入口点。
因此,我希望通过docker-compose on board命令寻求答案,希望他们能解决此问题
更新2016年3月
有一个提案,用于提供一种确定容器是否有效的内置方法。因此,docker-compose可能会在不久的将来使用它。
2016年6月更新
healthcheck将被集成到版本1.12.0中的docker
更新2017年1月
我发现了一个docker-compose解决方案,请参阅:
Docker Compose在启动Y之前先等待容器X
我在示例项目中完成
,您需要在以下位置安装至少docker 1.12.0+。
我还需要扩展Rabbitmq管理Dockerfile ,因为没有在官方映像上安装curl。
现在,我测试Rabbitmq容器的管理页面是否可用。如果curl以exitcode 0结束,则容器应用程序(python pika)将启动并将消息发布到hello队列。现在可以正常工作了(输出)。
docker-compose(2.1版):
版本: 2.1
服务:
应用程序:
构建:app /。
取决于:
兔子:
条件:service_healthy
链接:
-兔子
兔子:
生成:rabbitmq /。
个端口:
- 15672:15672
- 5672:5672
健康检查:
测试:[ CMD, curl, -f, http:// localhost:15672]
间隔:30s
超时:10秒
重试:5
输出:
rabbit_1 | = INFO REPORT ===== 2017年1月25日::: 14:44:21 ===
rabbit_1 |关闭AMQP连接< 0.718.0> (172.18.0.3:36590-> 172.18.0.2:5672)
app_1 | [x]发送 Hello World!
healthcheckcompose_app_1退出,代码为0
Dockerfile (rabbitmq + curl):
FROM rabbitmq:3-management
RUN apt-get update
运行apt-get install -y curl
EXPOSE 4369 5671 5672 25672 15671 15672
版本3不再支持 depends_on 的条件形式。 b $ b因此,我从depends_on移到失败后重新启动。现在,我的应用容器将重新启动2-3次,直到它正常工作为止,但它仍然是docker-compose功能,而不会覆盖入口点。
docker-compose(版本3) :
version: 3;
服务:
rabbitmq:#登录访客:访客
图片:rabbitmq:管理
端口:
- 4369:4369 ;
- 5671:5671
- 5672:5672
- 25672:25672
- 15671:15671;
- 15672:15672
健康检查:
测试:[ CMD, curl, -f, http:// localhost:15672]
间隔:30s
超时:10s
重试:5
应用程序:
构建:./app/
环境:
-HOSTNAMERABBIT = rabbitmq
重新启动:失败
取决于:
-Rabbitmq
链接:
-Rabbitmq
I am using rabbitmq and a simple python sample from here together with docker-compose. My problem is that I need to wait for rabbitmq to be fully started. From what I searched so far, I don't know how to wait with container x (in my case worker) until y (rabbitmq) is started.
I found this blog post where he checks if the other host is online. I also found this docker command:
wait
Usage: docker wait CONTAINER [CONTAINER...]
Block until a container stops, then print its exit code.
Waiting for a container to stop is maybe not what I am looking for but if it is, is it possible to use that command inside the docker-compose.yml? My solution so far is to wait some seconds and check the port, but is this the way to achieve this? If I don't wait, I get an error.
docker-compose.yml
worker:
build: myapp/.
volumes:
- myapp/.:/usr/src/app:ro
links:
- rabbitmq
rabbitmq:
image: rabbitmq:3-management
python hello sample (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()
Dockerfile for worker:
FROM python:2-onbuild
RUN ["pip", "install", "pika"]
CMD ["python","rabbit.py"]
Update Nov 2015:
A shell script or waiting inside your program is maybe a possible solution. But after seeing this Issue I am looking for a command or feature of docker/docker-compose itself.
They mention a solution for implementing a health check, which may be the best option. A open tcp connection does not mean your service is ready or may remain ready. In addition to that I need to change my entrypoint in my dockerfile.
So I am hoping for an answer with docker-compose on board commands, which will hopefully the case if they finish this issue.
Update March 2016
There is a proposal for providing a built-in way to determine if a container is "alive". So docker-compose can maybe make use of it in near future.
Update June 2016
It seems that the healthcheck will be integrated into docker in Version 1.12.0
Update January 2017
I found a docker-compose solution see: Docker Compose wait for container X before starting Y
Finally found a solution with a docker-compose method. Since docker-compose file format 2.1 you can define healthchecks.
I did it in a example project you need to install at least docker 1.12.0+. I also needed to extend the rabbitmq-management Dockerfile, because curl isn't installed on the official image.
Now I test if the management page of the rabbitmq-container is available. If curl finishes with exitcode 0 the container app (python pika) will be started and publish a message to hello queue. Its now working (output).
docker-compose (version 2.1):
version: '2.1'
services:
app:
build: app/.
depends_on:
rabbit:
condition: service_healthy
links:
- rabbit
rabbit:
build: rabbitmq/.
ports:
- "15672:15672"
- "5672:5672"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:15672"]
interval: 30s
timeout: 10s
retries: 5
output:
rabbit_1 | =INFO REPORT==== 25-Jan-2017::14:44:21 ===
rabbit_1 | closing AMQP connection <0.718.0> (172.18.0.3:36590 -> 172.18.0.2:5672)
app_1 | [x] Sent 'Hello World!'
healthcheckcompose_app_1 exited with code 0
Dockerfile (rabbitmq + curl):
FROM rabbitmq:3-management
RUN apt-get update
RUN apt-get install -y curl
EXPOSE 4369 5671 5672 25672 15671 15672
Version 3 no longer supports the condition form of depends_on. So i moved from depends_on to restart on-failure. Now my app container will restart 2-3 times until it is working, but it is still a docker-compose feature without overwriting the entrypoint.
docker-compose (version 3):
version: "3"
services:
rabbitmq: # login guest:guest
image: rabbitmq:management
ports:
- "4369:4369"
- "5671:5671"
- "5672:5672"
- "25672:25672"
- "15671:15671"
- "15672:15672"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:15672"]
interval: 30s
timeout: 10s
retries: 5
app:
build: ./app/
environment:
- HOSTNAMERABBIT=rabbitmq
restart: on-failure
depends_on:
- rabbitmq
links:
- rabbitmq
这篇关于Docker Compose在启动Y之前等待容器X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!