Docker 等待 postgresql 运行 [英] Docker wait for postgresql to be running
问题描述
我在我的项目中使用 postgresql 和 django.我将它们放在不同的容器中,问题是我需要在运行 django 之前等待 postgres.此时我正在使用 sleep 5
在 command.sh
文件中为 django 容器做这件事.我还发现 netcat 可以做到这一点,但我更喜欢没有额外包的方式.curl
和 wget
不能这样做,因为它们不支持 postgres 协议.有办法吗?
I am using postgresql with django in my project. I've got them in different containers and the problem is that i need to wait for postgres before running django. At this time i am doing it with sleep 5
in command.sh
file for django container. I also found that netcat can do the trick but I would prefer way without additional packages. curl
and wget
can't do this because they do not support postgres protocol.
Is there a way to do it?
推荐答案
你的解决方案 tiziano 的问题是 curl 没有默认安装,我想避免安装额外的东西.无论如何,我做了bereal所说的.如果有人需要,这里是脚本.
Problem with your solution tiziano is that curl is not installed by default and i wanted to avoid installing additional stuff. Anyway i did what bereal said. Here is the script if anyone would need it.
import socket
import time
import os
port = int(os.environ["DB_PORT"]) # 5432
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
s.connect(('myproject-db', port))
s.close()
break
except socket.error as ex:
time.sleep(0.1)
这篇关于Docker 等待 postgresql 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!