Docker等待postgresql运行 [英] Docker wait for postgresql to be running

查看:152
本文介绍了Docker等待postgresql运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用了django的postgresql。我有他们在不同的容器,问题是我需要等待postgres之前运行django。在这个时候,我正在使用django容器的command.sh文件中的 sleep 5 。我还发现,netcat可以做的伎俩,但我宁愿方式没有额外的包。 curl和wget不能这样做,因为他们不支持postgres协议。
有办法吗?

解决方案

您的解决方案的问题tiziano是默认情况下没有安装卷曲我想避免安装额外的东西。无论如何,我做了什么,贝多芬说。这是脚本,如果有人需要它。

  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
除了socket.error作为ex:
time.sleep(0.1)


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?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆