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

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

问题描述

我在我的项目中使用 postgresql 和 django.我将它们放在不同的容器中,问题是我需要在运行 django 之前等待 postgres.此时我正在使用 sleep 5command.sh 文件中为 django 容器做这件事.我还发现 netcat 可以做到这一点,但我更喜欢没有额外包的方式.curlwget 不能这样做,因为它们不支持 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屋!

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